I have disabled CakePHP default routes and have added some of my own. I have first create a routing prefix:
Configure::write('Routing.prefixes', array('settings));
And then I have added some routes:
Router::connect('/users', array('controller' => 'users', 'action' => 'index', 'settings' => true));
Router::connect('/users/add', array('controller' => 'users', 'action' => 'add', 'settings' => true));
Router::connect('/users/:id', array('controller' => 'users', 'action' => 'view', 'settings' => true), array('pass' => array('id'), 'id' => '[0-9]+'));
Router::connect('/users/:id/edit', array('controller' => 'users', 'action' => 'edit', 'settings' => true), array('pass' => array('id'), 'id' => '[0-9]+'));
Router::connect('/users/:id/delete', array('controller' => 'users', 'action' => 'delete', 'settings' => true), array('pass' => array('id'), 'id' => '[0-9]+'));
Building links with Html::link method works quite right:
$this->Html->link('Users', '/users')
generates
'/users'
and the action within UsersController is settings_index as expected.
However, PaginatorHelper::sort prepends /settings, like this:
'/settings/users/index/sort:username/direction:asc'
which actually only works if I enable the built-in routes. So I have two questions:
How can I make PaginatorHelper not to prepend '/settings'?
How can I make the PaginatorHelper named parameters work with my custom routes, so that I can have URL like '/users/sort:email/direction:desc'?
Thanks!!
Try using this command before your sort functions:
$this->Paginator->options(array('url' => array_merge(array('settings' => false), $this->passedArgs)));
This essentially sets some defaults for the paginator helper to use before it runs those functions. By setting settings to false, you will tell it not to set that route.
The answer to my problem actually involves two things since default routes are disabled.
First I have to provide named parameters for PaginationHelper:
Router::connectNamed(array(
'sort' => array('action' => 'index', 'controller' => array('users')),
'direction' => array('action' => 'index', 'controller' => array('users')),
));
And then I have to provide routes for controllers and actions using pagination:
Router::connect('/users/index/*', array(
'controller' => 'users',
'action' => 'index',
'settings' => true
));
It is important to say that this route should be at the end of the routes starting with '/users'. Otherwise the asterisk will take precedence. So my final set of routes looks like this:
Router::connectNamed(array(
'sort' => array('action' => 'index', 'controller' => array('users')),
'direction' => array('action' => 'index', 'controller' => array('users')),
));
Router::connect('/users', array('controller' => 'users', 'action' => 'index', 'settings' => true));
Router::connect('/users/add', array('controller' => 'users', 'action' => 'add', 'settings' => true));
Router::connect('/users/:id', array('controller' => 'users', 'action' => 'view', 'settings' => true), array('pass' => array('id'), 'id' => '[0-9]+'));
Router::connect('/users/:id/edit', array('controller' => 'users', 'action' => 'edit', 'settings' => true), array('pass' => array('id'), 'id' => '[0-9]+'));
Router::connect('/users/:id/delete', array('controller' => 'users', 'action' => 'delete', 'settings' => true), array('pass' => array('id'), 'id' => '[0-9]+'));
Router::connect('/users/index/*', array(
'controller' => 'users',
'action' => 'index',
'settings' => true
));
Related
I know this question is been asked several times, and I went through all other solutions, but I could do not solve the issue.
https://www.example.com/cars/view/161/BMW-X5
to be
https://www.example.com/cars/161/BMW-X5
my routes.php
Router::connect('/', array('controller' => 'pages', 'action' => 'display', 'home'));
Router::connect('/admin',array('controller' => 'users', 'action' => 'login', 'admin' => true));
CakePlugin::routes();
require CAKE . 'Config' . DS . 'routes.php';
You can simply connect with
Router::connect('/cars/*', array('controller' => 'cars', 'action' => 'view'));
CarsController::view() will receive single argument, which should be used to find car by name.
Link with $this->Html->link('My Link', array('controller' => 'cars', 'action' => 'view', $slug));
For more route components, you need to use named parameters:
Router::connect('/cars/:id/:slug',
['controller' => 'cars', 'action' => 'view'],
[
// :id parameter will be passed as argument of CarsController::view()
'pass' => ['id']
]
);
Link with $this->Html->link('My Link', array('controller' => 'cars', 'action' => 'view', 'id' => $id, 'slug' => $slug));
Is it possible to use annotations on top of regular routes in Phalcon. For example, I have this:
$router->add('/', [
'module' => 'home',
'controller' => 'index',
'action' => 'index'
])->setName('home');
$router->notFound([
'module' => 'home',
'namespace' => 'Home\Controller',
'controller' => 'error',
'action' => 'show404'
]);
foreach ($app->getModules() as $key => $module) {
$router->add('/'.$key.'/:params', [
'module' => $key,
'controller' => 'index',
'action' => 'index',
'params' => 1
])->setName($key);
$router->add('/'.$key.'/:controller/:params', [
'module' => $key,
'controller' => 1,
'action' => 'index',
'params' => 2
]);
$router->add('/'.$key.'/:controller/:action/:params', [
'module' => $key,
'controller' => 1,
'action' => 2,
'params' => 3
]);
}
$router->addModuleResource('api', 'Api\\Controller\\Index', '/api/index');
$router->addModuleResource('api', 'Api\\Controller\\Client', '/api/client');
The problem is that the router annotations that I define near the bottom don't seem to work, probably because the routes to /api/index and /api/client have already been defined above.
Also, at the same time, is there a way to define a notFound for a module? Basically, if I'm at some URL that starts with /api/, I would like it to go to an error page in my api module. Otherwise, it should go to error page in the regular module.
I have several templates:
/admin/
/somecode1/somecode2/
/staticpage.htm
Trying to enter on /admin/. Doing follow:
$router->addRoutes(array(
'adminsys' => new Zend_Controller_Router_Route('/:module/:controller/:action/*', array('module' => 'admin', 'controller' => 'index', 'action' => 'index')),
'page_catalog' => new Zend_Controller_Router_Route('/:code/:page', array('module' => 'default', 'controller' => 'Staticcatalog', 'action' => 'index', 'code' => '', 'page' => '')),
'static' => new Zend_Controller_Router_Route_Regex('([\wА-Яа-я\-\_]+)\.(htm|html)', array('module' => 'default', 'controller' => 'static', 'action' => 'index')
));
also I tried to change 'adminsys' on :
'adminsys' => new Zend_Controller_Router_Route('/admin/:controller/:action/*', array('module' => 'admin', 'controller' => 'index', 'action' => 'index')),
or
'adminsys' => new Zend_Controller_Router_Route('/admin/*', array('module' => 'admin', 'controller' => 'index', 'action' => 'index')),
But all time it routes on 'page_catalog'.
If I comment it, I can enter /admin/. But not with 'page_catalog'.
What I'm doing wrong here?
When you define routes, you define the general one first, and then you go more and more specific after. The router takes your routes 'last one first' and stops on the first that matches.
This means that if '/admin' could also work for the 'page_catalog' route, it would use this one, before even trying to match 'adminsys' route. And that's the thing, '/admin' could be a 'page_catalog' url where :code param would be 'admin'.
The second variant of the adminsys route is a good one, you just have to make it the last one of your routes in order to avoid any more general route to match first :
$router->addRoutes(array(
'page_catalog' => new Zend_Controller_Router_Route('/:code/:page', array('module' => 'default', 'controller' => 'Staticcatalog', 'action' => 'index', 'code' => '', 'page' => '')),
'static' => new Zend_Controller_Router_Route_Regex('([\wА-Яа-я\-\_]+)\.(htm|html)', array('module' => 'default', 'controller' => 'static', 'action' => 'index')),
'adminsys' => new Zend_Controller_Router_Route('/admin/:controller/:action/*', array('module' => 'admin', 'controller' => 'index', 'action' => 'index'))
));
I have this router:
Router::connect
(
':renting/:model_name/:number_name/',
array('controller' => 'cars', 'action' => 'view'),
array(
'pass' => array('renting', 'model_name', 'number_name'),
'renting' => 'renting'
)
);
When I do this in a view
$html->link("NAME",
array(
'controller' => 'cars',
'action' => 'view',
'renting',
$model_name,
$number_name
)
);
I obtain this link on the page: mypage.com/cars/view/renting/mustang/03212xd
and i need obtain mypage.com/renting/mustang/03212xd
Im stuck here. Thank you. Note: muypage.com/renting/mustang/03212xd redirects right to my controller function but not when use $html->link function.
If you name your parameters, you need to name your parameters:
array(
'controller' => 'cars',
'action' => 'view',
'renting' => 'renting',
'model_name' => $model_name,
'number_name' => $number_name
)
If :renting is always 'renting', you don't need to parameterize it:
Router::connect('/renting/:model_name/:number_name/',
array('controller' => 'cars', 'action' => 'view', 'renting'),
array('pass' => array('model_name', 'number_name')));
$html->link(…, array(
'controller' => 'cars',
'action' => 'view',
'renting',
'model_name' => $model_name,
'number_name' => $number_name
));
I've got a controller called Items and another one called Categories. Items may belong to different Categories.
The routes that I have are
/items
/items/:item_id
/items/categories
/items/categories/:category_id
And this is my routes.php
Router::connect('/items', array('controller' => 'items', 'action' => 'index');
Router::connect('/items/:item_id', array('controller' => 'items', 'action' => 'view'), array('pass' => array('item_id')), array("item_id" => "[0-9]+"));
Router::connect('/items/categories', array('controller' => 'categories', 'action' => 'index'));
Router::connect('/items/categories/:category_id', array('controller' => 'categories', 'action' => 'view', array('pass' => array('category_id')), array("category_id" => "[0-9]+"));
The problem is that the third route fails as it routes to the Items controller which it shouldn't.
How can I fix this problem?
be careful on the order of your routes... cake will match to the first occurrence, so try using your routes like this:
Router::connect('/items', array('controller' => 'items', 'action' => 'index');
Router::connect('/items/categories', array('controller' => 'categories', 'action' => 'index'));
Router::connect('/items/categories/:category_id', array('controller' => 'categories', 'action' => 'view', array('pass' => array('category_id')), array("category_id" => "[0-9]+"));
Router::connect('/items/:item_id', array('controller' => 'items', 'action' => 'view'), array('pass' => array('item_id')), array("item_id" => "[0-9]+"));
good luck
in you config/routes.php
put a line
Router::connect('/items/categories', array('controller' => 'categories',
'action' => 'index'));
or something similar.
it will redirect all you /items/categories to the index action of categories controller.
for admin pages (actions that use admin prefix):
Router::connect('/items/categories', array('controller' => 'categories',
'action' => 'index','admin'=>true));