Reputation: 504
I need to rewrite URL in my cake application in such a way that only slug (e.g. title) is shown in the URL. For example, if I have a cake URL like www.site.com/users/view/1 (controller/action/ID), I need to show only /vikram.sinha if the user's nick is vikram.sinha (no ID, no action, no controller or anything else in the URL).
I am making sure that the title (slug) is unique before storing and the best I could do is the following.
www.site.com/profile/vikram.sinha by adding the route below.
Router::connect('/profile/:slug', array('controller' => 'users', 'action' => 'view'),
array(
'pass' => array('slug'),
));
The problem I am facing is I also need to remove profile from the URL. Couldn't find a way to rewrite URL in cake without adding something before slug.
Or may be the URL can be rewritten using htaccess directly but I am not good with that either. If someone is suggesting using htaccess then please assume that the URL need to be changed is www.site.com/users/view/vikram.sinha
Thanks!
Upvotes: 0
Views: 245
Reputation: 29121
Your best bet IMO is to do something like reddit where each slug is prefixed with /r/.
Example:
mysite.com/u/johnsmith
Then, in your routes, you can do something like this:
Router::connect('/u/:slug/*', array('controller'=>'users', 'action'=>'view'),
array('pass'=>array('slug')));
If you really don't want to do that, you can use something like:
Router::connect('/:slug/*', array('controller'=>'users', 'action'=>'view'),
array('pass'=>array('slug')));
But keep in mind, any other controllers, plugins...etc etc etc will need their own routes, since it will think that ANYTHING passed after the mysite.com/ is a uname. Not the best idea IMO.
Upvotes: 1