Zend Framework Routing: .html extension

2019-03-26 01:35发布

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.

4条回答
来,给爷笑一个
2楼-- · 2019-03-26 02:02

The default route (without modules) is:

:controller/:action

Which you can remove by:

$router->removeDefaultRoutes();

Then add your version:

:controller/:action.html
查看更多
Melony?
3楼-- · 2019-03-26 02:11

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')
);
查看更多
聊天终结者
5楼-- · 2019-03-26 02:18

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);
    }
}
查看更多
登录 后发表回答