Pass variable into url with CakePHP - 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')));

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 2.4.2 : Route URL

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

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

Help with routing (/:name/:named_param)

Im stuck in cakephp routing.
I want to redirect "/coches/mustang-100/" to "cars" model with "display" view.
I'm trying with this, but it redirects me to "cars" "index" view:
Router::connect('/:coches/:modelo_coche/',
array('controller' => 'cars', 'action' => 'display'),
array('pass' => array('modelo_coche'),'coches' => 'coches')
);
I want to: www.myweb.com/coches/mustang-100/
I hope I got your needs right, please try if this one matches what you want:
Router::connect('/coches/:modelo_coche', array('controller' => 'cars', 'action' => 'display'), array('pass' => array('modelo_coche')));
This probably isn't the answer but I don't have the rep to comment on your question yet.
But, can you put a trailing slash on the end of a route like that?
I've never seen routing done with slashes at the end so this could be the problem here.

Routing configuration in cakephp

I am trying to implement routing in cakephp. I want the urls to mapped like this...
www.example.com/nodes/main -> www.example.com/main
www.example.com/nodes/about -> www.example.com/about
So for this I wrote in my config/routes.php file..
Router::connect('/:action', array('controller' => 'nodes'));
Now, I got the thing going but when I click on the links, the url in browser appears like
www.example.com/nodes/main
www.example.com/nodes/about
Is there some way where I can get the urls to appear the way they are routed?
Setting in .htaccess or httpd.conf would be easy - but I don't have access to that.
Regards
Vikram
This should work:
Router::connect('/main', array('controller' => 'nodes', 'action' => 'main'));
Router::connect('/about', array('controller' => 'nodes', 'action' => 'about'));
You may also do something more powerful, like this:
$actions = array('main','about');
foreach ($actions as $action){
Router::connect('/$action', array('controller' => 'nodes', 'action' => '$action'));
}
Basically if your links are created with Html helper, with the following format:
<?php echo $this->Html->link('your link', array('controller'=>'nodes', 'action'=>'main'));?>
Then the Cake will convert the links properly to www.example.com/main
But if your links are
<?php echo $this->Html->link('your link', '/nodes/main/');?>
they will point to www.example.com/nodes/main

Resources