>
Custom Url in CakePhp
Welcome, Guest

by nav on Tuesday, 22 September 2009
Custom Url in CakePhp


Hi,

First off, we need to create the route for our custom urls. The route you would need to use is as follows:

<?php
// cake/app/config/routes.php

// Default route
$Route->connect('/', array('controller' => 'pages''action' => 'display''home'));

// Default pages route
$Route->connect('/pages/*', array('controller' => 'pages''action' => 'display'));

// Custom URL route
$Route->connect('/*', array('controller' => 'users''action' => 'view'));

?>

As you can see, the two default routes come first with our new route coming last.

We're not finished here yet. Since we now have this catch all in place, we need to ensure that all our controllers are treated correctly. To do so for our controllers, we add the following to routes.php:

<?php
// cake/app/config/routes.php

// Default route
$Route->connect('/', array('controller' => 'pages''action' => 'display''home'));

// Set default controller routes
$Route->connect('/users/:action/*', array('controller' => 'users'));
$Route->connect('/items/:action/*', array('controller' => 'items'));
$Route->connect('/images/:action/*', array('controller' => 'images'));

// Default pages route
$Route->connect('/pages/*', array('controller' => 'pages''action' => 'display'));

// Custom URL route
$Route->connect('/*', array('controller' => 'users''action' => 'view'));

?>

Because of our new route, anything not matching the base path route or the pages route will be routed through the view action of the users controller. We need to ensure we grab URLs that should maintain Cake's default routing.

The final step in this process would be to do some error handling. Since we're using our catch all, anything that doesn't exist is still going to try to route to our users controller. A quick bit of work on the view function of our controller will redirect the user if an invalid URL is given:

function view($username)
{
    if ($user = $this->User->findByUsername($username)) {
        $this->set('user', $user);
    } else {
        $this->redirect('/pages/error');
        exit();
    }
}

 

 

Best Regards

Naveen Butola



Blogger Image
My Blog Title

Product Engineering, software engineering company, Product Development, Product Migration, Product Re-engineering, Product Maintenance, Product Testing Commercial Application Development, Business Software development, commercial software for startups, Application Support and Maintenance, software testing Product Maintenance, Outsource product maintenance, product support and maintenance Product Migration, Product Re-engineering, product re-engineering services Product Research, Product Engineering, UI Prototyping Services Software Testing Services, Quality Assurance services, professional software testers, Load Testing, Functional Testing, Cross Platform, Browser Testing, Test Automation, Testing Tools, software quality analysis Functional Testing Services, software quality analysis, Software Testing Services, Application Testing Services, Functional Testing Types Automated Testing, Automated Testing Services, automation testing, test script development, Automation Test Tools, outsource automation testing Load Testing, Performance Testing Services, Load Testing Tools Offshore Software Development, Outsource software services, offshore outsourcing services, offshore software development services, IT outsourcing services, software quality assurance services, Offshore IT services, Custom Application Development Services, Offshore Product Engineering Benefits of IT Outsourcing, Offshore Software Development companies, offshore software development firms Outsource planning, IT outsourcing, IT development services, offshore IT companies, offshore software development Offshore Software Development, Outsource software services, offshore outsourcing services, offshore software development services, IT outsourcing services, software quality assurance services, Offshore IT services, Custom Application Development Services, Offshore Product Engineering Offshore Software Development, Outsource software services, offshore outsourcing services, offshore software development services, IT outsourcing services, software quality assurance services, Offshore IT services, Custom Application Development Services, Offshore Product Engineering