Atu*_*vid 5 php routing cakephp url-rewriting
我在我的一个项目中使用cakephp,我的客户希望站点URL以.html而不是通常友好的URL结尾.我想知道在cakephp中是否可以通过它的任何路由技术来实现.请帮忙.
ban*_*cer 12
这在食谱中有很好的记载.
更新:http://book.cakephp.org/2.0/en/development/routing.html#file-extensions
要使用您的路由处理不同的文件扩展名,您需要在路由配置文件中添加一行:
Router::parseExtensions('html', 'rss');
Run Code Online (Sandbox Code Playgroud)
如果您要创建一个网址,例如/page/title-of-page.html,您将创建您的路线,如下所示:
Router::connect(
'/page/:title',
array('controller' => 'pages', 'action' => 'view'),
array(
'pass' => array('title')
)
);
Run Code Online (Sandbox Code Playgroud)
然后创建映射回路由的链接只需使用:
$this->Html->link(
'Link title',
array('controller' => 'pages', 'action' => 'view',
'title' => 'super-article', 'ext' => 'html')
);
Run Code Online (Sandbox Code Playgroud)
您可以发送到Router :: url()的参数之一(由HtmlHelper :: link()和Controller :: redirect()等其他方法调用)是'ext'.尝试将其设置为"html".例如:
echo $this->Html->link('Products', array('controller' => 'products', 'action' => 'index', 'ext' => 'html'));
Run Code Online (Sandbox Code Playgroud)
要么
$this->redirect(array('controller' => 'products', 'action' => 'index', 'ext' => 'html'));
Run Code Online (Sandbox Code Playgroud)
如果它工作,尝试找出一种方法,你可以覆盖Router :: url()默认添加它.
必须在不使用路由的情况下解决这个问题。保留页面的默认路由条目:
Router::connect('/pages/*', array('controller' => 'pages', 'action' => 'display'));
Run Code Online (Sandbox Code Playgroud)
并在显示操作中删除 .html 扩展名并呈现相应的视图:
preg_replace('/\.html$/','',$view);
$this->render(null,'default',$view);
Run Code Online (Sandbox Code Playgroud)
调用页面时将“ext”添加为 .html
归档时间: |
|
查看次数: |
4775 次 |
最近记录: |