In the recipe Adding catch-all routes for profile pages we created routes so that profile pages can be accessed specifying only the user name in the URL. However, that implementation had a problem: we had to disallow the automatic access of the index
action.
This recipe shows a different approach to our profile URL generation, by creating a custom route implementation that not only overcomes this problem, but makes sure the route is utilized only for existing profile records.
We need some sample data to work with. Follow the Getting ready section of the recipe Adding catch-all routes for profile pages.
1. Edit your
app/config/routes.php
file and add the following routes at the end of the file:App::import('Lib', 'ProfileRoute'); Router::connect('/:userName', array('controller' => 'profiles', 'action' => 'view'), array( 'routeClass' => 'ProfileRoute', 'pass' => array('userName') ) );
2. Now create a file named
profile_route...