简体   繁体   中英

Setting routes in application.ini in Zend Framework

I'm a Zend Framework newbie, and I'm trying to work out how to add another route to my application.ini file.

I currently have my routes set up as follows:

resources.router.routes.artists.route = /artists/:stub
resources.router.routes.artists.defaults.controller = artists
resources.router.routes.artists.defaults.action = display

...so that /artists/joe-bloggs uses the "display" action of the "artists" controller to dipslay the profile the artist in question - that works fine.

What I want to do now is to set up another route so that /artists/joe-bloggs/random-gallery-name goes to the "galleries" action of the "artists" controller.

I tried adding an additional block to the application.ini file (beneath the block above) like so:

resources.router.routes.artists.route = /artists/:stub/:gallery
resources.router.routes.artists.defaults.controller = artists
resources.router.routes.artists.defaults.action = galleries

...but when I do that the page at /artists/joe-bloggs no longer works (Zend tries to route it to the "joe-bloggs" controller).

How do I set up the routes in application.ini so that I can change the action of the "artists" controller depending on whether "/:gallery" exists?

I realise I'm probably making a really stupid mistake, so please point out my stupidity and set me on the right path (no pun intended).

Try reversing the order of the routes. ZF matches routes in the opposite order they are added (so that the default route is the last to be matched)

If that doesn't work, you'll probably have to investigate regex routes with optional components.

Your second block needs to have a different route name, rename the 'artists' word to something similar to this for your new block:

resources.router.routes.artists-gal.route = /artists/:stub/:gallery
resources.router.routes.artists-gal.defaults.controller = artists
resources.router.routes.artists-gal.defaults.action = galleries

I usually setup my routes in application/Bootstrap.php (or wherever your Bootstrap.php file is)

add a method like the one below:

protected function _initRoutes()
{
    $ctrl = Zend_Controller_Front::getInstance();
    $router = $ctrl->getRouter();
    $router->addRoute(
            'artist_detail',
            new Zend_Controller_Router_Route('artists/:stub',
                                            array('controller' => 'artists',
                                                  'action' => 'display'))
    ); 

    $router->addRoute(
            'artist_detail_gallery',
            new Zend_Controller_Router_Route('artists/:stub/:gallery',
                                            array('controller' => 'artists',
                                                  'action' => 'gallery'))
    );



}

As far as checking weather an specific artist has a gallery, in the case of my example, i would have a galleryAction method in the ArtistsController

do a check if a gallery exists for the 'stub' request paramater, if it doesnt throw a 404:

throw new Zend_Controller_Action_Exception("Object does not exist", 404);

or redirect them to some other page:

return $this->_helper->redirector('index', 'index'); //redirect to index action of index controller

Hope this helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM