Love Fuel?    Donate

FuelPHP Forums

Ask your question about FuelPHP in the appropriate forum, or help others by answering their questions.
Problems with routing and multiple variables
  • Hi,
    I'm trying to route something like this:
    'admin/(:segment)/(:any)' => '$1/admin/$2' (something I used on CI based on Phill Sturgeon's code) The idea is
    'admins/users/user' be routed to '/users/admin/user'
    or 'admins/users/user/parameters' be routed to '/users/admin/user/parameters' but the path I'm getting is only "users/admin/" Any ideas on what I'm doing wrong? Thanks
  • What are you exactly getting where? How do you check what the path is you're getting?
  • Hi WanWizard,
    I dumped the $path variable in core/route/matched to try to figure it out what was the problem For some reason the route works fine if I replace
    'admin/(:segment)/(:any)' => '$1/admin/$2'
    with
    'admin/(:segment)/(:any)' => '$1/admin/$3' // Note the $3 here instead of $2 Using this 'admins/users/user' is routed to '/users/admin/user' and admins/users/user/parameters' is routed to '/users/admin/user/parameters'. Now the problem is that this makes no sense to me. Thanks for your reply.
  • Issue seems to be the regex that is used to process ":segment". If you dump Request::main(), you'll see
    public 'route' => 
        object(Fuel\Core\Route)[3]
          public 'segments' => 
            array
              0 => string 'test' (length=4)
              1 => string 'index' (length=5)
          public 'named_params' => 
            array
              empty
          public 'method_params' => 
            array
              empty
          public 'path' => string 'admin/(:segment)/(:any)' (length=23)
          public 'module' => null
          public 'directory' => null
          public 'controller' => string 'test' (length=4)
          public 'action' => string 'index' (length=5)
          public 'translation' => string '$1/index/$2' (length=11)
          protected 'search' => string 'admin/([^/]+([^/]*))/(.+)' (length=25)
    

    As you can see, ":segment" is replaced by "([^/]+([^/]*))", which contains a double set of parentheses, giving you $1 and $2 (and because of that, the next one, your ":any", is $3.) Added to the bugtracker: https://github.com/fuel/core/issues/33.
  • In the Route class, if you replace the compile statement by
    $this->search = str_replace(array(':any', ':segment'), array('.+', '[^/]*'), $this->path);
    
    $2 works again, and the extra URI parameters will end up in the params array. If you can confirm this works, I'll commit the fix later.
  • I'm closing this thread. Please provide feedback about the proposed fix, or any other comments, via the issue tracker: https://github.com/fuel/core/issues/33.
This discussion has been closed.
All Discussions

Howdy, Stranger!

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

In this Discussion