Zend Framework Routing: .html extension

可紊 提交于 2019-12-04 19:13:48

问题


I know I've seen this done before but I can't find the information anywhere. I need to be able to route with .html extensions in the Zend Framework.

I.E. /controller/action.html should route to the appropriate controller / action.

We have an idea to throw away the .html extension with our .htaccess file but I think changing the route config would be the better solution.

Any advice is welcome.


回答1:


A quick search on google yielded the following tutorials:

Extending Zend Framework Route and Router for custom routing
Routing and complex URLs in Zend Framework




回答2:


This is the plugin I've used in several applications:

/**
 * Removes .html extension from URI, if present.
 */
class Application_Plugin_RemoveHtmlExtension extends Zend_Controller_Plugin_Abstract
{
    public function routeStartup(Zend_Controller_Request_Abstract $request)
    {
        // remove ".html" from the end of the URI
        $url = preg_replace('#\.html$#i', '', $request->getRequestUri());

        $request->setRequestUri($url);
    }
}



回答3:


I was trying to do the same for an old application. Here is what worked for me.

$front = Zend_Controller_Front::getInstance();
$router = $front->getRouter();
$router->addRoute('routeHTML', new Zend_Controller_Router_Route_Regex( '([a-z-]+)/([a-z-]+)/([a-z-]+)\.html', array(),
        array(1 => 'module', 2 => 'controller', '3' => 'action') ,
        '%s/%s/%s.html')
);



回答4:


The default route (without modules) is:

:controller/:action

Which you can remove by:

$router->removeDefaultRoutes();

Then add your version:

:controller/:action.html


来源:https://stackoverflow.com/questions/472606/zend-framework-routing-html-extension

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