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
));
Related
I don't know if someone else is using kohana (koseven with new name) framework for develepment. I need help about routing. I am migrating an asp site to php with using koseven ( kohana) framework and I must keep all the url routing on current site. Because of this I must use more than one routing on my project.
Url structer must be like this:
domain.com/contenttype/contentid -> contenttype is dynamic and gets data over Content Controller
domain.com/profile/username ->profile is the controller and index is the action. I must get the user name from id parameter.
domain.com/categories/categorname (Works fine-> categories is the controller, index is the action and categorname is the id parameter.
There is an admin page on my site and using a directory route on it.
Here is my route on bootstrap.php file:
Route::set('panel', '<directory>(/<controller>(/<action>(/<id>)))', array('directory' => 'panel'))
->defaults(array(
'controller' => 'panel',
'action' => 'index',
));
Route::set('kategori','<kategori>(/<id>)', array('id'=>'.*'))
->defaults([
'controller'=>'kategori',
'action'=>'index',
]);
Route::set('default', '(<controller>(/<action>(/<id>)))', array('id'=>'.*'))
->defaults([
'controller' => 'anasayfa',
'action' => 'index',
]);
First Problem: If I copy kategori route for profile it uses kategori route instead of profile.
Second Problem: How can I get dynamic routing for the contenttype. Content controller is the default controller and it will list the contents under the dynamic contenttype if there isn't given any content title on the id parameter. If the id parameter is identified at this time it will Show the detail of content.
Thanks.
Route::set('panel', 'panel(/<controller>(/<action>(/<id>)))', ['controller' => '\w+', 'action' => '\w+', 'id' => '[-\w]+'])
->defaults(array(
'directory' => 'panel',
'controller' => 'dashboard',
'action' => 'index',
'id' => null,
));
Route::set('kategori','<kategori>(/<id>)', ['kategori' => '[-\w]+', 'id' => '[-\w]+'])
->defaults([
'controller' => 'kategori',
'action' => 'index',
'id' => null,
])
->filter(function ($route, $params, $request) {
$model = ORM::factory('Kategori', ['kategori' => $params['kategori'], 'id' => $params['id']]);
if ($model->loaded()) {
$params['model'] = $model;
return $params;
}
return false;
});
Route::set('default', '(<controller>(/<action>(/<id>)))', ['controller' => '\w+', 'action' => '\w+', 'id' => '[-\w]+'])
->defaults([
'controller' => 'anasayfa',
'action' => 'index',
'id' => null,
]);
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.
I can somehow change the generated and accepted routes for simple urls, in the routes.php:
Router::connect('/login', array('controller' => 'users', 'action' => 'login'));
Router::connect('/logout', array('controller' => 'users', 'action' => 'logout'));
Router::connect('/register', array('controller' => 'users', 'action' => 'add'));
This works like a charm. However, this doesn't:
Router::connect('/eintrag/:id', array('controller' => 'entries', 'action' => 'view'));
Router::connect('/bearbeiten/:id', array('controller' => 'entries', 'action' => 'edit'));
When I try to get a route for this, via echo $this->Html->url(array('controller' => 'entries', 'action' => 'view', $entry['id'])), I get /entries/view/1. And the url /eintrag/1 is not accepted by the router.
How can I prettify my view and edit routes like I can do with parameterless routes?
You need to use a third param in your route, as you are passing it :id specifically.
// SomeController.php
public function view($id = null) {
// some code here...
}
// routes.php
Router::connect(
'/eintrag/:id', // e.g. /eintrag/1
array('controller' => 'entries', 'action' => 'view'),
array(
// this will map ":id" to $id in your action
'pass' => array('id'),
'id' => '[0-9]+'
)
);
should do it.
More info # the cookbook
$this->Html->url() is just a Helper Function, that simply generates a URL according to paramaters passed, however when you actually open this Url then it will route request made for /eintrag/1 to /entries/view/1
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'));