我正在尝试用cakephp创建分页,但是它给了我找不到的错误地址。 在此服务器上找不到请求的地址'/ mygroup / newsfeed / page:2'。
我在日志文件中收到以下错误
Error: [MissingActionException] Action GroupsController::page:2()
could not be found.
Exception Attributes: array (
'controller' => 'GroupsController',
'action' => 'page:2',
)
请在下面找到我的代码:
查看:
<?php if($this->params['paging']['NewsFeed']['pageCount'] > 1): ?>
<?php $this->paginator->options(array('url' => array('controller' => 'groups',
'action' => 'newsfeed', 'sluggroup' => $groupdata['Group']['group_slug'])));?>
<div class="paginationbiz">
<ul>
<li class="prevnext disablelink"><?php echo $this->paginator->prev('« Prev',array()); ?></li>
<li><?php echo $this->paginator->numbers(); ?></li>
<li class="prevnext"><?php echo $this->paginator->next('Next »',array()); ?></li>
</ul>
</div>
<?php endif; ?>
routes.php文件
Router::connect('/:sluggroup/:action/*', array('controller' => 'groups',
'action' => 'newsfeed'),array('pass' => array('sluggroup')));
控制器:
$this->paginate = array(
'conditions' => array('NewsFeed.group_id'=>$groupdata['Group']['id'],'NewsFeed.status'=>'A'),
'joins' => array(
array(
'alias' => 'newslikes',
'table' => 'news_feed_likes',
'type' => 'LEFT',
'conditions' => array('newslikes.news_feed_id = NewsFeed.id','newslikes.user_id'=>$this->Auth->user('id'),'newslikes.status'=>'1')
),
),
'fields' => array('IFNULL(newslikes.user_id,0) AS likestatus','NewsFeed.id','NewsFeed.group_id','NewsFeed.posted_message','NewsFeed.event_id','NewsFeed.event_desc','NewsFeed.event_slug','NewsFeed.event_title','NewsFeed.doc_id','NewsFeed.doc_title','NewsFeed.doc_file','NewsFeed.doc_path','NewsFeed.alert_id','NewsFeed.alert_title','NewsFeed.alert_desc','NewsFeed.created','NewsFeed.user_id','NewsFeed.status','NewsFeed.newslike'
,'IFNULL(newslikes.status,0) AS status'),
'order' => array(
'NewsFeed.updated' => 'desc'
),'limit' =>10, 'page'=>1,
);
$this->set('newsfeed', $this->paginate( $this->NewsFeed ) );
答案 0 :(得分:0)
由于同样的问题,你有similar question(也没有回答)。
你的问题是路线
Router::connect('/:sluggroup/:action/*', array('controller' => 'groups',
'action' => 'newsfeed'),array('pass' => array('sluggroup')));
那,我的朋友,正在杀了你。对于像 domain.url / your-controller / your-action 这样的每个网址,您都可以,因为该网址与您的路线不符。 但是,对于 domain.url / your-controller / your-action / some-parameter 这样的网址,您会得到一个匹配。
domain.url/your-controller/your-action != /:sluggroup/:action/* //ok case
domain.url/sluggroup/action/* == /:sluggroup/:action/* //ok case
//NOT THE MATCH THE MATCH YOU WANT
domain.url/your-controller/your-action/params == /:sluggroup/:action/*
我看到的唯一选择是更改该路线,否则这将继续发生在您身上,不需要的匹配。现在,有两个选择......好吧,可能更多。如果获取sluggroup参数的url对应一个控制器,让我们称之为“GroupsController”,你可以做类似的事情
Router::connect('/groups/:sluggroup/:action/*', array('controller' => 'groups',
'action' => 'newsfeed'),array('pass' => array('sluggroup')));
这将匹配 domain.com/groups/slug/action/etc ,但等网址,它仍然会为您提供同一控制器的其他操作的问题(例如 domain.com/groups/edit/2 )。您可以避免为每个操作添加更多网址,例如
Router::connect('/groups/edit/*', array('controller' => 'groups',
'action' => 'edit')));
Router::connect('/groups/edit', array('controller' => 'groups',
'action' => 'edit')));
Router::connect('/groups/:sluggroup/:action/*', array('controller' => 'groups',
'action' => 'newsfeed'),array('pass' => array('sluggroup')));
另一个选项是使路线跳过具有正则表达式参数的规则
Router::connect('/:sluggroup/:action/*', array('controller' => 'groups',
'action' => 'newsfeed'),
array('pass' => array('sluggroup'),
array('sluggroup' => '[^(groups)]')));
我对正则表达式并不擅长,但我的想法是你只将该路由应用于sluggroup
与正则表达式匹配的网址,否则跳过它,它将到达正确的路由。
似乎没有什么可持续的,你是不是没有像 domain.com/groups/newwsfeed/slug/other-params 这样的网址并保持简单?