I have a about us page url like below:
base_url/cmsPages/index/cmsid:1
And in routes.php i have defined
Router::connect(
'/about_us',
array('controller' => 'cmsPages', 'action' => 'index', 'cmsid' => 1),
);
But i am not getting cmsid in $this->request->params['named']['cmsid'] at index action.
Please help, how can i achieve this.
You can use following:
public function index($cmsid = null) {
// some code here...
}
// routes.php
Router::connect(
'/about_us',
array('controller' => 'cmsPages', 'action' => 'index'),
array('pass'=>array('cmsid'=>1))
);
for detailed information please visit following link :
http://book.cakephp.org/2.0/en/development/routing.html#passing-parameters-to-action
Try this code for getting named parameter
Router::connectNamed(array('id'));
Router::connect('/about-us:id', array(
'plugin' => false
'controller' => 'cmsPages',
'action' => 'index'
),array(
"pass"=>array("id")
),array(
'id' => '[0-9]+'
)
);
If you are using named parameters, you can define this route
Router::connect(
'/about_us',
array('controller' => 'cmsPages', 'action' => 'index'),
array('cmsid'=>1)
);
Related
For example :
Router::connect(
'/:username',
array('controller' => 'users', 'action' => 'profile'),
array('pass' => array('username'))
);
If the username parameter is prefixed by #, then it will redirect to a method. If not, it will redirect to different method.
Note : I'm using version 2.8
This should work:
Router::connect(
'/#:username',
array('controller' => 'users', 'action' => 'action1'),
array('pass' => array('username'))
);
Router::connect(
'/:username',
array('controller' => 'users', 'action' => 'action2'),
array('pass' => array('username'))
);
I'm trying to connect this:
http://example.com/activate/A1B2C3
To this:
http://example.com/users/activate/A1B2C3
Router::connect('/activate/:token', array('controller' => 'users', 'action' => 'activate'), array('pass' => 'token'));
But the parameter is not being passed along. What am I doing wrong here?
pass must be an array.
Router::connect('/activate/:token',
array('controller' => 'users', 'action' => 'activate'),
array('pass' => array('token'))
);
I am trying to redirect /my-old-url/$slug to /news/$slug in CakePHP with the following code:
Router::redirect(
'/my-old-url/*',
array('controller' => 'news', 'action' => 'view'),
array('persist' => true)
);
Using this code throws an error (may be a bug?)
Warning (2): Invalid argument supplied for foreach() [CORE/Cake/Routing/Route/CakeRoute.php, line 381]
public function persistParams($url, $params) {
foreach ($this->options['persist'] as $persistKey) {
Even trying the following code does not work:
Router::redirect(
'/my-old-url/:slug',
array('controller' => 'news', 'action' => 'view'),
array('persist' => array('slug'))
);
This code redirects my to /news/view without any parameters.
I use the following code to connect the news URLs:
Router::connect('/news',
array('controller' => 'news', 'action' => 'index')
);
Router::connect('/news/:slug',
array('controller' => 'news', 'action' => 'view'),
array('pass' => array('slug'))
);
What to do?
Have you properly setup your routing for the new url?
Router::connect(
'/news/:slug',
array('controller' => 'news', 'action' => 'view'),
array('pass' => array('slug'), 'slug' => '[\w]')
);
Then have it do the redirect with
Router::redirect(
'/old-url/:slug',
array('controller' => 'news', 'action' => 'view'),
array('persistent' => true, 'pass' => array('slug'))
);
I don't believe you need to define slug again ([\w]) in the redirect but if it doesn't work then try it ('slug' => '[\w]' after 'pass').
Obviously you need to test if /news/test works as expected with the routing before trying the redirect.
Try following code
Router::connect('/my-old-url/*', array('controller' => 'News','action' => 'view'),array('params' => '[a-zA-Z0-9]+'));
There probably was a bug in CakePHP. Upgrading to the latest version (currently v2.3.9) did the trick. The code in my question does work.
Firstly, I understand there have been many questions and solutions about this but none I've found fix my issue.
In summary, the Html helper is not reverse routing correctly and instead of URLs like this:
http://website.com/user_of_website/slug_name
I get URLs like this:
http://website.com/things/view/username:user_of_website/slug:a_thing
Here is my router setup:
Router::connect('/', array('controller' => 'pages', 'action' => 'display', 'home'));
/**
* ...and connect the rest of 'Pages' controller's urls.
*/
Router::connect('/pages/*', array('controller' => 'pages', 'action' => 'display'));
//User routes
Router::connect('/login', array('controller' => 'users', 'action' => 'login'));
Router::connect('/logout', array('controller' => 'users', 'action' => 'logout'));
Router::connect('/register', array('controller' => 'users', 'action' => 'add'));
Router::connect('/users/:action/*', array('controller' => 'users'));
Router::connect('/things/:action/*', array('controller' => 'things'));
Router::connect(
'/:username/:slug',
array(
'controller' => 'things',
'action' => 'view'
),array(
'username' => '[a-zA-Z0-9_]+',
'slug' => '[a-zA-Z0-9_]+',
'pass'=>array(
'username',
'slug'
)
)
);
Router::connect('/:userName', array('controller' => 'things', 'action' => 'user'),array('pass'=>array('userName')));
And my Html helper:
echo $this->Html->link('View', array(
'controller' => 'things',
'action' => 'view',
'username' => 'user_of_website',
'slug' => 'a_thing'
),array(
'class' => 'text-warning'
));
I really can't see what I'm doing wrong. Any help greatly appreciated.
NB, I'm using CakePHP 2.3
Have you tried if it works if you disable the 'regular' routes for things? E.g. /things/:action/*.
My guess is that, because of the wildcard, this route will match the URL as well, and, because it is defined before your custom route, will be matched in stead.
If that resolves the problem, you may try to move that route below your 'custom' route
In my CakePHP App I'd like to pass arguments in a custom route.
What works now
(domain/controller/action/param)
domain.com/dealers/view/1
What I'd like to do
(domain/param/controller/action/param)
domain.com/washington/dealers/view/1
This is my code in routes.php:
Router::connect('/:city/dealers/view/:id', array('controller' => 'dealers', 'action' => 'view'),
array(
'pass' => array('city', 'id')
),
array('city' => '[a-z]+')
);
This just redirects domain.com/washington/dealers/view/1 to domain.com/dealers/index for the obvious reason that I did not properly pass the parameters. Does anyone know what I am missing?
city should not be in a separate array ex:
Router::connect(
'/:city/dealers/view/:id',
array('controller' => 'dealers', 'action' => 'index'),
array(
'pass' => array('city', 'id'),
'city' => '[a-z]+'
));