Cakephp 2.4.2 : Route URL - cakephp

I have to do route for following urls. I have gone through book.cakephp.org/2.0/en/development/routing.html and googled it and but didn't find any solution for me.
http://example.com/Homes/Browse/12
http://example.com/Homes/Browse/13
http://example.com/Homes/Browse/14
http://example.com/Homes/Browse/15
http://example.com/Homes/Browse/16
http://example.com/Homes/Browse/17
http://example.com/Homes/Browse/18
and so on..
I want the output like this for all urls mentioned above.
OUTPUT: http://example.com/Homes
I tried these both codes but doesn't work for me.
Router::connect('/Homes/*', array('controller' => 'Homes', 'action' => 'Browse'));
Router::connect('/Homes/*', array('controller' => 'Homes', 'action' => 'Browse','[0-9]+'));

Try this
Router::connect('/Homes', array('controller' => 'Homes', 'action' => 'Browse'));

Related

How to remove controller and action name from URL in CakePHP?

How can I write working router:connect to have SEO friendly links?
In my site, I have articles and category. In right sidebar article's category is listed with below link.
<?php echo $this->Html->link(ucwords($data['Category']['name']),array('controller'=>'Articles','action'=>'displayArticles','cat'=>$data['Category']['name'])) ?>
This gives me link like - http://example.com/Articles/displayArticles/category-name , now I want the link as http://example.com/category-name. So for that I have tried below code but its not working.
Router::connect(
'/:query/*', array('controller' => 'Articles', 'action' => 'displayArticles'), array(
'params' => array('query', 'cat'),
'named' => array(
'query', 'cat'
)
)
);
So please someone let me know, how to achieve just category name(parameter) in URL.
Thanks in advance!
Router::connect(
'/:query',
array('controller' => 'Articles', 'action' => 'displayArticles',1)
array('query' => '[a-zA-Z]+')
);
Here id is numeric with regex.
please see this
You will also have to give parameter count in router.

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'
));

Pass variable into url with CakePHP

on my website I use Cake PHP and I have an url like this for an article:
/article/testx?name=Stefy
I would like to do a kind of "mod rewrite" and have an url like this:
/name/Stefy
I tried to do it from routes.php but I don't know how to do.
I checked on CakePHP website about "pass" function in array and other topics here on StackOverflow but I can't find a solution, probably because I am a beginner with CakePHP.
Can you help me please?
I thought I should something like this:
Router::connect('/name/:id', array('controller' => 'articoli', 'action' => 'display','testx?name=$id') );
but of course it doesn't work. I think I have to use "pass" in the routes.php
Can you help me?
Thank you!
yeah, you kinda do have to use "pass":
Router::connect('/name/:id',
array('controller' => 'articoli', 'action' => 'display'),
array('pass' => array('id')));
you can than generate links like this:
$this->Html->link('Title', array('action' => 'display', 'id' => 1));
Router::connect('/:slug',array('controller' => 'salons', 'action' => 'details'), array('pass' => array('slug')));

CakePHP Global slug router

I have configured slugs for all my posts and I need the Router that will do the links like:
/controller/post_slug_name
and I need this for all the controllers, but when I go with:
Router::connect('/admin', array('admin' => true, 'controller' => 'settings', 'action' => 'dashboard'));
Router::connect('/:controller/:slug', array('action' => 'index'), array('pass' => array('slug')));
The admin panel is not working. How can I make it like this, simple, and with admin panel working? Thanks
EDIT:
With those tree routers is working as I would like, except that in the control panel I am getting even the index actions in the urls and not cool
Router::connect('/admin', array('admin' => true, 'controller' => 'settings', 'action' => 'dashboard'));
Router::connect('/admin/:controller/:action/*', array('admin' => true));
Router::connect('/:controller/:slug', array('action' => 'index'), array('pass' => array('slug')));
I have not tried this so I am not sure it works, but please try the following...
Router::connect('/:controller/:slug', array('action' => 'view:slug'));
You view function also needs to accept $slug as paramenter:
function view($slug){
...
}
If the above does not work, you could try this as well:
Router::connect('/:controller/*',array('action' => 'view'));
Once again, I have not tried any of these codes and dont know if any works, just putting ideas outthere. When I get home I will them.
Thanks,
try connecting /admin/* instead of just /admin

CakePHP use Regex variable capture in routes?

In my CakePHP app I have static pages set up like this:
Router::connect(
'/terms',
array('controller' => 'pages', 'action' => 'display', 'terms')
);
This will rewrite /terms to /pages/display/terms To make prettier shorter URLs.
Now If I wanted to do this for all my static pages, it would get quite redundant:
Router::connect(
'/terms',
array('controller' => 'pages', 'action' => 'display', 'terms')
);
Router::connect(
'/privacy',
array('controller' => 'pages', 'action' => 'display', 'privacy')
);
Router::connect(
'/about',
array('controller' => 'pages', 'action' => 'display', 'about')
);
With regular mod_rewrite you can do something like this:
/(terms|privacy|about) /pages/display/$1
So I naturally attempted this:
Router::connect(
'/(terms|privacy|about)',
array('controller' => 'pages', 'action' => 'display', '$1')
);
It does not work. Is there support for something like this, if so how do you do it?
Just off the top of my head, this may solve the issue. To perform a regex match on a portion you need to do so like this:
Router::connect(
'/:page',
array(
'controller' => 'pages',
'action' => 'display',
),
array(
'page' => '(terms|privacy|about)',
'pass' => array('page')
)
);
Notice how the page placeholder in the URL gets extrapolated on in the third parameter, the array. In there we say that it must match the provided regular expression. Also of note is that it also says to pass the page placeholder to the action method. That way it knows which page to render.

Resources