Zend Framework's Rewrite Router

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
Luke
The Ninja Space Mod
Posts: 6424
Joined: Fri Aug 05, 2005 1:53 pm
Location: Paradise, CA

Zend Framework's Rewrite Router

Post by Luke »

I've got the following route set up...

Code: Select all

$routes['rotary_calendar'] = new Zend_Controller_Router_Route(
    'calendar/:year/:month',
    array('controller' => 'event', 'action' => 'calendar', 'year' => $date['year'], 'month' => $date['mon']),
    array('year' => '/d+', 'month' => '/d+')
);
Now this works fine if you enter in http://www.site.com/calendar (it calls the correct controller, action and sets the default params), but if you try and enter parameters manually such as /calendar/2008/4, it tries to call "calendar" as a controller. This is not what I want... I want it to find this route and call event as the controller and calendar as the action, and then set 2008 as year and 4 as month. Does anybody know what I'm doing wrong?
User avatar
CoderGoblin
DevNet Resident
Posts: 1425
Joined: Tue Mar 16, 2004 10:03 am
Location: Aachen, Germany

Post by CoderGoblin »

First off, don't know the answer and I think you are probably more familiar with this than I am but ...

1) What happens though if you take array the validation array?
2) What happens f you try with just a single parameter ?
User avatar
Jenk
DevNet Master
Posts: 3587
Joined: Mon Sep 19, 2005 6:24 am
Location: London

Post by Jenk »

It must be tripping onto the final route ('/:controller/:action') if it is calling calendar as the controller.

Try adding a var_dump or echo into the router source to see what it trips on.
User avatar
Luke
The Ninja Space Mod
Posts: 6424
Joined: Fri Aug 05, 2005 1:53 pm
Location: Paradise, CA

Post by Luke »

Code: Select all

[_requestUri:protected] => /Rotary/calendar/2007/3
                    [_baseUrl:protected] => /Rotary
                    [_basePath:protected] => 
                    [_pathInfo:protected] => /calendar/2007/3
                    [_params:protected] => Array
                        (
                            [controller] => calendar
                            [action] => 2007
                            [3] => 
                        )

                    [_aliases:protected] => Array
                        (
                        )

                    [_dispatched:protected] => 1
                    [_controllerKey:protected] => controller
                    [_actionKey:protected] => action
is that what you mean? That doesn't really tell me anything more than you did though (that it's using the last route). Hmm...
User avatar
Luke
The Ninja Space Mod
Posts: 6424
Joined: Fri Aug 05, 2005 1:53 pm
Location: Paradise, CA

Post by Luke »

:roll: I could have sworn I tried this before, but I changed it to this and now it works.

Code: Select all

$routes['rotary_calendar'] = new Zend_Controller_Router_Route(
    'calendar/:year/:month',
    array('year' => $date['year'], 'month' => $date['mon'], 'controller' => 'event', 'action' => 'calendar'),
    array('year' => '[0-9]{2,4}', 'month' => '[0-9]{1,2}')
);
User avatar
xinnex
Forum Commoner
Posts: 33
Joined: Sun Jan 07, 2007 7:38 pm
Location: Copenhagen, Denmark

Post by xinnex »

Sorry if I'm hijacking a thread here, but you seem to have cracked these router-things..
Have you tapped into the module-functionality with routers yet?

I'm trying to route http://mysite.com/forum to a module called "SimpleForum".. ;

Code: Select all

/application
 /default
 /SimpleForum
  /controllers
  /views
etc.
User avatar
Luke
The Ninja Space Mod
Posts: 6424
Joined: Fri Aug 05, 2005 1:53 pm
Location: Paradise, CA

Post by Luke »

module-functionality? :?

never heard of it, but I just briefly noticed it in the manual. Maybe something like:

Code: Select all

$router->addRoute(
    'forum',
    new Zend_Controller_Router_Route('forum/:controller/:action', array('module' => 'SimpleForum', 'controller' => 'index', 'action' => 'index')
);
User avatar
xinnex
Forum Commoner
Posts: 33
Joined: Sun Jan 07, 2007 7:38 pm
Location: Copenhagen, Denmark

Post by xinnex »

Thanks, I'll give it a shot..

(btw.. I'm talking about modules as in mvc-packages.. It's on in the list you posted about ZF 0.8.0)

Edit: For others struggling with modules and routers, I just realized that the manual has been updated, so much for bleeding edge...
http://framework.zend.com/manual/en/zen ... riterouter
User avatar
Jenk
DevNet Master
Posts: 3587
Joined: Mon Sep 19, 2005 6:24 am
Location: London

Post by Jenk »

Just noticed..

Code: Select all

array('year' => '/d+', 'month' => '/d+')
should be:

Code: Select all

array('year' => '\d+', 'month' => '\d+')
:lol:
Post Reply