2009年11月12日星期四

Re: [fw-mvc] Problem with Route

-- Jigal sanders <jigalroecha@gmail.com> wrote
(on Thursday, 12 November 2009, 06:21 PM +0100):
> I am following the book Beginning Zend Framework of Armando Padilla.
> The it is suggested to create an alternative route:
>
> $Router->addRoute("artistprofile",
> new Zend_Controller_Router_Route
> (
> "artist/:artistname",
> array(
> "artistname" => "The Smits",
> "controller" => "artist",
> "action" => "profile"
> )));
>
>
> But now when i do:
>
> http://localhost/artist/new
>
> It looks for the profile action in stead of the new action.
>
> What is going wrong?

The new route takes precedence over the default route. Since you didn't
specify any requirements (a regexp) for what a valid artistname looks
like, it's taking any path segment following "artist/" as the
artistname.

You can solve this in one of two ways:

* Provide a regular expression for the "artistname" segment. This will
also require that you do not provide a default for the "artistname".
$Router->addRoute("artistprofile", new Zend_Controller_Router_Route(
"artist/:artistname",
array(
"controller" => "artist",
"action" => "profile",
),
array(
'artistname' => '[a-z0-9 _-]{4,}', // must be >= 4 characters
)
));

* Provide additional routes registered *later* for specific actions:
$Router->addRoute("artistprofile", new Zend_Controller_Router_Route(
"artist/:artistname",
array(
"artistname" => "The Smits",
"controller" => "artist",
"action" => "profile",
)
));
$Router->addRoute("newartist", new Zend_Controller_Router_Route_Static(
"artist/new",
array(
"controller" => "artist",
"action" => "new",
)
));

--
Matthew Weier O'Phinney
Project Lead | matthew@zend.com
Zend Framework | http://framework.zend.com/

没有评论: