开发者

Flexible routing in Zend

I haven't used the Zend Router much yet so not sure ho开发者_StackOverflow中文版w difficult or easy this is, but I think Zend is flexible so it's got to have a way to do this easily.

So I create a controller Cont with 2 actions actone and acttwo. This naturally gives me

//the default index controller
site.com/                
site.com/index/index     

//and my controller
site.com/cont/index
site.com/cont/actone
site.com/cont/acttwo

Is there a way I can access the actone action which is in the cont controller using a route that looks like this

site.com/actone

I realize I could get this look by creating a separate controller called Actone and this would be its index action but this actone action logically belongs to the Cont controller, so I want to just give the appearance of that path.


You can create a custom route...

Via application.ini's - Router Application Resource

resources.router.routes.route_title.route               = "/actone"
resources.router.routes.route_title.defaults.controller = "cont"
resources.router.routes.route_title.defaults.action     = "actone"
resources.router.routes.route_title.type                = "Zend_Controller_Router_Route_Static"

Or by adding one directly to the router.

$router = Zend_Controller_Front::getInstance()->getRouter();    
$router->addRoute('route_title', new Zend_Controller_Rotuer_Route_Static(
    '/actone',
    array(
        'controller' => 'cont',
        'action'     => 'actone'
    )
));


If you could make your Cont controller the default controller that would probably do the trick. IIRC this is in Zend_Controller_Dispatcher_Abstract, there's setDefaultControllerName() as well as setDefaultAction() and setDefaultModule().

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜