开发者

Zend: route on 'root' level

I'm currently working with Zend for my project. This project had categories. Usually, I'd rewrite the URL's with Zend routes so I can reach my page as following: http://site.ext/category/[category-name]/. But for SEO purposes, I'd like to create root-l开发者_开发百科evel URL's. In other words: http://site.ext/[category-name]/.

But ofcourse, Zend will try to find a controller that's called [category-name]. What is the best way to get around this problem? I've thought of something like a 'fallback controller'. In case the page isn't found, let the callback controller handle the request and check if the category exists. If not: proceed to the error controller.

Is this the best solution, and what is the opinion of my fellow programmers?

Best regards,

Martijn


I've done essentially what you've suggested. I extended Zend_Controller_Action and defined my routes at runtime in the init()

Technically, it worked fine. Whether it was efficient, or the best possible solution, I don't know, but it worked well for me.


Don't use the Router's default routes - remove them and set up your own.


Finally went for a front controller plugin, this is a snippet of the code I use currently.

<?php
class App_Controller_Plugin_Seo extends Zend_Controller_Plugin_Abstract
{
    public function preDispatch( Zend_Controller_Request_Abstract $request )
    {
    // Retreive request params (module / controller / action / <params>)
    $params = $request->getParams( );

    // Initiate categories model
    $providers = new Application_Model_DbTable_Providers;

    // Lookup provider
    $provider = $providers->getProviderByUrl( $params[ 'controller' ] );

    // If the provider exists
    if ( ! is_null( $provider ) ) {

        // Rewrite request
        $request->setModuleName( 'default' )
                ->setControllerName( 'provider' )
                ->setActionName( 'view' )
                ->setParams(
                    array(
                        'url' => $params[ 'controller' ]
                    )
                );

        return;

    }

    return;
}
}
?>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜