我已禁用CakePHP默认路由并添加了一些我自己的路由。我首先创建一个路由前缀:
Configure::write('Routing.prefixes', array('settings));
然后我添加了一些路线:
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]+'));
使用Html::link
方法构建链接非常正确:
$this->Html->link('Users', '/users')
产生
'/users'
,UsersController
内的操作符合预期settings_index
。
但是,PaginatorHelper::sort
预先/settings
,如下所示:
'/settings/users/index/sort:username/direction:asc'
实际上只有在启用内置路由时才有效。所以我有两个问题:
PaginatorHelper
不加'/settings'
?'/users/sort:email/direction:desc'
之类的网址?谢谢!
答案 0 :(得分:1)
在排序功能之前尝试使用此命令:
$this->Paginator->options(array('url' => array_merge(array('settings' => false), $this->passedArgs)));
这实际上为paginator helper在运行这些函数之前设置了一些默认值。通过将settings
设置为false
,您将告诉它不要设置该路线。
答案 1 :(得分:0)
我的问题的答案实际上涉及两件事,因为默认路由被禁用。
首先,我必须为PaginationHelper
提供命名参数:
Router::connectNamed(array(
'sort' => array('action' => 'index', 'controller' => array('users')),
'direction' => array('action' => 'index', 'controller' => array('users')),
));
然后我必须使用分页为控制器和操作提供路由:
Router::connect('/users/index/*', array(
'controller' => 'users',
'action' => 'index',
'settings' => true
));
重要的是要说这条路线应该在以'/users'
开头的路线的末尾。否则星号将优先。所以我的最终路线看起来像这样:
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
));