How can I append .html to all my URLs in cakephp?

寵の児 提交于 2019-11-29 11:15:45

That is well documented in the cookbook.

UPDATE: http://book.cakephp.org/2.0/en/development/routing.html#file-extensions

To handle different file extensions with your routes, you need one extra line in your routes config file:

Router::parseExtensions('html', 'rss');

If you want to create a URL such as /page/title-of-page.html you would create your route as illustrated below:

Router::connect(
    '/page/:title',
    array('controller' => 'pages', 'action' => 'view'),
    array(
        'pass' => array('title')
    )
);

Then to create links which map back to the routes simply use:

$this->Html->link(
    'Link title',
    array('controller' => 'pages', 'action' => 'view', 
          'title' => 'super-article', 'ext' => 'html')
);

One of the parameters you can send to Router::url() (which is called by other methods like HtmlHelper::link() and Controller::redirect()) is 'ext'. Try setting this to 'html'. E.g:

echo $this->Html->link('Products', array('controller' => 'products', 'action' => 'index', 'ext' => 'html'));

or

$this->redirect(array('controller' => 'products', 'action' => 'index', 'ext' => 'html'));

If it works, try figuring out a way you can override Router::url() to add it in by default.

Had to solve this without using Routes. Kept the default route entry for pages:

Router::connect('/pages/*', array('controller' => 'pages', 'action' => 'display'));

and in the display action removed the .html extension and rendered the respective view:

preg_replace('/\.html$/','',$view);
$this->render(null,'default',$view);

While calling the pages added 'ext' to be .html

µBio

According to this page you can do something like this

Router::connect('/(.*).html', array('controller' => 'pages', 'action' => 'display'));

but as you are talking about extensions, that may have other consequences.

As Routes Configuration - File extensions documentation section says, you could use:

Router::parseExtensions('html', 'rss');

This will tell the router to remove any matching file extensions, and then parse what remains.

You will need to associate the html extension to the PHP module in Apache as well. I don't remember exactly the adjustment needed but it will be in /etc/httpd/httpd.conf file. (This file may be in a slightly different place depending on your server's OS.) Just look for the line that associates .php with the PHP module. I believe you may be able to define this in the .htaccess file as well but weather or not you can depends on what you are allowed to do in the httpd.conf file.

Its quite Simple,Open file app/config/routes.php and just add

Router::parseExtensions('html', 'rss');

Above the line

 Router::connect('/', array('controller' => 'pages', 'action' => 'display', 'home'));

Now you can access even your controller methods with .html extensions .

I hope it helps .

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!