4.4. Advanced Routing Configuration: Admin Routing and Webservices

There are some settings in /app/config/core.php you can take advantage of in order to organize your application and craft URLs that make the most sense to you and your users.

The first of these is admin routing. If your application has a ProductsController as well as a NewsController, you might want to set up some special URLs so users with administrative privileges can access special actions in those controllers. To keep the URLs nice and easy to read, some people prefer /admin/products/add and /admin/news/post to something like /products/adminAdd and /news/adminPost.

To enable this, first, uncomment the CAKE_ADMIN line in your /app/config/core.php file. The default value of CAKE_ADMIN is 'admin', but you can change it to whatever you like. Remember this string, because you'll need to prepend it to your administrative actions in your controller. So, admin actions in this case would be named admin_actionName(). Here's some examples of desired URLs and possible CAKE_ADMIN and controller action settings:

/admin/products/add          CAKE_ADMIN = 'admin'
                             name of action in ProductsController = 'admin_add()'

/superuser/news/post         CAKE_ADMIN = 'superuser'
                             name of action in NewsController = 'superuser_post()'

/admin/posts/delete          CAKE_ADMIN = 'admin'
                             name of action in PostsController = 'admin_delete()'
 

Using admin routes allows you to keep your logic organized while making the routing very easy to accomplish. When enabled, you can easily determine in the controller whether an admin route has been accessed by using:

$this->params[CAKE_ADMIN];

or

$this->params['admin'];

(assuming 'admin' is the value of CAKE_ADMIN).

[Note] Note

Please note that enabling admin routes or using them does not enable any sort of authentication or security. You'll need implement those yourself.

Similarly, you can enable Cake's webservices routing to make easier there as well. Have a controller action you'd like to expose as a webservice? First, set WEBSERVICES in /app/config/core.php to 'on'. This enables some automatic routing somewhat similar to admin routing, except that a certain set of route prefixes are enabled:

What this does is allows you to provide an alternate views that will automatically be available at /rss/controllerName/actionName or /soap/controllerName/actionName. This allows you to create a single action that can have two views: one for normal HTML viewiers, and another for webservices users. By doing this, you can easily allow much of the functionality of your application to be available via webservices.

For example, let's say I have some logic in my application that tells users who is on the phone in my office. I already have a HTML view for this data, but I want to offer it in XML so it can be used in a desktop widget or handheld application. First I need to enable Cake's webservice routing:

Example 4.6. /app/config/core.php (partial)

/**
 *  The define below is used to turn cake built webservices
 *  on or off. Default setting is off.
 */
	define('WEBSERVICES', 'on');

Next, I can structure the logic in my controller just as I normally would:

Example 4.7. messages_controller.php

<?php
class PhonesController extends AppController
{
    function doWhosOnline()
    {
        // this action is where we do all the work of seeing who's on the phone...

        // If I wanted this action to be available via Cake's xml webservices route,
        // I'd need to include a view at /app/views/posts/xml/do_whos_online.thtml.
        // Note: the default view used here is at /app/views/layouts/xml/default.thtml.

        // If a user requests /phones/doWhosOnline, they will get an HTML version.
        // If a user requests /xml/phones/doWhosOnline, they will get the XML version.
    } 
}
?>