I am facing some problems in routing under cakephp
there are two actions in my controller
they are as below:
example.com/posts/show/show-by-day
example.com/posts/view/slug-post
I want them as:
example.com/article/show-by-day.html
example.com/article/slug-post.html
So i routes file under config file I wrote as:
Router::connect('/article/:show_by_day', array('controller' => 'posts', 'action' => 'show'), array('pass' => array('show_by_day')));
Router::connect('/article/:slug', array('controller' => 'posts', 'action' => 'view'), array('pass' => array('slug')));
its working fine when I hit the url example.com/article/show-by-day.html
but when I hit url example.com/article/slug-post.html , its again point to action show.
So how can I solve it?
Many thanks!
try this
Router::connect( '/article/show-by-day.html',
array(
'controller' => 'posts',
'action' => 'show'
),
array(
'pass' => array('show_by_day')
)
);
Router::connect( '/article/slug-post.html',
array(
'controller' => 'posts',
'action' => 'view'
),
array(
'pass' => array('slug')
)
);
Explanation:
The first parameter of the Router::connect is the exact URL you want to match - in the original code, a : was included - which parameterized the URL instead of using exact match. The second and third paramters in Router::connect are the actual controller / action that need to be invoked along with required parameters.
Related
I have created URLs in cake 2.x using strings like
$this->Html->link('View', '/posts/view/' . $id)
//posts/view/id
in multiple times And then later decided that /posts should be called /articles instead in URL.
//articles/view/id
I don't want to change my existing code, I want to use Reverse Routing.
I read about the reverse routing but could not find any suitable example
related to my problem.
It will be appreciated if anybody gives me solution regarding my problem ?
Dont hard code the urls, use arrays instead
$this->Html->link('View', array('controller' => 'posts', 'action' => 'view' ,$id));
And in your route file (app/Config/routes.php), specify the routing rule
//The indexaction
Router::connect(
'/articles',
array('controller' => 'posts', 'action' => 'index')
);
//The view action
Router::connect(
'/articles/:id',
array('controller' => 'posts', 'action' => 'view'),
array(
'pass' => array('id'),
'id' => '[0-9]+'
)
);
I have this route set up, and manually entering the url works:
Router::connect('/:controller/:id/*',
array('action' => 'view'),
array('pass'=>array('id'), 'id' => '[0-9]+'));
However, with html->link, I can't generate a link without the action.
$this->Html->link('linkName', array(
'controller' => 'controllerName',
'action' => 'view',
$id, $slug));
generates controllerName/view/id
$this->Html->link('linkName', array(
'controller' => 'controllerName',
$id, $slug));
generates controllerName/index/id
How can I generate the url controller/id with html helper?
Thanks!
CakePHP needs specificity. Here, I had to specify the action and the id, then cake matched these attributes to the correct route and generated the correct url:
$this->Html->link('linkName', array(
'controller' => 'controllerName',
'action' => 'view',
'id' => $id,
$slug
));
I'm trying to setup the following routing in cakePHP 2.3:
domain/news/slug
I've followed the cookbook guidelines on routing and the route that gets created is correct. The problem I run into is that when selecting the link I get the 'Missing Method in NewsController' error message.
Here's what I've configured:
Router::connect(
'/news/:slug/',
array('controller' => 'news', 'action' => 'view'),
array(
'pass' => array('slug'),
'slug' => '[^_]+'
)
);
I'm passing in the slug with a regular expression (any string that does not include an underscore).
This is my link in the index page:
<?php echo $this->Html->link(
$news['News']['title'],
array(
'controller' => 'news',
'action' => 'view',
'slug' => $news['News']['slug']
)
); ?>
As mentioned, the URL is built correctly, and looks like this: /news/test-slug-news-story
But when I click on it I get the 'Missing Method in NewsController' error message
Is it obvious what I'm missing, cause I've looked at this too long to be able to see it.
Thanks, Paul
You can try this one:
<?php
// Routing code
Router::connect('/news/:slug/',
array(
'controller' => 'news',
'action' => 'view'
),
array(
'slug' => '[a-zA-Z0-9_-]+'
)
);
?>
<?php
// HTML Link code.
echo $this->Html->link(
$news['News']['title'],
array(
'controller' => 'news',
'action' => 'view',
'slug' => $news['News']['slug']
)
);
?>
If it is not working for you please let me know :)
Thanks
As mentioned above, I discovered that by having a backslash after 'slug' in the route setting, the controller interprets the ':slug/' as the controller action.
One of those 'doh' moments.
Code should look like this:
Router::connect(
'/news/:slug',
array('controller' => 'news', 'action' => 'view'),
array(
'pass' => array('slug'),
'slug' => '[a-zA-Z0-9_-]+'
)
);
I'm using Zend Framework and the URL View Helper to create URLs
I have some lines like this in my navigation:
$this->url(array('controller' => 'index', 'action' => 'index'))
$this->url(array('controller' => 'who', 'action' => 'view', 'id' => $row->who_id));
$this->url(array('controller' => 'projects', 'action' => 'view', 'id' => $row->mai_id));
$this->url(array('controller' => 'content', 'action' => 'view', 'type' => 'theater', 'id' => $row->the_id));
$this->url(array('controller' => 'shows', 'action' => 'view'));
This way, at first, I have some URL like this
http://ccgss.local/information/location
http://ccgss.local/who/view/id/1
But when I access another link with more parameters like http://ccgss.local/content/view/id/1/type/theater
it messes up with the parameters that still were there: http://ccgss.local/who/view/id/1/type/theater
I mean, the parameters don't clean up when I access another page.
How do I fix this?
You need to reset parameters when calling url helper.
$this->url(array('controller' => 'index', 'action' => 'index'), null, true);
The second argument is the name of the route to use. Keep it null if you want to use the current route.
The third argument tells whether or not to reset parameters. It's false by default.
So, just set it to true to get rid of existing parameters.
I'm generating plain simple links with CakePHP's HtmlHelper the following way:
$html->link("Newest", array(
'controller' => 'posts',
'action' => 'listView',
'page'=> 1,
'sort'=>'Question.created',
'direction'=>'desc',
));
Having the following route rule:
Router::connect('/foobar/*',array(
'controller' => 'posts',
'action' => 'listView'
));
The link is nicely generated as /foobar/page:1/sort:Question.created/direction:desc. Just as I want, it uses my URL prefix instead of controller/action names.
However, for some links I must add named parameters like this:
$html->link("Newest", array(
'controller' => 'posts',
'action' => 'listView',
'page'=> 1,
'sort'=>'Question.created',
'direction'=>'desc',
'namedParameter' => 'namedParameterValue'
));
The link in this case points to /posts/listView/page:1/sort:Question.created/direction:desc/namedParameter:namedParameterValue. But I do not want to have contoller/action names in my URL-s, why is Cake ignoring in this case my routers configuration?
Quite undocumented, but mentioned, this solved it:
Router::connectNamed(array('namedParameter', 'page', 'sort', 'direction'));