3.4.5 Configurazione del sistema di Routing

Il Routing é una caratteristica che permette di mappare gli URL in azioni dei controller. é inserita in CakePHP per rendere piú gradevoli, configurabili e flessibili gli URL. Non é necessario usare la funzione mod_rewrite di Apache, anche se sarebbe consigliato per rendere ancora piú gradevole la barra dell'indirizzo.

In CakePHP 1.2 il routing é stato potenziato e ora offre molte caratteristiche aggiuntive.

Prima di iniziare a configurare il routing, devi sapere che CakePHP ha delle impostazioni di default, che funzionano piuttosto bene nella maggior parte delle applicazioni. Puoi accedere a una azione direttamente dall'URL semplicemente scrivendo il suo nome. Puoi anche passarle dei parametri, sempre tramite l'URL.

    Ecco come é composto il meccanismo di routing di default:
    http://example.com/controller/action/param1/param2/param3
  1. Ecco come é composto il meccanismo di routing di default:
  2. http://example.com/controller/action/param1/param2/param3

L'URL /posts/view richiama l'azione view() del controller PostsController, e /products/viewClearance richiama l'azione view_clearance() del controller ProductsController. Se l'URL non specifica una azione, viene richiamata automaticamente l'azione index().

Il setup di default permette anche di passare dei parametri alle tue azioni tramite l'URL. Per esempio, la richiesta di /posts/view/25 equivale al chiamare l'azione view(25) del PostsController.

Novitá: in CakePHP 1.2 si puó anche nominare i parametri. Puoi dare un nome ai parametri e inviare i rispettivi valori tramite URL. Per esempio la richiesta di /posts/view/title:first+post/category:general diviene una chiamata all'azione view() del PostsController. In questa azione puoi accedere ai valori dei parametri title e category tramite la notazione $this->passedArgs['title'] e $this->passedArgs['category'].

Ecco alcuni esempi per spiegare meglio.

URL alla azione del controller, mappata nel modo di default:
    
URL: /monkeys/jump
Richiama: MonkeysController->jump();
 
URL: /products
Richiama: ProductsController->index();
 
URL: /tasks/view/45
Richiama: TasksController->view(45);
 
URL: /donations/view/recent/2001
Richiama: DonationsController->view('recent', '2001');

URL: /contents/view/chapter:models/section:associations
Richiama: ContentsController->view();
$this->passedArgs['chapter'] = 'models';
$this->passedArgs['section'] = 'associations';

Definire le proprie regole di routing permette di controllare come l'applicazione risponderá a determinati URL. Le regole di routing si definiscono nel file /app/config/routes.php utilizzando il metodo Router::connect().

Il metodo connect() richiede tre parametri: l'URL che vuoi mappare, i valori di default per gli elementi personalizzati, e l'espressione regolare che permette di aiutare il router a comprendere quali URL sono validi.

Il formato di base per definire una regola di routing é:

Router::connect(
    'URL',
    array('nomeParametro' => 'valorePredefinito'),
    array('nomeParametro' => 'espressioneRegolare')
)
  1. Router::connect(
  2. 'URL',
  3. array('nomeParametro' => 'valorePredefinito'),
  4. array('nomeParametro' => 'espressioneRegolare')
  5. )

Il primo parametro &greave; utilizzato per dire al router che tipo di URL deve controllare. L'URL é una normale stringa delimitata da slash, ma puó contenere anche un carattere qualsiasi (*) un un elemento personalizzato (gli elementi sono contraddistinti dall'uso dei due punti nell'URL). Quando usi un nome indichi al router a che tipo di URL fa riferimento, e specificando degli elementi puoi settare dei parametri dell'azione del controller.

Una volta specificato l'URL, si utilizzano gli ultimi due parametri del metodo connect() per indicare a CakePHP cosa fare quando la richiesta soddisfa la regola impostata. Il secondo parametro é un array associativo. Le chiavi dell'array possono essere richiamate dopo il primo elemento dell'URL, oppure puoi usare gli elementi di default: :controller, :action e :plugin. I valori delle chiavi sono i valori che imposti di default. Ecco qualche esempio su come si usa il secondo parametro del metodo connect().

Router::connect(
    '/pages/*',
    array('controller' => 'pages', 'action' => 'display')
);
  1. Router::connect(
  2. '/pages/*',
  3. array('controller' => 'pages', 'action' => 'display')
  4. );

Questa regola si trova alla linea 40 del file routes.php. Indica che tutti gli URL che iniziano con /pages/ richiamano la funzione display() del controller PagesController(); per esempio, la richiesta /pages/products richiama PagesController->display('products').

Router::connect(
    '/government',
    array('controller' => 'products', 'action' => 'display', 5)
);
  1. Router::connect(
  2. '/government',
  3. array('controller' => 'products', 'action' => 'display', 5)
  4. );

In questo secondo esempio si vede come puoi usare il secondo parametro di connect() per definire dei valori di default. Se stai costruendo una sito che reggruppa i prodotti in diverse categorie di clienti, puoi considerare di creare una regola di routing. Questo ti permette di usare /government al posto di /products/display/5.

Per aggiungere flessibilitá puoi specificare degli elementi personalizzati. In questo modo puoi definire in che posizione dell'URL devono trovarsi determinati parametri. Quando viene effettuata una richiesta, i valori di ciascun elemento sono disponibili in $this->params all'interno del controller. Questo é un modo diverso da come sono trattati i parametri con un nome spiegati prima, infatti nota la differenza: i parametri con un nome (/controller/action/name:value) si rirovano in $this->passedArgs, mentre i valori degli elementi personalizzati delle regole di routing si trovano in $this->params. Quando definisci un elemento di routing personalizzato, puoi anche identificarlo con una espressione regolare, che indichi a CakePHP come riconoscere se l'URL é valido o no.

Router::connect(
    '/:controller/:id',
    array('action' => 'view'),
    array('id' => '[0-9]+')
);
  1. Router::connect(
  2. '/:controller/:id',
  3. array('action' => 'view'),
  4. array('id' => '[0-9]+')
  5. );

Questo semplice esempio illustra come creare un modo rapido per visualizzare i modelli da tutti i controller, tramite un elegante URL come /controllername/id. L'URL descritto con connect() specifica due elementi: :controller e :id. L'elemento :controller é un elemento di default di CakePHP, quindi il router sa come funziona e quindi é in grado di identificare il nome del controller nell'URL. L'elemento :id é un elemento personalizzato, e deve essere definito tramite l'espressione regolare che si trova nel terzo parametro. In questo modo si indica a CakePHP come riconoscere l'ID nell'URL, piuttosto che qualcos'altro, come per esempio il nome di un'azione.

Quando hai definito questa regola, richiedendo /apples/5 ottieni lo stesso che richiedendo /apples/view/5. Ambedue le richieste richiamano il metodo view() del controller ApplesController. All'interno del metodo view(), puoi accedere al valore di ID tramite $this->params['id'].

Un altro esempio, da vero professionista.

Router::connect(
    '/:controller/:year/:month/:day',
    array('action' => 'index', 'day' => null),
    array(
        'year' => '[12][0-9]{3}',
        'month' => '(0[1-9]|1[012])',
        'day' => '(0[1-9]|[12][0-9]|3[01])'
    )
);
  1. Router::connect(
  2. '/:controller/:year/:month/:day',
  3. array('action' => 'index', 'day' => null),
  4. array(
  5. 'year' => '[12][0-9]{3}',
  6. 'month' => '(0[1-9]|1[012])',
  7. 'day' => '(0[1-9]|[12][0-9]|3[01])'
  8. )
  9. );

Stavolta é piuttosto complesso, ma questo indica quanto potente puó essere il sistema di routing. L'URL in questione ha quattro elementi. Il primo é ormai noto: é l'elemento standard che indica a CakePHP il nome del controller.

Poi specifichiamo alcuni valori. Indipendentemente dal controller, vogliamo richiamare l'azione index(). Impostiamo il parametro day (il quarto elemento nell'URL) a null per indicare che é opzionale.

Infine specifichiamo l'espressione regolare che identifica l'anno, i mesi e i giorni in formato numerico.

Alla fine questa regola valida richieste come /articles/2007/02/01, /posts/2004/11/16, e /products/2001/05 (il parametro day é opzionale, ricordi?), richiamando l'azione index() nei rispettivi controllers, con i parametri personalizzati della data, disponibili in $this->params.

3.4.5.1 Default Routing

Before you learn about configuring your own routes, you should know that CakePHP comes configured with a default set of routes. CakePHP’s default routing will get you pretty far in any application. You can access an action directly via the URL by putting its name in the request. You can also pass parameters to your controller actions using the URL.

    URL pattern default routes: 
    http://example.com/controller/action/param1/param2/param3

The URL /posts/view maps to the view() action of the PostsController, and /products/viewClearance maps to the view_clearance() action of the ProductsController. If no action is specified in the URL, the index() method is assumed.

The default routing setup also allows you to pass parameters to your actions using the URL. A request for /posts/view/25 would be equivalent to calling view(25) on the PostsController, for example.

3.4.5.2 Named parameters

New in CakePHP 1.2 is the ability to use named parameters. You can name parameters and send their values using the URL. A request for /posts/view/title:first+post/category:general would result in a call to the view() action of the PostsController. In that action, you’d find the values of the title and category parameters inside $this->passedArgs[‘title’] and $this->passedArgs[‘category’] respectively.

Some summarizing examples for default routes might prove helpful.

URL to controller action mapping using default routes:  
    
URL: /monkeys/jump
Mapping: MonkeysController->jump();
 
URL: /products
Mapping: ProductsController->index();
 
URL: /tasks/view/45
Mapping: TasksController->view(45);
 
URL: /donations/view/recent/2001
Mapping: DonationsController->view('recent', '2001');

URL: /contents/view/chapter:models/section:associations
Mapping: ContentsController->view();
$this->passedArgs['chapter'] = 'models';
$this->passedArgs['section'] = 'associations';

3.4.5.3 Defining Routes

Defining your own routes allows you to define how your application will respond to a given URL. Define your own routes in the /app/config/routes.php file using the Router::connect() method.

The connect() method takes up to three parameters: the URL you wish to match, the default values for custom route elements, and regular expression rules to help the router match elements in the URL.

The basic format for a route definition is:

Router::connect(
    'URL',
    array('paramName' => 'defaultValue'),
    array('paramName' => 'matchingRegex')
)
  1. Router::connect(
  2. 'URL',
  3. array('paramName' => 'defaultValue'),
  4. array('paramName' => 'matchingRegex')
  5. )

The first parameter is used to tell the router what sort of URL you’re trying to control. The URL is a normal slash delimited string, but can also contain a wildcard (*) or custom route elements (URL elements prefixed with a colon). Using a wildcard tells the router what sorts of URLs you want to match, and specifying route elements allows you to gather parameters for your controller actions.

Once you’ve specified a URL, you use the last two parameters of connect() to tell CakePHP what to do with a request once it has been matched. The second parameter is an associative array. The keys of the array should be named after the route elements in the URL, or the default elements: :controller, :action, and :plugin. The values in the array are the default values for those keys. Let’s look at some basic examples before we start using the third parameter of connect().

Router::connect(
    '/pages/*',
    array('controller' => 'pages', 'action' => 'display')
);
  1. Router::connect(
  2. '/pages/*',
  3. array('controller' => 'pages', 'action' => 'display')
  4. );

This route is found in the routes.php file distributed with CakePHP (line 40). This route matches any URL starting with /pages/ and hands it to the display() method of the PagesController(); The request /pages/products would be mapped to PagesController->display(‘products’), for example.

Router::connect(
    '/government',
    array('controller' => 'products', 'action' => 'display', 5)
);
  1. Router::connect(
  2. '/government',
  3. array('controller' => 'products', 'action' => 'display', 5)
  4. );

This second example shows how you can use the second parameter of connect() to define default parameters. If you built a site that features products for different categories of customers, you might consider creating a route. This allows you link to /government rather than /products/display/5.

For additional flexibility, you can specify custom route elements. Doing so gives you the power to define places in the URL where parameters for controller actions should lie. When a request is made, the values for these custom route elements are found in $this->params of the controller. This is different than named parameters are handled, so note the difference: named parameters (/controller/action/name:value) are found in $this->passedArgs, whereas custom route element data is found in $this->params. When you define a custom route element, you also need to specify a regular expression - this tells CakePHP how to know if the URL is correctly formed or not.

Router::connect(
    '/:controller/:id',
    array('action' => 'view'),
    array('id' => '[0-9]+')
);
  1. Router::connect(
  2. '/:controller/:id',
  3. array('action' => 'view'),
  4. array('id' => '[0-9]+')
  5. );

This simple example illustrates how to create a quick way to view models from any controller by crafting a URL that looks like /controllername/id. The URL provided to connect() specifies two route elements: :controller and :id. The :controller element is a CakePHP default route element, so the router knows how to match and identify controller names in URLs. The :id element is a custom route element, and must be further clarified by specifying a matching regular expression in the third parameter of connect(). This tells CakePHP how to recognize the ID in the URL as opposed to something else, such as an action name.

Once this route has been defined, requesting /apples/5 is the same as requesting /apples/view/5. Both would call the view() method of the ApplesController. Inside the view() method, you would need to access the passed ID at $this->params[‘id’].

One more example, and you’ll be a routing pro.

Router::connect(
    '/:controller/:year/:month/:day',
    array('action' => 'index', 'day' => null),
    array(
        'year' => '[12][0-9]{3}',
        'month' => '(0[1-9]|1[012])',
        'day' => '(0[1-9]|[12][0-9]|3[01])'
    )
);
  1. Router::connect(
  2. '/:controller/:year/:month/:day',
  3. array('action' => 'index', 'day' => null),
  4. array(
  5. 'year' => '[12][0-9]{3}',
  6. 'month' => '(0[1-9]|1[012])',
  7. 'day' => '(0[1-9]|[12][0-9]|3[01])'
  8. )
  9. );

This is rather involved, but shows how powerful routes can really become. The URL supplied has four route elements. The first is familiar to us: it’s a default route element that tells CakePHP to expect a controller name.

Next, we specify some default values. Regardless of the controller, we want the index() action to be called. We set the day parameter (the fourth element in the URL) to null to flag it as being optional.

Finally, we specify some regular expressions that will match years, months and days in numerical form.

Once defined, this route will match /articles/2007/02/01, /posts/2004/11/16, and /products/2001/05 (remember that the day parameter is optional?), handing the requests to the index() actions of their respective controllers, with the custom date parameters in $this->params.

3.4.5.4 Passing parameters to action

Assuming your action was defined like this and you want to access the arguments using $articleID instead of $this->params['id'], just add an extra array in the 3rd parameter of Router::connect().

// some_controller.php
function view($articleID = null, $slug = null) {
    // some code here...
}

// routes.php
Router::connect(
    // E.g. /blog/3-CakePHP_Rocks
    '/blog/:id-:slug',
    array('controller' => 'blog', 'action' => 'view'),
    array(
        // order matters since this will simply map ":id" to $articleID in your action
        'pass' => array('id', 'slug'),
        'id' => '[0-9]+'
    )
)
  1. // some_controller.php
  2. function view($articleID = null, $slug = null) {
  3. // some code here...
  4. }
  5. // routes.php
  6. Router::connect(
  7. // E.g. /blog/3-CakePHP_Rocks
  8. '/blog/:id-:slug',
  9. array('controller' => 'blog', 'action' => 'view'),
  10. array(
  11. // order matters since this will simply map ":id" to $articleID in your action
  12. 'pass' => array('id', 'slug'),
  13. 'id' => '[0-9]+'
  14. )
  15. )

And now, thanks to the reverse routing capabilities, you can pass in the url array like below and Cake will know how to form the URL as defined in the routes.

// view.ctp
// this will return a link to /blog/3-CakePHP_Rocks
<?= $html->link('CakePHP Rocks', array(
    'controller' => 'blog',
    'action' => 'view',
    'id' => 3,
    'slug' => Inflector::slug('CakePHP Rocks')
)) ?>
  1. // view.ctp
  2. // this will return a link to /blog/3-CakePHP_Rocks
  3. <?= $html->link('CakePHP Rocks', array(
  4. 'controller' => 'blog',
  5. 'action' => 'view',
  6. 'id' => 3,
  7. 'slug' => Inflector::slug('CakePHP Rocks')
  8. )) ?>

3.4.5.5 Prefix Routing

Many applications require an administration section where priveledged users can make changes. This is often done through a special URL such as /admin/users/edit/5. In CakePHP, admin routing can be enabled from within the core configuration file by setting the admin path for Routing.admin.

Configure::write('Routing.admin', 'admin');
  1. Configure::write('Routing.admin', 'admin');

In your controller, any action with an admin_ prefix will be called. Using our users example, the method name of our UsersController would be admin_edit.

You can use the Router to use custom prefixes for uses beyond admin routing, too.

Router::connect('/profiles/:controller/:action', array('prefix' => 'profiles', 'profiles' => true)); 
  1. Router::connect('/profiles/:controller/:action', array('prefix' => 'profiles', 'profiles' => true));

Any calls to the profiles section would look for the profiles_ prefix on the method calls. Our users example would have a URL structure that looks like /profiles/users/edit/5 would call the profiles_edit method within the UsersController. Also important to remember, using the HTML helper to build your links will help maintain the prefix calls. Here's an example link being built using the HTML helper:

echo $html->link('Edit your profile', array('controller' => 'users', 'action' => 'profiles_edit', 'profiles' => true)); 
  1. echo $html->link('Edit your profile', array('controller' => 'users', 'action' => 'profiles_edit', 'profiles' => true));

You can set up multiple prefixed routes using this approach to create a flexible URL structure for your application.