Showing posts with label kohana 3 router. Show all posts
Showing posts with label kohana 3 router. Show all posts

Kohana 3 :: alternate 404 method

controller

<?php defined('SYSPATH') or die('No direct script access.');

if ( ! $this->_auth->logged_in('login'))
{
 throw new Kohana_Exception('You are not allowed to view this resource.');
}

Kohana 3 :: Routes, URLs, and Links / part 3

Request Parameters

The directory, controller and action can be accessed from the Request instance in either of these two ways:
$this->request->action;
Request::instance()->action;
All other keys specified in a route can be accessed from within the controller via:
$this->request->param('key_name');
The Request::param method takes an optional second argument to specify a default return value in case the key is not set by the route. If no arguments are given, all keys are returned as an associative array.

Kohana 3 :: Routes, URLs, and Links / part 2


More Route Examples

There are countless other possibilities for routes. Here are some more examples:
/*
 * Authentication shortcuts
 */
Route::set('auth', '<action>',
  array(
    'action' => '(login|logout)'
  ))
  ->defaults(array(
    'controller' => 'auth'
  ));
 
/*
 * Multi-format feeds
 *   452346/comments.rss
 *   5373.json
 */
Route::set('feeds', '<user_id>(/<action>).<format>',
  array(
    'user_id' => '\d+',
    'format' => '(rss|atom|json)',
  ))
  ->defaults(array(
    'controller' => 'feeds',
    'action' => 'status',
  ));
 
/*
 * Static pages
 */
Route::set('static', '<path>.html',
  array(
    'path' => '[a-zA-Z0-9_/]+',
  ))
  ->defaults(array(
    'controller' => 'static',
    'action' => 'index',
  ));
 
/*
 * You don't like slashes?
 *   EditGallery:bahamas
 *   Watch:wakeboarding
 */
Route::set('gallery', '<action>(<controller>):<id>',
  array(
    'controller' => '[A-Z][a-z]++',
    'action'     => '[A-Z][a-z]++',
  ))
  ->defaults(array(
    'controller' => 'Slideshow',
  ));
 
/*
 * Quick search
 */
Route::set('search', ':<query>', array('query' => '.*'))
  ->defaults(array(
    'controller' => 'search',
    'action' => 'index',
  ));
Routes are matched in the order specified so be aware that if you set routes after the modules have been loaded a module could specify a route that conflicts with your own. This is also the reason that the default route is set last, so that custom routes will be tested first.