如何将。html附加到cakephp中的所有url ?

时间:2022-03-25 21:09:37

I am using cakephp in one of my projects and my client wants the site URLs to end with .html and not the usual friendly urls. I was wondering if its possible in cakephp to do so through any of its routing techniques. Please help.

我正在我的一个项目中使用cakephp,我的客户端希望站点url以.html结尾,而不是通常的友好url。我想知道cakephp是否有可能通过它的任何路由技术来实现这一点。请帮助。

7 个解决方案

#1


12  

That is well documented in the cookbook.

这在烹饪书中有很好的记载。

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

更新:http://book.cakephp.org/2.0/en/development/routing.html文件扩展名

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:

如果您想创建一个URL,例如/page/title- page。您将创建如下所示的路径:

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')
);

#2


5  

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:

可以将其中一个参数发送到Router::url()(由其他方法调用,如HtmlHelper::link()和Controller: redirect())是'ext'。尝试将其设置为“html”。例句:

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.

如果可以,请尝试找出一种方法,可以覆盖Router::url(),以便在默认情况下添加它。

#3


2  

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:

在显示操作中删除.html扩展并呈现各自的视图:

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

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

在调用页面时,添加的“ext”为.html

#4


2  

According to this page you can do something like this

根据这个页面你可以做这样的事情。

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

路由器::连接(' /(. *)。html的阵列(“控制器”= >“页”,“行动”= >“显示”));

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

但当你谈论扩展时,可能会产生其他的后果。

#5


2  

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.

这将告诉路由器删除任何匹配的文件扩展名,然后解析剩下的内容。

#6


0  

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.

您还需要将html扩展与Apache中的PHP模块关联起来。我不记得确切需要的调整,但它将在/etc/httpd/httpd中conf文件。(根据服务器的操作系统,这个文件可能位于稍微不同的位置。)只需查找将. PHP与PHP模块关联的行。我相信您也可以在.htaccess文件中定义这一点,但是无论天气如何,您都可以依赖于您在httpd中可以做什么。conf文件。

#7


0  

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

它非常简单,打开文件应用程序/配置/路由。php和加

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 .

现在,您甚至可以使用.html扩展访问控制器方法。

I hope it helps .

我希望这能有所帮助。

#1


12  

That is well documented in the cookbook.

这在烹饪书中有很好的记载。

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

更新:http://book.cakephp.org/2.0/en/development/routing.html文件扩展名

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:

如果您想创建一个URL,例如/page/title- page。您将创建如下所示的路径:

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')
);

#2


5  

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:

可以将其中一个参数发送到Router::url()(由其他方法调用,如HtmlHelper::link()和Controller: redirect())是'ext'。尝试将其设置为“html”。例句:

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.

如果可以,请尝试找出一种方法,可以覆盖Router::url(),以便在默认情况下添加它。

#3


2  

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:

在显示操作中删除.html扩展并呈现各自的视图:

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

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

在调用页面时,添加的“ext”为.html

#4


2  

According to this page you can do something like this

根据这个页面你可以做这样的事情。

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

路由器::连接(' /(. *)。html的阵列(“控制器”= >“页”,“行动”= >“显示”));

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

但当你谈论扩展时,可能会产生其他的后果。

#5


2  

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.

这将告诉路由器删除任何匹配的文件扩展名,然后解析剩下的内容。

#6


0  

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.

您还需要将html扩展与Apache中的PHP模块关联起来。我不记得确切需要的调整,但它将在/etc/httpd/httpd中conf文件。(根据服务器的操作系统,这个文件可能位于稍微不同的位置。)只需查找将. PHP与PHP模块关联的行。我相信您也可以在.htaccess文件中定义这一点,但是无论天气如何,您都可以依赖于您在httpd中可以做什么。conf文件。

#7


0  

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

它非常简单,打开文件应用程序/配置/路由。php和加

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 .

现在,您甚至可以使用.html扩展访问控制器方法。

I hope it helps .

我希望这能有所帮助。