我试图在线寻找一个解决方案(一如既往)但显然没有提出任何建议。
我正在使用CakePHP,我需要在某些控制器中添加一些排序。我还想为此创建自定义路由。所以这是我的代码不起作用(分页工作,排序nope):
RatesController.php:
public $paginate = array(
'Rate' => array(
'limit' => 10,
'order' => array(
'Rate.created' => 'desc'
),
'conditions' => array( 'Rate.accepted' => 1 ),
//'fields' => array('Rate.*', 'User.username')
),
'Airline' => array(
'limit' => 50,
/*'order' => array(
'Airline.rate' => 'desc',
'Airline.name' => 'asc'
),*/
'conditions' => array(
'Airline.rate >' => 0
),
'fields' => array('Airline.id', 'Airline.name', 'Airline.image_id', 'Airline.rate', 'Image.*')
)
);
function index($category = 'airlines'){
pr($this->request);
$data = $this->paginate('Airline');
pr($data);
}
并且在视野中:
$this->Paginator->options(array(
'url' => array(
'controller' => 'rates',
'action' => 'index',
'category' => $category
)
));
echo $this->Paginator->first('«', array('escape' => false), null, array('class' => 'disabled', 'escape' => false));
echo $this->Paginator->prev('<', array('escape' => false), null, array('class' => 'disabled', 'escape' => false));
echo $this->Paginator->numbers(array(
'modulus' => 6,
'separator' => false
));
echo $this->Paginator->next('>', array('escape' => false), null, array('class' => 'disabled', 'escape' => false));
echo $this->Paginator->last('»', array('escape' => false), null, array('class' => 'disabled', 'escape' => false));
echo' sort by ';
echo $this->Paginator->sort('Airline.rate');
echo' or by ';
echo $this->Paginator->sort('Airline.name');
路线:
Router::connectNamed(array('page'), array('default' => false, 'greedy' => false));
Router::connectNamed(array('sort', 'direction'), array('default' => false, 'greedy' => false));
Router::connect('/rate-airlines', array('controller' => 'rates', 'action' => 'index', 'category' => 'airlines'));
/*
Router::connect(
'/rate-airlines/:page/',
array('controller' => 'rates', 'action' => 'index', 'category' => 'airlines'),
array(
'named' => array('page' => '[a-z]+')
)
);
Router::connect(
'/rate-airlines/:page/:sort/:direction',
array('controller' => 'rates', 'action' => 'index', 'category' => 'airlines'),
array(
'pass' => array('page', 'sort', 'direction'),
//'id' => '[0-9]+',
'named' => array('page' => '[\d]+', 'sort', 'direction')
)
);*/
Router::connect(
'/rate-airlines/:sort/:direction',
array('controller' => 'rates', 'action' => 'index', 'category' => 'airlines'),
array(
'named' => array('sort', 'direction')
)
);
我真的不知道为什么它不起作用,我已经花了好几个小时试图让它工作,寻找答案。
聚苯乙烯。无论我做了什么,我都无法将:sort和:指向请求中的命名数组:
CakeRequest Object
(
[params] => Array
(
[plugin] =>
[controller] => rates
[action] => index
[named] => Array
(
)
[pass] => Array
(
)
[sort] => Airline.rate
[direction] => asc
[category] => airlines
[isAjax] =>
)
...
)
有什么想法吗?
麦克
答案 0 :(得分:1)
你真的不应该使用路由参数进行排序。这是一个糟糕的主意,CakePHP已将它们放入即将发布的3.x版本中。
而是使用查询字符串参数。这正是他们的目的:操纵单个视图。如果你有一个列表,那就传递'sort'和'direction'等参数。
假设您有一个类似 http://example.com/rates/?sort=name&direction=asc 的网址,您可以按照以下方式在控制器中解析它:
<?php
class RatesController extends AppController {
public function index() {
$sort = isset($this->request->query['sort']) ? $this->request->query['sort'] : 'created';
$direction = isset($this->request->query['direction']) ? $this->request->query['direction'] : 'desc';
$this->Paginator->settings = array(
'order' => array($sort => $direction)
);
$rates = $this->paginate('Rate');
$this->set(compact('rates'));
}
}
我还想更进一步,使用查询字符串进行分页......
<?php
class AppController extends Controller {
public $paginate = array(
'paramType' => 'querystring'
);
}
......但这只是个人偏好。
以上是在漫长的一天写的。在考虑之后,CakePHP处理开箱即用的分页。您无需手动指定包含名称参数的路由。
只要您使用Pagination组件和Paginator帮助程序(就像您所做的那样),并为控制器操作定义了单个路由,您就不需要执行任何其他操作。在您的情况下,您的控制器将如下所示:
<?php
class RatesController extends AppController {
public function index() {
$rates = $this->paginate('Rate');
$this->set(compact('rates'));
}
}
您的观点如下:
<?php echo $this->Paginator->sort('Airline.name'); ?>
如果您想将 / rate 重写为 / rates-airlines ,那么您的单一路线将如下所示:
<?php
Router::connect('/rates-airlines', array(
'controller' => 'rates',
'action' => 'index',
));
通过以上操作,paginate
调用将考虑要排序的列和方向。
对不起上面的错综复杂的答案!
答案 1 :(得分:0)
感谢您的回复。问题是我真的需要自定义路由以进行分页,所以我终于使用以下代码实现了我想要的目标:
在 AppController.php 中(请注意,所有$ this-&gt; request-&gt;查询变量都是因为我还想通过GET方法使用表格来使用排序界面)
function beforeFilter() {
if (isset($this->request->params['page'])) {
$this->request->params['named']['page'] = $this->request->params['page'];
}elseif( isset($this->request->query['page']) ){
$this->request->params['named']['page'] = $this->request->query['page'];
}
if (isset($this->request->params['sort'])) {
$this->request->params['named']['sort'] = $this->request->params['sort'];
}elseif (isset($this->request->query['sort'])) {
$this->request->params['named']['sort'] = $this->request->query['sort'];
}
if (isset($this->request->params['direction'])) {
$this->request->params['named']['direction'] = $this->request->params['direction'];
}elseif (isset($this->request->query['direction'])) {
$this->request->params['named']['direction'] = $this->request->query['direction'];
}
routes.php 中的
Router::connect('/rates-airlines', array('controller' => 'rates', 'action' => 'index', 'airlines'));
Router::connect(
'/rates-airlines/:page',
array('controller' => 'rates', 'action' => 'index', 'airlines'),
array(
'named' => array('page' => '[\d]+')
)
);
Router::connect(
'/rates-airlines/:page/:sort/:direction',
array('controller' => 'rates', 'action' => 'index', 'airlines'),
array(
'named' => array('page' => '[\d]+', 'sort', 'direction')
)
);
Router::connect(
'/rates-airlines/:sort/:direction',
array('controller' => 'rates', 'action' => 'index', 'airlines'),
array(
'named' => array('sort', 'direction')
)
);
希望有人会觉得这很有用!
麦克
答案 2 :(得分:0)
有一种更简单的方法。只需再添加一个带有'/ *'的路由即可:
Router::connect('/rates-airlines', array('controller' => 'rates', 'action' => 'index', 'airlines'));
Router::connect('/rates-airlines/*', array('controller' => 'rates', 'action' => 'index', 'airlines'));
所有操作都会正常进行。