问题描述
我使用cakephp 2.3.1,我有Paginator Component的问题。
我的目标是页面:
example.com/abruzzo
example.com/abruzzo/2
example.com/abruzzo/3
我创建了以下路线:
Router :: connect('/:regione /:page',array('controller'=>'regions','action'=>'home'),array('page'=>'[0-9 ] +'));
Router :: connect('/:regione',array('controller'=>'regions','action'=>'home'));
(您可以看到第一个路由工作处理页 param)
现在,为了正确处理页面参数,我将以下行添加到RegionsController的 beforeFilter 。
public function beforeFilter()
{
$ this-> request-> params ['named'] ['page'] = isset($ this-> request-> params ['page']))? $ this-> request-> params ['page']:1;
}
因为我读的Paginator componentsenet会看到ad ['named'] [
p>这是正确的吗?我真的需要这个黑客在beforeFilter()?
然后,我需要显示pagine的数字:
<?php echo $ this-> Paginator-> numbers(); ?>
这里的问题是在链接上创建的URL错误..他们指向: p>
example.com/regions/home/page:2
example.com/regions/home/page:3
etc ...
我不要我需要:
example.com/abruzzo
example.com/abruzzo/2
example.com / abruzzo / 3
第二个问题:
如何强制我想要的URL而不是控制器/操作/页面:N格式?
谢谢!
解决方案这可能是一个。
但是,回答你的第一个问题:如果您确定您的网址不会收到更多参数,则您的路由可以更像
Router :: connect ('/:regione / *',array('controller'=>'regions','action'=>'home'));
并在home操作中执行
public function home($ pageOption = null){
if(is_numeric($ option)){
$ this-> passedArgs ['page'] = $ pageOption;
}
$ items = $ this-> paginate('Home');
...
}
我认为beforeFilter选项更好,因为
现在,帮助其他类似问题(和你的第二个问题)的文章恢复到这个
在home.ctp视图中(或任何你喜欢的)
$ prev_link = str_replace ('page:','',$ this-> Paginator-> prev('«Prev'));
$ prev_link = preg_replace('/ \ / 1/','',$ prev_link);
$ next_link = str_replace('page:','',$ this-> Paginatornext('Next»'));
echo $ prev_link;
echo $ next_link;
所以,而不是通常的
echo $ this-> Paginator-> prev('< Prev');
echo $ this-> Paginator-> numbers();
echo $ this-> Paginator-> next('Next>');
您必须手动替换网址的page:部分,
当然,您可以随时修改PaginationHelper,但我不建议这样做,因为对于每个蛋糕更新,您必须检查PaginationHelper是否更改,然后重新-tweak it。
阅读另一个问题上发表的文章,以防您不清楚某些内容希望它有帮助。
I am using cakephp 2.3.1 and I have problem with Paginator Component.
My goal are pages like:
example.com/abruzzo example.com/abruzzo/2 example.com/abruzzo/3
I have created the follow route:
Router::connect('/:regione/:page', array('controller'=>'regions','action'=>'home'), array('page' =>'[0-9]+')); Router::connect('/:regione', array('controller' => 'regions', 'action' => 'home'));
(as you can see the first route work handle the page param)
Now, to handle the page param correctly, I added the follow line to RegionsController's beforeFilter.
public function beforeFilter() { $this->request->params['named']['page'] = (isset($this->request->params['page'])) ? $this->request->params['page'] : 1; }
because I read the Paginator componenet will look ad ['named']['page'] instead of ['page'] directly.
FIRST QUESTION:
Is this correct? do I really need this hack in beforeFilter() ?
Then, I Need to show the numbers of the pagine using:
<?php echo $this->Paginator->numbers(); ?>
The problem here is that the URL created on the link are wrong.. they point to:
example.com/regions/home/page:2 example.com/regions/home/page:3 etc...
I do not need URLs like these, I need:
example.com/abruzzo example.com/abruzzo/2 example.com/abruzzo/3
SECOND QUESTION:
How could force the url i want instead of controller/action/page:N format?
Thanks!
解决方案This is probably a duplicate.
But, answering your first question: it depends. If you're sure your url is not going to receive more parameters, then your routing can be more like
Router::connect('/:regione/*', array('controller'=>'regions','action'=>'home'));
and in the "home" action do
public function home($pageOption=null) { if (is_numeric($option)) { $this->passedArgs['page'] = $pageOption; } $items = $this->paginate('Home'); ... }
I think the beforeFilter option is better, since it's reusable and allows you to have your params better organized.
Now, the article that helped the other similar question (and your second question) resumes to this
In home.ctp view (or whatever you like)
$prev_link = str_replace('page:', '', $this->Paginator->prev('« Prev')); $prev_link = preg_replace('/\/1"/', '"', $prev_link); $next_link = str_replace('page:', '', $this->Paginatornext('Next »')); echo $prev_link; echo $next_link;
So, instead of the usual
echo $this->Paginator->prev('< Prev'); echo $this->Paginator->numbers(); echo $this->Paginator->next('Next >');
you'll have to manually replace the "page:" part of the url and then echo it.
Of course, you could always modify the PaginationHelper, but I wouldn't recommend that because for every cake update, you have to check if the PaginationHelper was changed an then re-tweak it.
Read the article posted on the other question in case you're not clear about something Hope it helps.
这篇关于使用CakePHP自定义分页路线2.3.1的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!