optional zend route - php

I have a Zend app using a custom route:
$router->addRoute(
'search',
new Zend_Controller_Router_Route('search/:s/*',array(
'module' => 'public',
'controller' => 'post',
'action' => 'search'
));
$router2->addRoute(
'search',
new Zend_Controller_Router_Route('search/:s/type/:controller/*',array(
'module' => 'public',
'controller' => 'post',
'action' => 'search'
));
But i wanna know if it's possible to make "type/:controller" optional (and a default value for controller asignated) and unificate these two rules in one so that It works for:
mydomain/search/prueba => controller is post and action is search
mydomain/search/prueba/type/event => controller is event and action is search

Related

Kohana more than one dynamic routing for specific urls

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,
]);

Router cakephp pointing to same action

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.

Cakephp2.3: Custom route does not working

I have define the custom route rule as:
Router::connect('/permission/',
array(
'plugin' => 'Authorization',
'controller' => 'permissions',
'action' => 'index',
'admin' => true,
),
);
With this setting routes works fine for url like http://example.com/permission
But I also need to define route for the parameters
I have tried for it as:
Router::connect('/permission/:index',
array(
'plugin' => 'Authorization',
'controller' => 'permissions',
'action' => 'index',
'admin' => true,
),
array(
'pass' => array('index')
)
);
With this route cake throw exception:
Error: PermissionController could not be found.
I want route to be work for
http://example.com/permission/2
It should point to http://example.com/admin/authorization/permissions/index/2
can anyone know, how to define custom routes with parameters correct way?
You need to modify your route like as
Router::connect('/permission/:id', array('plugin'=>'authorization,'controller' => 'permissions', 'action' => 'index'),array('pass'=>array('id')));

CakePHP Routes - Generate appropiate links for /:controller/:id/ with html helper

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

How to strip unused parameters from the url?

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.

Categories