CakePHP Routing: Changing pretty URLs site-wide - cakephp

Let's say I want a page to have a pretty URL:
Config/routes.php
Router::connect('/profile', array('controller' => 'users', 'action' => 'profile'));
If I want to send visitors to that page, I can use the URL like:
$this->redirect('/profile');
$this->Html->link('Your Profile', '/profile');
But let's say I change my mind and I now want the URL to be:
/account
How can I change the entire site without changing every instance of /profile to /account?
Alternatively...
Another way to ask my question would be how can I properly code all URLs using Cake array syntax (which I prefer to do rather than hardcode anything):
$this->redirect(array('controller' => 'users', 'action' => 'profile'));
$this->Html->link('Your Profile', array('controller' => 'users', 'action' => 'profile'));
And then ensure that any time that controller/action combination is called, it sends people to the URL:
/profile
And have this rule in one place that can be changed. Something like:
Router::connect(array('controller' => 'users', 'action' => 'profile'), '/profile');
// Later change to
Router::connect(array('controller' => 'users', 'action' => 'profile'), '/account');
Is there any way to do that, and also allow further request parameters to be passed along to be added to the URL?

Have a look at the routing documentation: http://book.cakephp.org/2.0/en/development/routing.html
In your app/routes.php add:
Router::connect('/profile', array('controller' => 'users', 'action' => 'profile'));
Now you can create your links like this:
echo $this->Html->link('Link to profile', array('controller' => 'users', 'action' => 'profile'));
Or if you want to allow additional parameters:
// When somebody comes along without parameters ...
Router::connect('/profile', array('controller' => 'users', 'action' => 'profile'));
// When somebody parses parameters
Router::connect('/profile/*', array('controller' => 'users', 'action' => 'profile'));
And then you will be able to do something like:
$userId = 12;
echo $this->Html->link('Link to other profile', array('controller' => 'users', 'action' => 'profile', $userId));
The $userId will then be available in the controller via:
echo $this->request->params['pass'][0];
// output: 12
This way you can easily change the url's of your website without having to change every single view/redirect or what so ever. Please bare in mind that you should not change your controller names! Because that will mess up a lot. Choose wisely ;-)
http://book.cakephp.org/2.0/en/development/routing.html
http://book.cakephp.org/2.0/en/core-libraries/helpers/html.html

Related

CakePHP routing to controller and page

Is it possible to identify a controller and pagename in the same url
Router::connect('/:controller/');
Router::connect('/:pagename/', array('controller' => 'home','action'=>'index'));
www.example.com/controller
so that the controller goes to the :controller/index
www.example.com/pagename
so that the page goes to home/index
Its really confusing what really you wants. If a url ends with :controller like www.example.com/posts its generally map index action. Now if the url ends with pagename, means action, like www.example.com/mypage, you can map that as-
Router::connect('/mypage', array('controller' => 'homes', 'action' => 'index'));
So when a user browse www.example.com/mypage, it will map to HomesController and index action.
Try following code for pagename:
Router::connect('/:slug', array('controller' => 'home', 'action' => 'index'));
UPDATE 1
If you want to treat www.example.com/user as same as www.example.com/users then you need to add following, because CakePHP controller name is plural as per CakePHP naming convention:
Router::connect('/user/:action/*', array('controller' => 'users'));
UPDATE 2
Router::connect(':slug') overwrite all of default routing. So you need to use custom routing class as like as :
App::uses('SlugRoute', 'Routing/Route');
Router::connect(
'/:slug',
array('controller' => 'home', 'action' => 'index'),
array('routeClass' => 'SlugRoute')
);

cakephp url rewriting not working properly in the router file

I am facing some problem with rewriting cakephp urls
Here is my url:
domain.com/users/members/mygroup
domain.com/users/admins/mygroup
I want to rewrite this to
domain.com/mygroup/members
domain.com/mygroup/admins
I have tried the following code but it's not working
In the routes.php i have created the following routes
Router::connect('/:groupname/:members', array('controller' => 'users', 'action' => 'members'),array('pass' => array('groupname')));
Router::connect('/:groupname/:admins', array('controller' => 'users', 'action' => 'admins'),array('pass' => array('groupname')));
Here is the links:
<?php echo $this->html->link('Members',array('controller'=>'users','action'=>'members','groupname'=>$groupdata['Group']['group_slug'],'members'=>members),array('escape'=>false,'class'=>'links','id'=>'memlink'));?>
<?php echo $this->html->link('Admin',array('controller'=>'users','action'=>'admins','groupname'=>$groupdata['Group']['group_slug'],'admins'=>admins),array('escape'=>false,'class'=>'links','id'=>'admnlink'));?>
When i create routes like this the first routing i.e member routing is working fine, but the second routing admin is not working, it's picking the members action and executing the members method but the url appears correct, only the action is wrong.
How can i resolve this.
Try this.
Router::connect('/:groupname/:members', array('controller' => 'users', 'action' => 'members'),array('pass' => array('groupname', 'members')));
Router::connect('/:groupname/:admins', array('controller' => 'users', 'action' => 'admins'),array('pass' => array('groupname', 'admins')));
Update
Router::connect('/:groupname/:action', array('controller' => 'users'),array('pass' => array('groupname')));
Router::connect('/:groupname/:action', array('controller' => 'users'),array('pass' => array('groupname')));
working fine for me, put the code like this,
echo $this->html->link('Members',array(
'controller'=>'users','action'=>'members',
'groupname'=> 'mygroup'), array(
'escape'=>false,'class'=>'links','id'=>'memlink'
));
echo $this->html->link('Admin',array(
'controller'=>'users','action'=>'admins',
'groupname'=> 'mygroup'),array(
'escape'=>false,
'class'=>'links','id'=>'admnlink'
));

Routing all but certain parameters in CakePHP

I'm trying to set up a routing definition in my project which will allow users to have profiles accessible by simply using their username as the only parameter in the url, like www.example.com/username as opposed to www.example.com/user/view/username
I've set up a catch all route to go to an action which checks if the user exists, as the last route in the config file, and it works, but it over rides all of the basic routing that cake does. Meaning that I would have to define a route for every controller I want to provide access to just to make sure I never make it to the catchall. My routes:
Router::connect('/events/edit/*', array('controller' => 'events', 'action' => 'edit'));
Router::connect('/events/view/*', array('controller' => 'events', 'action' => 'view'));
Router::connect('/events/add', array('controller' => 'events', 'action' => 'add'));
Router::connect('/events/*', array('controller' => 'events', 'action' => 'index'));
Router::connect('/*', array('controller' => 'users', 'action' => 'view'));
So, this will allow me to access my events page, but any other pages get sent to the second router, expectedly.
What I'd like is to have is a route that does the basic cake function of /controller/action/param if the controller exists, but have it fall through to the last route which goes to the user/view page otherwise.
My question is, for what I'm trying to accomplish, am I doing this the right way? If I need to define a route for every controller I want access to, I will, but I have to think there's a better way to accomplish this.
Thanks
According to the my understanding of your question, I think You can proceed like this.
App::uses('UserRoute','Lib');
Router::connect('/:user_name', array('controller' => 'users', 'action' => 'view'),
array('routeClass'=>'UserRoute', 'pass' => array('user_name')));
and in your app/lib create a file UserRoute.php like this
<?php
App::uses('Lib', 'CakeRoute');
class UserRoute extends CakeRoute {
function parse($url) {
$params = parent::parse($url);
if (empty($params)) {
return false;
}
App::import('Model', 'User');
$User = new User();
$user_count = $User->find('count',array(
'conditions' => array('User.username' => $params['user_name']),
'recursive' => -1
));
if ($user_count) {
return $params;
}
return false;
}
}
Hope this will help you..

CakePHP - How to make routes with custom parameters?

My Cake URL is like this:
$token = '9KJHF8k104ZX43';
$url = array(
'controller' => 'users',
'action' => 'password_reset',
'prefix' => 'admin',
'admin' => true,
$token
)
I would like this to route to a prettier URL like:
/admin/password-reset/9KJHF8k104ZX43
However, I would like the token at the end to be optional, so that in the event that someone doesn't provide a token it is still routed to:
/admin/password-reset
So that I can catch this case and redirect to another page or display a message.
I've read the book on routing a lot and I still don't feel like it explains the complex cases properly in a way that I fully understand, so I don't really know where to go with this. Something like:
Router::connect('/admin/password-reset/:token', array('controller' => 'users', 'action' => 'password_reset', 'prefix' => 'admin', 'admin' => true));
I don't really know how to optionally catch the token and pass it to the URL.
You'll want to use named parameters. For an example from one of my projects
Router::connect('/:type/:slug',
array('controller' => 'catalogs', 'action' => 'view'),
array(
'type' => '(type|compare)', // regex to match correct tokens
'slug' => '[a-z0-9-]+', // regex again to ensure a valid slug or 404
'pass' => array(
'slug', // I just want to pass through slug to my controller
)
));
Then, in my view I can make a link which will pass the slug through.
echo $this->Html->link('My Link', array('controller' => 'catalogs', 'action' => 'view', 'type' => $catalog['CatalogType']['slug'], 'slug' => $catalog['Catalog']['slug']));
My controller action looks like this,
public function view($slug) {
// etc
}

How to redirect in CakePHP with # in url, and not %23?

I am using CakePHP and I am doing this:
$this->redirect(array('controller' => 'users', 'action' => 'view',$id));
output in the browser: .../users/view/42
Since I am using JQueryUI tabs, I want the user to be redirected to the tab he just edited, so it should looks something like :
$this->redirect(array('controller' => 'users', 'action' => 'view',$id."#groups"));
output in the browser: .../users/view/42%23groups
But expected result: .../users/view/42#groups
Q: How to send a correct url with #id in it to send the focus ?
I want, if possible, not use a custom GET param that echo a js in the view.ctp to get the focus asked.
I hope it is a CakePHP issue and don't need to change some .htaccess like (How to rewrite a URL with %23 in it?)
Please read the documentation
http://book.cakephp.org/2.0/en/core-libraries/helpers/html.html#HtmlHelper::url
$this->redirect(array(
'controller' => 'users', 'action' => 'view', $id, '#' => 'groups'));
This is how I achieved it:
// Redirect to the home page.
$this->redirect(array(
'controller' => 'cars',
'action' => 'index',
'#' => 'thankyou'
));

Resources