Sunday, November 23, 2008

Understanding Cake Routes

Cake Routes

What is a Route? Put simply, a route is a path from one point to another. In the world of networking, routes are built in routers to tell packets where to go to get to their destination. In the same way routing in Cake PHP is a set of rules that help a web browser display the right web page.

How do Cake Routes Work? Without exploring the 1000+ lines of code in the routes.php class, routes work by parsing the URL of the web page that you just requested. Your web browser sends that URL to the CakePHP Application. The Cake PHP application will then load your list of routes that you've defined.

Routes work by going Top-Down and looking for the first / best matching path. When it finds a match, the router ignores any additional routes that exist and it returns the routing information that decides which part of your application to load.

Why should we use routes? Let's say you have a giant application with tons of features, addons and tables. Now you need to load a single page. How does your application know just what files to load for this page without loading the entire application into memory? You could have an index.php file that contains a giant switch case with all the possible combinations of actions you have available in your system, however management of this becomes a nightmare. How will you determine just which classes and which files to load in a neat, managable, and expandable way?

Cake Routing is the answer. A Cake route connects a url path to a MVC Controller. That's it. Complete management in a few single lines of code. When the connection to the route is made, it will only ever load 1 controller. That controller decides which models (database tables) that it will be accessing along with which templates and layouts will be displayed. You never load anything that you don't need in that specific controller, which cuts down on memory consumption and helps the environment. Well not the environment so much, but it does cut down on needless hair loss.

Let's look at some examples:

Router::connect('/', array('controller' => 'pages', 'action' => 'display', 'home'));

The first route above does the following:
If the first thing I find after the URL is simply a "/" (which is usually always the home page) then we want to open up the pages_controller.php file in the app/controllers/ directory, and then look for a function called "display". Because "home" is stuck at the end of the array, it will pass the word "home" to that controller.

If it does not find /app/controllers/pages_controller.php, it will look in the cake core for the pages_controller (which is in cake/console/libs/templates/skel/controllers). If it can not find it there, cake will display an error.

Cake Routes also allow wildcards. This means that it will match everything. Here is an example:

Router::connect('/pages/*', array('controller' => 'pages', 'action' => 'display'));

This route does the following:
Any time there is a /pages/ after the URL, this route will kick in. It expects to have something after the pages/, and that something can be anything. For instance, if you had (http://url.com/pages/this-is-my-favorite-page), this route will kick in and it will send you to the pages controller, using the function display().
Once you get the hang of routes, there is quite a bit you can do with them. They can be customized in many ways.

Tips
  • Remember that routes work from top to bottom. Place your most exact matching routes at the top, then less exact routes farther down.
  • You can test routes by creating a file called "app/app_controller.php". Inside of it add the following lines:
  1. function __construct() {
  2. $route = Router::currentRoute();
  3. pr($route);
  4. parent::__construct();
  5. }
With this, you can test different URLs, and this code will print out the route that it the URL matches.

Note: the pr() should be commented out when you are not testing routes.
  • You can loop Route::connect commands in a foreach() statement to shorten the amount of code that you have. This helps for large administration pages.
Links:

You can read more about routes here.

Example routes can be found here.

No comments: