Cakephp | URL routing using prefix after domain name

| By Webner

Routes in an application are configured in app/Config/routes.php. Defining your own routes allows you to define where your application will take the control to for a given URL.

Default routing is like this:


Prefix routing will add a prefix (aryan below) in the middle of the URL:

Prefix routing: domain/prefix/controller/action


For achieving prefix routing put following code lines in your app/Config/routes.php file:

/* Get URL Separate in array Remove empty elements */
$url = array_filter(explode("/", Router::url()));
$trail = $url;

/* Get Controllers list */
$Controllers = App::objects('controller');
$cleanControllers = array();

/* Remove string 'Controller' from element lower string */
foreach($Controllers as $Controller)
    $cleanControllers[] = strtolower(str_replace('Controller', '', $Controller));

/* Check if first element, from URL, is NOT a controllers array */
if(!in_array(strtolower(reset($url)), $cleanControllers))
    if (isset($trail[1]) && $trail[1]!='')
        // write your own code here as needed
        if ($trail[2]=='login')
            Router::connect('/*', array('controller' => 'user', 'action' => 'login'));
        elseif ($trail[2]=='home')
            Router::connect('/*', array('controller' => 'user', 'action' => 'home'));

require CAKE . 'Config' . DS . 'routes.php';

Webner Solutions is a Software Development company focused on developing CRM apps (Salesforce, Zoho), LMS Apps (Moodle/Totara), Websites and Mobile apps. If you need Web development or any other software development assistance please contact us at

Leave a Reply

Your email address will not be published. Required fields are marked *