我的网站有很多"页面"。页面是针对不同主题创建的,并由用户主持(例如,加勒比海盗的页面或Justin Bieber的页面)。该页面属于某个类别,例如,电影或音乐。页面的默认网址为http://www.example.com/category-directory/page-id。可选地,如果主持人想要为页面建立他们自己的链接作为一种更容易的导航方式,他们可以通过为" Page.link"提供价值来实现。列(例如:http://www.example.com/potc用于电影加勒比海盗)。
我的所有路线都已设置,这本身需要一些时间。路由检查" .com"之后是否有URL的第一个参数。匹配"链接"任何页面的列,如果是,则路由到该页面及其下的控制器/操作。如果没有,照常路线。
当用户正在查看http://www.example.com/potc/articles/99或http://www.example.com/movies/106/articles/99时(鉴于加勒比海盗是id = 106的页面),用户将被定向到文章控制器,查看动作,id = 99. 106作为$ page_id传递给控制器的动作,并在AppController中设置。
我在控制器中提供的URL数组工作正常,例如,如果id不存在,我会重定向到'controller' => 'articles', 'action' => 'index', 'page_id' = $page_id
。我被重定向回http://www.example.com/potc/articles。我实际上不必定义控制器,因为默认情况下它使用当前控制器。
但是,我对分页链接有疑问。比如我在文章索引页面上说有100篇文章,但我只查看前20页。我希望将URL输出为http://www.example.com/potc/articles/page:2。
相反,如果我将URL数组设置为'controller' => 'articles', 'action' => 'index', 'page_id' => $page_id
,类似于我在控制器中的操作,我会得到链接http://www.example.com/articles/page_id:106/page:2我可以理解为什么这条路由不起作用,因为我可能需要传递值Page.link如果存在,如果不传递Category.directory和Page.id.我已尝试使用网址数组'controller' => 'articles', 'action' => 'index', 'category' => $page['Category']['directory'], 'page_id' => $page_id
执行后一选项。我有一个匹配此路线,但分页链接仍然生成为http://www.example.com/articles/category:movies/page_id:106/page:2
以下是来自我的routes.php的一些片段:https://gist.github.com/9ba0a54a7f4d68803d14
我的问题是:" 我可以使用分页链接的精确当前网址(减去对我当前页面的引用)吗?"例如,如果我正在查看http://www.example.com/dynamic-route/controller/id/page:2,则分页链接将自动转到http://www.example.com/dynamic-route/controller/id/page:3
我将所有分页代码放入一个元素中,我希望在需要分页的地方将该元素包含在我的视图文件中。
现在,如果我正在查看文章控制器,索引操作,则分页链接的默认网址为http://www.example.com/articles/page:2,但我需要http://www.example.com/dynamic-route/articles/page:2
我尝试使用
$this->Paginator->options = array(
'url' => 'http://www.example.com/dynamic-route/articles'
);
但链接的输出方式与http://www.example.com/articles/http://www.example.com/dynamic-route/articles/page:2
类似编辑: Jeztah,你提到了
' customlink' => ' [α-Z0-9] {1}([A-Z0-9 - ] {2,} [A-Z0-9] {1})'?
在我的路线中。好吧,我不能这样做,因为路由会进行查询以检查自定义链接是否在数据库中,如果是,则相应地路由。
我的路由器文件的一个例子是:
获取第一个网址参数http://www.domain.com/ CUSTOM-LINK 后,请执行以下操作:
if($param1 != "users" && $param1){
$pagelink = str_replace('-', ' ', $param1);
$pagesModel = ClassRegistry::init('Page');
$matchedpage = $pagesModel->find('first', array(
'conditions' => array('Page.link LIKE' => "$pagelink"), 'recursive' => '-1'
));
if($matchedpage['Page']['id']){
Router::connect('/' . $param1 . '/:controller', array('action' => 'index', 'page_id' => $matchedpage['Page']['id']), array(
'pass' => array('page_id'),
'page_id' => '[0-9]+',
'persist' => array('page_id'),
));
Router::connect('/' . $param1 . '/:controller/:action/:id', array('page_id' => $matchedpage['Page']['id']), array(
'pass' => array('page_id', 'id'),
'page_id' => '[0-9]+',
'persist' => array('page_id'),
));
Router::connect('/' . $param1 . '/:controller/:id', array('action' => 'view', 'page_id' => $matchedpage['Page']['id']), array(
'pass' => array('page_id', 'id'),
'page_id' => '[0-9]+',
'id' => '[0-9]+',
'persist' => array('page_id'),
));
Router::connect('/' . $param1 . '/:controller/:action', array('page_id' => $matchedpage['Page']['id']), array(
'pass' => array('page_id'),
'page_id' => '[0-9]+',
'persist' => array('page_id'),
));
} // end if page matches
} // end if param1 is not users
如果我转到http://www.domain.com/custom-link/articles,http://www.domain.com/custom-link/articles/1,http://www.domain.com/custom-link/articles/edit/1等,则会显示正确的页面。如果我尝试导航到不存在自定义链接的页面,请继续关闭我的路由器文件,如果没有其他路由匹配,则抛出错误消息。一切都很好。
在我的ArticlesController.php中,重定向方法按预期工作。比如说,我尝试转到http://www.domain.com/custom-link/articles/99,但ID 99不存在,我已使用
正确地重定向回http://www.domain.com/custom-link/articles$ this-> redirect(array(' action' =>' index',' page_id' => $ page_id), null,true);
但是,在分页时,我将url选项设置为
数组('控制器' =>'文章','动作' =>'索引',' page_id& #39; => $ PAGE_ID);
其中$ page_id在AppController中设置为不被路由器传递。虽然我不需要设置控制器和动作,因为将使用电流。同样,我的分页链接显示为http://www.domain.com/articles/page_id:3/page:2而不是我想要的http://www.domain.com/custom-link/articles/page:2
编辑: Jeztah
我刚刚将整个路径文件更改为您拥有自定义链接参数(我重命名为' link')然后创建了一个组件,以便在app控制器中使用以获取页面ID来自链接。
示例路线
Router::connect('/:link/:controller/:action/:id', array(), array(
'pass' => array('link', 'id'),
'link' => '[a-z0-9]{1}([a-z0-9\-]{2,}[a-z0-9]{1})?',
'persist' => array('link'),
));
Router::connect('/:link/:controller/:id', array('action' => 'view'), array(
'pass' => array('link', 'id'),
'link' => '[a-z0-9]{1}([a-z0-9\-]{2,}[a-z0-9]{1})?',
'id' => '[0-9]+',
'persist' => array('link'),
));
但是,将url数组分页为array('link' => 'test-example')
,url将输出为http://www.domain.com/articles/link:test-example/page:2
。我的代码与您的代码完全一样,减去重命名" custom-link"到"链接"。此外,现在我的路线无法区分我是否尝试访问http://www.domain.com/custom-link,这将是'控制器' => '页面','行动' => '视图' (他的页面基本上会使用新闻模型并显示该页面的最新消息),如果我试图访问http://www.domain.com/articles,那将是“控制器”。 => '文章','行动' => '指数'这将显示所有页面的最新文章。注意,文章和新闻是单独的模型。
Router::connect('/:link', array('controller' => 'pages', 'action' => 'view'), array(
'pass' => array('link'),
'link' => '[a-z0-9]{1}([a-z0-9\-]{2,}[a-z0-9]{1})?',
'persist' => array('link'),
));
与
相同Router::connect('/:controller', array('action' => 'index'));
因为检查一下是否链接'在路线中不再属于特定页面。
编辑:好的,我正在取得进展。我发现下一个和之前的分页链接不能很好地接受paginator->选项。我删除了paginator->选项,并将下一个按钮的url数组留空。该链接输出为http://www.domain.com/articles/custom-link/articles/page:2。陆侃!但是,有没有办法让第一篇"文章/"?
我的路线是
Router::connect('/:link/:controller/*', array(), array(
'link' => '[a-z0-9]{1}([a-z0-9\-]{2,}[a-z0-9]{1})?',
));
答案 0 :(得分:0)
最好使用相对路径:
$this->Paginator->options = array(
'url' => '/dynamic-route'
);
Or:
$this->Paginator->options = array(
'url' => '/dynamic-route/articles'
);
Or:
$this->Paginator->options = array(
'url' => array('controller'=>'dynamic-route')
);
或者 开始在Router文件中构建自定义路由 http://api.cakephp.org/class/router#method-Routerurl
答案 1 :(得分:0)
如果您正确构建路线,这应该全部自动运作:
在你的routes.php中
/**
* Movies view/details page
*/
Router::connect(
'/:link',
array(
'controller' => 'movies',
'action' => 'view',
),
array(
/**
* Custom Link:
* lowercase alphanumeric and dashes, but NO leading/trailing dash
* should be at least 4 characters long
*
* IMPORTANT:
* custom links should *NOT* overlap with actual controller
* names, like 'movies, articles' etc!
*/
'link' => '[a-z0-9]{1}([a-z0-9\-]{2,}[a-z0-9]{1})?',
// Define what should be passed to the 'view' action as arguments
'pass' => array('link'),
/**
* Define what parameters should be automatically preserved
* when creating URLs/links
*/
'persist' => array('link'),
)
);
/**
* Article view/details page
*/
Router::connect(
'/:link/:controller/:page_id',
array(
'controller' => 'articles',
'action' => 'view',
),
array(
/**
* Specify the 'controllers' that are allowed to be put
* after link. You can add more, for example
* 'reviews' to show reviews for a movie. this
* will result in Reviews::view([page_id]) to be shown
*/
'controller' => 'articles',
'link' => '[a-z0-9]{1}([a-z0-9\-]{2,}[a-z0-9]{1})?',
'page_id' => '[1-9]{1}[0-9]{0,}',
// Define what should be passed to the 'view' action as arguments
'pass' => array('page_id', 'link'),
/**
* Define what parameters should be automatically preserved
* when creating URLs/links
*/
'persist' => array('page_id', 'link'),
)
);
/**
* Article overview/index page
*/
Router::connect(
'/:link/:controller/*',
array(
'controller' => 'articles',
'action' => 'index',
),
array(
'controller' => 'articles',
'link' => '[a-z0-9]{1}([a-z0-9\-]{2,}[a-z0-9]{1})?',
// Define what should be passed to the 'index' action as arguments
'pass' => array('link'),
/**
* Define what parameters should be automatically maintained
* when creating URLs/links
*/
'persist' => array('page_id', 'link'),
)
);
打开http://example.com/potc时,您将被带到Movies :: view('potc')
打开http://example.com/potc/articles时,您将被带到Articles :: index('potc')
当打开http://example.com/potc/articles/page:2时,您还将被带到Articles :: index('potc')。 “页面”将作为命名参数
传递打开http://example.com/potc/articles/99时,您将被带到文章:: view(99,'potc')
在您的文章/ index.ctp视图中:
debug($this->Html->link('current page', array()));
// outputs:
// <a href="/potc/articles/">current page</a>
debug($this->Html->link('next page', array('page' => 2)));
// outputs:
// <a href="/potc/articles/page:2">next page</a>
debug($this->Html->link('view article', array('action' => 'view', 'page_id' => 99)));
// outputs:
// <a href="/potc/articles/99">view article</a>
访问分页链接(http://example.com/potc/articles/page:2/sort:title/dir:desc)
debug($this->request);
输出:
object(CakeRequest) {
params => array(
'plugin' => null,
'controller' => 'articles',
'action' => 'index',
'named' => array(
'page' => '2',
'sort' => 'title',
'dir' => 'desc'
),
'pass' => array(
(int) 0 => 'potc'
),
'link' => 'potc',
(int) 0 => 'page:2/sort:title/dir:desc',
'paging' => array(
'Article' => array(
'page' => (int) 1,
'current' => (int) 0,
'count' => (int) 3,
'prevPage' => false,
'nextPage' => false,
'pageCount' => (int) 1,
'order' => array(),
'limit' => (int) 10,
'options' => array(
'page' => (int) 2,
'sort' => 'title',
'order' => array(),
'conditions' => array()
),
'paramType' => 'named'
)
),
'models' => array(
'Article' => array(
'plugin' => null,
'className' => 'Article'
)
)
)
data => array()
query => array()
url => 'potc/articles/page:2/sort:title/dir:desc'
base => ''
webroot => '/'
here => '/potc/articles/page:2/sort:title/dir:desc'
}
如您所见,您需要的所有信息都在请求对象
中我看到你在这条路线中使用':id':
/:link/:controller/:action/:id
但是你没有包含正则表达式。 另外,我认为你不想使用这条路线,因为这条路线要匹配, 您必须在网址中加入操作。例如:
/test-example/articles/view/1
这条路线的一个更好的选择,就是我原来的例子中的这条路线:
/:link/:controller/:page_id
这将路由到:
Controller::view('page_id', 'link')
正如我之前提到的,添加路线的'顺序'决定首先匹配的是什么, 因此,如果您在包含CakePHP默认路由之前添加了路由,则首选路由。
在这种情况下; /测试实例/ /页/
将始终与'/:link'匹配,因此如果您传递有效的控制器名称(!),则/ pages / view / test-example,也 为了防止这种情况,您应该在包含CakePHP默认路由后添加路由。
如果添加路线 /:链路/:控制器/
然后这个网址: /测试实例/东西
如果'某事'是已知控制器
,仅匹配/:link/:controller/:action/:id
如果'controller'是一个已知的控制器将匹配,任何动作都将被'接受',但是 如果您尝试访问它,将导致“未找到异常”。另外(如上所述) 你应该为'id'
添加一个正则表达式您添加的最后一条路线:
Router::connect('/:link/:controller/*', array(), array(
'link' => '[a-z0-9]{1}([a-z0-9\-]{2,}[a-z0-9]{1})?',
));
缺少默认的'action'参数,因此CakePHP可能无法匹配, 这应该是:
Router::connect('/:link/:controller/*',
array(
'action' => 'index',
), array(
'link' => '[a-z0-9]{1}([a-z0-9\-]{2,}[a-z0-9]{1})?',
));
这样,它将匹配:
/any-link/controllername/pass1
/any-link/controllername/pass1/pass2
etc.
将'pass1,pass2'作为参数发送到控制器, 例如:
/test-example/articles/some/thing/else
将路由到:
ArticlesController::index('some', 'thing', 'else');
我的路线最好的顺序是:
注意最后两条路线的顺序; 如果'id'是数字值,路由3将仅匹配,从而导致'view'操作被路由 路径4将匹配控制器之后的“任何”,并将显示控制器的“索引”动作
答案 2 :(得分:0)
编辑:这是一个经过编辑的答案,当我意识到我的最后一个答案是有缺陷的,当涉及到ArticlesConntroller :: redirect
对于任何能够设法阅读整个问题/评论并关心知道答案的人。我想出了一个解决方案,感谢Jeztah的帮助,我将把这个奖励给予我。给我带来困难的一个问题是页面可能有2个URL。例如:http://www.domain.com/potc或http://www.domain.com/movies/106。 基本上我使用了我和Jeztah的想法的组合。
首先,我必须在 routes.php 中执行检查,以查看.com之后的第一个参数是否与网页的快捷方式“链接”匹配(例如:http://www.domain.com/potc = potc )。
// get params of URL
$requestURI = explode('/', $_SERVER['REQUEST_URI']);
$scriptName = explode('/',$_SERVER['SCRIPT_NAME']);
for($i= 0;$i < sizeof($scriptName);$i++) {
if ($requestURI[$i] == $scriptName[$i]) {
unset($requestURI[$i]);
}
}
$param = array_values($requestURI);
// only concerned about first param
$param1 = $param[0];
// do the following routes if url != http://www.domain.com/users/* or http://www.domain.com
if($param1 != "users" && $param1){
// convert http://www.domain.com/assassins-creed to "assassins creed"
$pagelink = str_replace('-', ' ', $param1);
$pagesModel = ClassRegistry::init('Page');
// check if there is a page whose "link" matches converted link, if so, get the field "id"
$matched_page_id = $pagesModel->field('id', array('Page.link LIKE' => "$pagelink"));
// if there is a match, route as follows
if($matched_page_id){
// Jeztah's way with some modifications
// domain.com/potc/articles/edit/1
Router::connect(
'/:link/:controller/:action/:id',
array('page_id' => $matched_page_id, 'category' => 0),
array(
'pass' => array('id', 'link', 'category', 'page_id'),
'link' => '[a-z0-9]{1}([a-z0-9\-]{2,}[a-z0-9]{1})?',
'persist' => array('link'),
'id' => '[0-9]+'
)
);
// domain.com/potc/articles/1
Router::connect(
'/:link/:controller/:id',
array('action' => 'view', 'page_id' => $matched_page_id, 'category' => 0),
array(
'pass' => array('id', 'link', 'category', 'page_id'),
'link' => '[a-z0-9]{1}([a-z0-9\-]{2,}[a-z0-9]{1})?',
'persist' => array('link'),
'id' => '[0-9]+'
)
);
// domain.com/potc/articles/add
Router::connect(
'/:link/:controller/:action',
array('page_id' => $matched_page_id, 'category' => 0),
array(
'pass' => array('link', 'category', 'page_id'),
'link' => '[a-z0-9]{1}([a-z0-9\-]{2,}[a-z0-9]{1})?',
'persist' => array('link'),
'action' => 'add|uncategorized',
)
);
// domain.com/potc/articles OR domain.com/articles/page:2
Router::connect(
'/:link/:controller/*',
array('action' => 'index', 'page_id' => $matched_page_id, 'category' => 0),
array(
'pass' => array('link', 'category', 'page_id'),
'link' => '[a-z0-9]{1}([a-z0-9\-]{2,}[a-z0-9]{1})?',
'persist' => array('link'),
)
);
} // end if first param matches a page's "link" shortcut
} // end if first param is not users and is not empty
// do some general site routing
// do some general site routing
// now route for page's that do not have a link shortcut (example: http://www.domain.com/movies/106)
// domain.com/movies/106/articles/edit/1
Router::connect(
'/:category/:page_id/:controller/:action/:id',
array('link' => '0'),
array(
'pass' => array('id', 'link', 'category', 'page_id'),
'page_id' => '[0-9]+',
'id' => '[0-9]+'
)
);
// domain.com/movies/106/articles/1
Router::connect(
'/:category/:page_id/:controller/:id',
array('action' => 'view', 'link' => '0'),
array(
'pass' => array('id', 'link', 'category', 'page_id'),
'page_id' => '[0-9]+',
'id' => '[0-9]+'
)
);
// domain.com/movies/106/articles/add
Router::connect(
'/:category/:page_id/:controller/:action',
array('link' => '0'),
array(
'pass' => array('link', 'category', 'page_id'),
'page_id' => '[0-9]+',
'action' => 'add|uncategorized'
)
);
// domain.com/movies/106/articles OR domain.com/movies/106/articles/page:2
Router::connect(
'/:category/:page_id/:controller/*',
array('action' => 'index', 'link' => '0'),
array(
'pass' => array('link', 'category', 'page_id', 'controller'),
'page_id' => '[0-9]+'
)
);
因为在以下链接路由和:类别路由中都传递了page_id,所以我没有必要按照建议获得页面ID的组件。
现在在我的 ArticlesController
中public function index($link = null, $category = null, $page_id = null, $controller = null) {
if($page_id){
// display all articles for a particular page if on
// http://www.domain.com/potc/articles or http://www.domain.com/movies/106/articles
$this->set('articles', $this->paginate('Article', array('Article.page_id' => $page_id, 'Article.status <= 5')));
}
else{
// display all articles if on http://www.domain.com/articles
$this->set('articles', $this->paginate('Article', array('Article.status <= 5')));
}
} // end index function
public function view($id = null, $link = null, $category = null, $page_id = null) {
$this->Article->id = $id;
$article_page = $this->Article->field('page_id', array('id =' => $id));
if (!$this->Article->exists()) {
$this->Session->setFlash('This article does not exist');
// using PageLinkRedirect function of my custom component,
// determine where to redirect. Either to "potc/articles" or
// "movies/106/articles";
$this->redirect('../'.$this->CustomPage->PageLinkRedirect($link, $category, $page_id).'/articles', null, true);
}
elseif($article_page != $page_id) {
$this->Session->setFlash('Invalid article for this page');
// using PageLinkRedirect function of my custom component,
// determine where to redirect. Either to "potc/articles" or
// "movies/106/articles";
$this->redirect('../'.$this->CustomPage->PageLinkRedirect($link, $category, $page_id).'/articles', null, true);
}
else{
$this->set('title_for_layout', $this->Article->field('title', array('id =' => $id)));
$this->set('article', $this->Article->read(null, $id));
$this->set('comments', $this->paginate($this->Article->Comment, array('Comment.module_id' => $this->viewVars['module_id'], 'Comment.post_id' => $id)));
} // end else
} // end view function
我的分页存储在一个元素中,但我将元素加载到文章index.ctp视图的底部
<?php echo $this->element('all/pagination', array('pagination_url' => 'articles')); ?>
我会为每个不同的控制器放置相同的代码,但更改“文章”一词(例如:“评论”,“新闻/主题”等)
我的pagination.ctp元素看起来像这样
<?php
if($this->params['paging'][key($this->params['paging'])]['pageCount'] > 1){
$pagination_class = "pagination_enabled";
}
else{
$pagination_class = "pagination_disabled";
}
?>
<div class="<?php echo $pagination_class; ?>">
<?php echo $this->Paginator->counter(array(
'format' => __('Page {:page} of {:pages}, showing {:current} records out of {:count} total, starting on record {:start}, ending on {:end}')
));
?>
<ul>
<?php
// $link is set in AppController from the link param, same with category and page_id
if($link){
$this->Paginator->options = array('url' => array('controller' => null, $link, $pagination_url));
// This outputs http://www.domain.com/potc/articles where articles
// is from the passed $pagination_url when I loaded the element
// in my Articles index.ctp view. I have to set controller = null
// or else the link will output as http://www.domain.com/articles/potc/articles
}
else{
$this->Paginator->options = array('url' => array('controller' => null, 'link' => null, $category, $page_id, $pagination_url));
// This outputs http://www.domain.com/movies/106/articles where articles
// is from the passed $pagination_url when I loaded the element
// in my Articles index.ctp view. I have to set controller = null AND
// link = null or else the link will output as
// http://www.domain.com/articles/0/movies/9/articles
}
echo $this->Paginator->prev('< ' . __('previous'), array('tag' => 'li'), null, array('class' => 'prev disabled'));
if($this->Paginator->numbers){echo $this->Paginator->numbers(array('tag' => 'li', 'separator' => ''));}
echo $this->Paginator->next(__('next') . ' >', array('tag' => 'li'), null, array('class' => 'next disabled'));
?>
</ul></div>