Love Fuel?    Donate

FuelPHP Forums

Ask your question about FuelPHP in the appropriate forum, or help others by answering their questions.
Named routes not working as expected
  • Hi guys, I'm playing around with the named routes functionality, but they are not working as expected. The way I would expect at least .. see the example below: routes:
    ':section/:page' => 'web/route',
    

    In other words, I want to redirect all requests to the route method of my web controller. In my setup the first segment of a URL defines the section, and the rest of the URL is a unique identifier of a page. Now consider the following action_route method:
     public function action_route() 
     {
      print $this->param('section');
      print '<hr />';
      print $this->param('page');
     }
    

    Now consider the following URLs: - [url=http://localhost:8888/fuel1.0/public/my_section/my_page]http://localhost:8888/fuel1.0/public/my_section/my_page[/url] -> works as expected ( section equals 'my_section' and page equals 'my_page' ) - [url=http://localhost:8888/fuel1.0/public/my_section/my_page]http://localhost:8888/fuel1.0/public/my_section/my_page[/url]/my_subpage -> doesn't work as expected ( section equals 'my_section/my_page' and page equals 'my_sub_page' ). Instead of appending uri segments to the last parameter, the uri-segments shift to the first segment. I don't think this is intended behaviour is it? Since it would also break the example in the documents (http://fuelphp.com/docs/general/routing.html) under Advanced routing. It basically means that simply by adding an extra segment to the URLs, one can break your application. Any thoughts? -Michiel
  • Please create a ticket for this at http://dev.fuelphp.com.
  • Reading through this issue again, I don't think there's a routing issue. A route is defined as an regular expression, and ':a/:b' doesn't match 'x/y/z'. And that is why your route isn't matched.
  • I understand that, but the way it works now, leaves room for unexpected input. Imagine the blog example from the documentation: array(
    'blog/:year/:month/:id' => 'blog/entry', // Routes /blog/2010/11/entry_name to /blog/entry
    ), In the case of the exact url ' /blog/2010/11/entry_name' the params match up exactly. Now imagine that for some reason an extra segment is added to the url (a user is free to type a url in his browser): /blog/2010/11/entry_name/foo Now all of a sudden the year parameter equals "2011/11", the month parameter equals "entry_name" and id equals "foo". To me this doesn't sound very logical ..
  • A fix has been pushed to develop and master branches. Any extra URI segments are now added to the last named parameter if you haven't defined a catch for it:
    'blog/:year/:month/:id' => 'blog/entry'
    
    in case of /blog/2010/11/entry_name/foo, 'id' will now contain 'entry_name/foo' If you don't want that, work around that with a 'catch-all' in your regex:
    'blog/:year/:month/:id(|/.*)' => 'blog/entry'
    
    which will 'eat' all extra URI segments...
  • That's a very nice solution! I think this is much more logical behaviour now. And the catch all solution makes it even more elegant.
  • Harro Verton wrote on Thursday 7th of April 2011:
    'blog/:year/:month/:id(|/.*)' => 'blog/entry'
    
    which will 'eat' all extra URI segments...
    Why not
    'blog/:year/:month/:id(:any)' => 'blog/entry'
    or
    'blog/:year/:month/:id/(:any)' => 'blog/entry'
    
    , as described in manual??
  • Have you tried what that does? In both cases, 'blog/:year/:month/:id' (without any extra segments) will not be matched anymore. ":any" does not mean "anything including nothing".

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

In this Discussion