Love Fuel?    Donate

FuelPHP Forums

Ask your question about FuelPHP in the appropriate forum, or help others by answering their questions.
Module Routes
  • Hi Everyone, 

    I'm having a bit of an issue with module routes, essentially the issue is this. 

    I have an application with but nothing in it essentially, all code is in two modules. I'm using these as a way to separate code, these two modules are essentially frontend and admin code. 

    So anyway in my application I have the following routes...

    /app/config/routes.php
    ==============
    root => /frontend/content/home
    error => /frontend/mycontroller/error

    I also have several other routes in my frontend module, but these don't appear to work. For instance I have a route in: /app/modules/frontend/config/routes.php
    about => content/pages/about

    But it doesn't work when I go to /about on my application, it does work if I go to /frontend/about. I've tried adding (:any) => /frontend/$1 but this doesn't work, can anyone tell me how to get a setup like this working? It looks as if my route /frontend/$1 works if I have a controller for this, e.g. if I had a controller called "about" but this isn't what I wanted, I was hoping that this would route to /frontend/about and this would be dealt with by the routes in my /frontend module.

    regards

    James
  • I was thinking I could probably do this with the inline routes example, however I'm unsure as to how I could pass the parameter across there.

    e.g. I would have soemthing like this in my main application

    return array(
    '(:any)' => function () {
    return \Request::forge('frontend/$1', false)->execute();
    };
  • I figured it out now, if anyone is wanting to do this.... 

    I added this into my main application routes, 

    '(:any)' => function()
    {
         $module_route = \Uri::string();
         $method = \Request::main()->get_method();
         return \Request::forge('frontend/'.$module_route)->set_method($method)->execute()->response();
    },

    If someone could let me know if they see a potential issue with this that'd be great.
  • Module routes are only loaded when the module is active. This means that all routes in a module must start with the name of the module.
  • I think you can achieve the same using

    '(:any)' => 'frontend/$1'
  • Harro, 

    '(:any)' => 'frontend/$1'

    Doesn't work unless the controller exists, so for instance if you have the route 
    'about' => 'frontend/content/pages/about' 

    adn tried to replace it with the any one you gave, it will try to redirect the response to a controller called "about" in frontend module. 

    The only way I could do this is with function I mentioned previously, but as this appears to work that's great.
  • No, then it will indeed not work, because routes are not processed recursively.

    Which in your case will be solved by a second Request, which will process the routes again. It just comes across as quite heavy, basically doubling the amount of requests your server has to process.

    Perhaps it's an option to put a bit more intelligence in the routing system?

    In your app/config/routes.php, call a static class method that loads the routes.php from your module (or modules), merge them, and return them instead of the static array?

    Or alternatively, inject routes directly, using \Router::add(). If you do that before the request gets routed, your dynamic routes will used.

Howdy, Stranger!

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

In this Discussion