Love Fuel?    Donate

FuelPHP Forums

Ask your question about FuelPHP in the appropriate forum, or help others by answering their questions.
Routing issue
  • Hi guys, I have a couple issues. My routing looks like this currently:
     'news/:category/:furl' => 'news/entry',
     ':category/:furl' => 'content/page',
    

    I have an admin controller which handles my admin panel. It's got a base admin controller, and then a folder which extends the base admin controller. Whenever I visit admin/login/ in my browser, it tries to load content/page instead of the admin's login controller. I have many admin controllers... how can I route the admin requests to the admin controller?
  • Harro Verton wrote on Friday 1st of July 2011:
    Ok, I understand. I've just expanded my routes to
    'admin/index(.*)?' => 'welcome/admin/$1',
    
     ':this/:that' => 'welcome/index',
    
    and still welcome/admin is called, the second route doesn't have any influence. You will have to add the admin route manually, as this last rule will capture any URI, and autorouting no longer works.
    'admin/login(.*)?' => 'admin/login/$1' does indeed work. However, 'admin/:controller(/.*)?' => 'admin/$1$2' does not. I really need the latter.
  • 'admin/:controller(/.*)?' => 'welcome/admin/$1',
    
    works without problems here. With URI 'admin/something/test/one/two' I get
    // from $this->params():
    array
      'controller' => string 'something' (length=9)
    
    // from Uri::segments()
    array
      0 => string 'admin' (length=5)
      1 => string 'something' (length=9)
      2 => string 'test' (length=4)
      3 => string 'one' (length=3)
      4 => string 'two' (length=3)
    

    What is it exactly that doesn't work?
  • Harro Verton wrote on Wednesday 6th of July 2011:
    'admin/:controller(/.*)?' => 'welcome/admin/$1',
    
    works without problems here. With URI 'admin/something/test/one/two' I get
    // from $this->params():
    array
      'controller' => string 'something' (length=9)
    
    // from Uri::segments()
    array
      0 => string 'admin' (length=5)
      1 => string 'something' (length=9)
      2 => string 'test' (length=4)
      3 => string 'one' (length=3)
      4 => string 'two' (length=3)
    

    What is it exactly that doesn't work?

    It seems having 'admin/$1$2' on the right side was causing the issue. Simply replacing this with 'admin/$1' caused it to start working.
  • Okay, this still doesn't work if I visit, say, admin/news/create. I have admin/news.php with action_create() inside of it, but when I visit that URL it just shows me the page at admin/news/. Any ideas?
  • With what routes? with the above route, welcome/admin/create will be called, with :controller = news. Fuel only checks for a controller/method match is the request isn't captured by a route. So if you have any route that captures admin/news/create, the fact that you have a news controller with an action_create() method is no longer relevant.
  • Harro Verton wrote on Thursday 7th of July 2011:
    With what routes? with the above route, welcome/admin/create will be called, with :controller = news. Fuel only checks for a controller/method match is the request isn't captured by a route. So if you have any route that captures admin/news/create, the fact that you have a news controller with an action_create() method is no longer relevant.

    That makes sense. So what's the best way to do what I'm trying to achieve? Do I need to specify routes for each of my admin controllers?
  • I honestly can't say. The way routing works is that the URI is matched against each of the routes, in the sequence in which they are defined. If there is no match at the end, autodetection is performed. If that fails to, you'll get a 404. It is up to you, and your specific situation, to define the routes that do what you want.
  • Harro Verton wrote on Thursday 7th of July 2011:
    I honestly can't say. The way routing works is that the URI is matched against each of the routes, in the sequence in which they are defined. If there is no match at the end, autodetection is performed. If that fails to, you'll get a 404. It is up to you, and your specific situation, to define the routes that do what you want.

    It looks like this is the routing I needed to get things working the way I wanted:
     'admin/login(/.*)?' => 'admin/login/$1',
     'admin/:controller' => 'admin/$1',
    
  • Well, since you made a wildcard route, you're always matching those first 2 uri segments to /content/page. Why don't you add another route for admin with a wildcard? 'admin/:controller' => 'admin/$1'
  • I've tried the following, but it doesn't work: 'admin/(:any)' => 'admin/$1' The problem is it's not always going to be a single segment. Any ideas? 'admin/:controller' => 'admin/$1' doesn't work either.
  • It doesn't work for all use cases or this one in particular? Did you place this rule above or below that other wildcard?
  • Use a regex to capture the (optional) rest of the URI:
    'admin/:controller(/.*)?' => 'admin/$1$2'
    
  • Calvin Froedge wrote on Thursday 30th of June 2011:
    It doesn't work for all use cases or this one in particular? Did you place this rule above or below that other wildcard?

    Tried both above and below my other routes. Neither one works.
  • What is the exact URL you're trying to match? Currently we're guessing to what doesn't work...
  • The url is admin/login currently. It doesn't work. If someone isn't logged in, and they try to visit part of my admin section, they get redirected to admin/login/original/path/goes/here. Everything after /login/ is used to redirect them again after logging in.
  • Ok, so:
    - your controller is admin, method is index
    - you want to keep all other URI segments to be able to redirect back
    - you want a route that captures that I'll see if I can test this...
  • Actually the controller is technically admin/login. I have an admin folder in my controllers directory. I do have an admin controller with an index method for admin/, but admin/login is actually in admin/login.php. It of course extends Controller_Admin, etc. Basically I just need a catch-all route that will grab the first URI segment as "category" and the 2nd as "furl". This will be used to redirect any non-matching routes to the content section, where I am querying the database (eventually, cache) to see if there is an actual page there. If there is, it will show it. If not, 404.
  • Just added an action_admin() method to the welcome controller of a fresh Fuel install. Added this route: 'admin/index(.*)?' => 'welcome/admin/$1', And finished with '(:any)' => 'welcome/404' to disable any fall-though or autodetect behaviour. A URI like admin/index/test/one/two loads the new admin method without problems, and URI::segments() gives me all segments in the URI. So I can't reproduce your issue here.
  • Harro Verton wrote on Thursday 30th of June 2011:
    Just added an action_admin() method to the welcome controller of a fresh Fuel install. Added this route: 'admin/index(.*)?' => 'welcome/admin/$1', And finished with '(:any)' => 'welcome/404' to disable any fall-though or autodetect behaviour. A URI like admin/index/test/one/two loads the new admin method without problems, and URI::segments() gives me all segments in the URI. So I can't reproduce your issue here.

    I don't think you understand my issue. My issue is not that the admin routing doesn't work - it does... even without defining it as a route. However, my actual problem is when I add the following route, the admin routing no longer works period. ':category/:furl' => 'content/page',
  • Ok, I understand. I've just expanded my routes to
    'admin/index(.*)?' => 'welcome/admin/$1',
    
     ':this/:that' => 'welcome/index',
    
    and still welcome/admin is called, the second route doesn't have any influence. You will have to add the admin route manually, as this last rule will capture any URI, and autorouting no longer works.

Howdy, Stranger!

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

In this Discussion