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,
]);
Related
I am using cake php and due to some reason i want to hide controller and action name from the url . current url us like
http://192.168.1.31/home/this_is_test
where home is controller name and this_is_test is slug which is dynamic . i want the url like
http://192.168.1.31/this_is_test.
my routes.php is
Router::connect('/', array('controller' => 'home', 'action' => 'index'));
Router::connect('/dashboard', array('controller' => 'dashboard', 'action' => 'index'));
Router::connect('/login', array('controller' => 'users', 'action' => 'login'));
Router::connect('/admin/login', array('controller' => 'users', 'action' => 'login', 'admin' => true));
Router::connect('/contents/*', array('controller' => 'contents', 'action' => 'view'));
Router::connect('/home/*', array('controller' => 'Home', 'action' => 'index'));
I have read a couple of solution after googling . also tried this in routes.php . but no luck
Router::connect(
'/:query',
array('controller' => 'Home', 'action' => 'index',1),
array('query' => '[a-zA-Z]+')
);
anybody have idea about this if it is possible??
Your solution
For static text try this:
Router::connect('/this_is_test', array(
'controller' => 'home',
'action' => 'this_is_test OR any_other action name'
));
If it's dynamic
Router::connect('/:id',
array('controller' => 'home', 'action' => 'index'),
array(
'pass' => array('id'),
array('id' => '[A-Za-z]')
)
);
References: Cakephp2.x Route
I hope I knew what you really want to achieve. You can place the Route in the last position. Here is the reference .
Other option would be to use alias for your controller. So you call your controller some thing else and set a new name for your controller then call it in you Route.
If this doesn't work then you would need to write a bespoke Component in order to help you to do that.
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')));
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 want this routes to work
forum/forum-name.9 -> controller = forum, action = index, id = forum-name.9
forum/forum-name.9/edit -> controller = forum, action = edit, id = forum-name.9
forum/rules -> controller = forum, action = rules, id = null
I tried
Route::set('default', '(/<controller>)((/<id>)(/<action>)))',
array(
'controller' => '[a-zA-Z_-]+',
'action' => '[a-zA-Z_-]+',
'id' => '[a-zA-Zа-я0-9.-]+',
))
->defaults(array(
'controller' => 'forum',
'action' => 'index',
'id'=>null
));
But its wrong, because id now can contain only letter
Routes should be specific. Do NOT try to sole everything using one route. These will do what you want.
Route::set('forum/rules', 'forum/rules')
->defaults(array(
'controller' => 'forum',
'action' => 'rules',
));
Route::set('forum', 'forum/(<name>.)<id>(/<action>)',
array(
'action' => 'edit', // the action must not be present (and default to 'index') or be 'edit'
'name' => '\w+',
'id' => '\d+',
))
->defaults(array(
'controller' => 'forum',
));
Also, only add - to the regex for actions and controllers if you overload something and have it replaced. PHP class and function/method names are not allowed to contain a dash.
This solved my problem
'id' => '(\w+.)?\d+'
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