开发者

Zend Framework: How to map fully dynamic route

The title of this question was hard开发者_Go百科 to come up with, it might not be the best, anyway.

I have a site with regions, categories and suppliers, the obvious solution is to use the default route of

"/:module/:controller/:action"

So that my URLs will look something like this

"/region/midlands/category/fashion"
"/region/midlands/supplier/ted-baker"

What I want to achieve is a URL format like this however, this would need to involve a database query to check for the existence of midlands, fashion and ted-baker

"/midlands/fashion"
"/midlands/ted-baker"

My original solution was to use something like this

"/region/midlands/fashion"

With a route defined as

routes.category.route = "/region/:region/:category"
routes.category.defaults.controller = category
routes.category.defaults.action = index
routes.category.defaults.module = default
routes.category.defaults.category = false
routes.category.defaults.region = false

routes.supplier.route = "/supplier/:supplier"
routes.supplier.defaults.controller = supplier
routes.supplier.defaults.action = index
routes.supplier.defaults.module = default
routes.supplier.defaults.supplier = false

But that means prefixing everything with region or supplier. I almost need to hijack the request completely with a plug in?

What is the best way of achieving this?

Thanks for any help.

Edit.

@St.Woland, the problem is that I want this route

/:region/:supplier

To work with this URL

/midlands/ted-baker

But that route effectively overrides the default router


The best way is to add a method into your Bootstrap class like this:

<?php
class Bootstrap extends Zend_Application_Bootstrap_Bootstrap
{
    protected function _initMyRoutes()
    {
        // First, initialize Database resource
        $this->bootstrap('db');
        // Second, initialize Router resource
        $this->bootstrap('router');

        // Finally, instantiate your database table with routes
        $m_routes = new Model_Routes() ;

        // Now get the Router
        $router = $this->getResource('router');

        // ... and add all routes from the database
        foreach( $m_routes->fetchAll() as $route ) {
            $router->addRoute( $route->name, new Zend_Controller_Router_Route( $route->path, $route->toArray() ) ) ;
        }
    }
}

Then in your application.ini:

[production]
bootstrap.path = APPLICATION_PATH/Bootstrap.php
bootstrap.class = Bootstrap

This will initialize the router with routes from the database.

You should keep in mind, that queing the database upon every request is not efficient, so make sure to use cache.


I have used the ErrorController to do this in the end. I catch the Exception where there is controller or no route and then act accordingly.

There are calls made to a few database tables for the specific route which cannot be found rather than fetching all as in St.Woland's solution. The results are cached with tags which helps a great deal, this removes all database queries for finding routes

public function errorAction()
{
    $errors = $this->_getParam('error_handler');

    switch ($errors->type) 
    {
        case Zend_Controller_Plugin_ErrorHandler::EXCEPTION_NO_ROUTE:
        case Zend_Controller_Plugin_ErrorHandler::EXCEPTION_NO_CONTROLLER:
            //Code for locating routes in Db goes here
    }
}
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜