Love Fuel?    Donate

FuelPHP Forums

Ask your question about FuelPHP in the appropriate forum, or help others by answering their questions.
routing - backend/module
  • Hello,

    I must develop an application that would be organized by module.

    I have a question about routing. in documentation, I read this :
    These routes will be loaded when Fuel detects that the first URI segment refers to a module, and only the routes of that module will be loaded.

    I would like have route like that : 
    /backend/page/create
    /backend/invoice/manage
    /backend/user/list
    ....
    And i have a module 
    • .../app/module/backend
    • ../app/module/page
    • ../app/module/invoice
    • ../app/module/user
    • ../app/module/...
    but if I understand the documentation, it's not possible, I must routes like that :
    /backend/page/create
    /backend/user/list
    /backend/invoice/manage
    ....

    Even so, is there a method to do that, to have this organization ?
    Maybe, it's possible to force the loading of the routes of all activated modules ?

    Thanks,
    ...and sorry for my english

    Philippe
  • Hi,

    Here is my tip :

    your app/config/routes.php :
        'page(/:params)' => array('myPageModule/router/index/front', 'name' => 'lb_page'),
        'page' => array('myPageModule/router/index/front', 'name' => 'lb_page_index'),
        'backend/page(/:params)' => array('myPageModule/router/index/backend', 'name' => 'lb_page_admin'),
        'backend/page' => array('myPageModule/router/index/backend', 'name' => 'lb_page_admin_dashboard'),


    Has you see, all routes beginning with "page" or "backend/page" go in the controller "router" of module "myPageModule". With a context frontend or backend.

    The action index in controller Router in module myPageModule:

        public function action_index($context = 'front')
        {
            $route = $this->params('params');
            $route = (empty($route['params'])) ? '' : $route['params'];
            
            if ($context == 'backend')
                return Request::forge('myPageModule/backend/' . $route)->set_method('GET')->execute();
            else
                return Request::forge('myPageModule/' . $route)->set_method('GET')->execute();
        }

    The problem is now all request will be seen as "HMVC" request, but i've not found other solution..

  • thanks !

    I think I understand the method and It seem to fit to my problem.
    I'll test it and see if i don't have problem with the rest of my app

  • That line in the documentation refers to module level routing.

    It says that if you define a config/routes.php in your page module, all URI's you want to match (the left-hand side) must start with 'page'.

    If not, the initial request wasn't for that module, and that routes.php file will not be loaded anyway.

    It does not apply to the global routing configuration in app/config/routes.php, in which you can put whatever you want.
  • > Harron

    OK, so I understand if I define the route  backend/page(/:params) => 'myPageModule/router/index/backend in the global routes file (app/config/routes.php), it'll work

    And I think :
    I make a script that scans the active modules ('always_load'/modules) and construct a file app/cache/routes.php (for example) and include it in app/config/routes.php
    so, i keep the routes organize by module.
    I'll try it
  • In the end, everything is php, including your routes.php config file.

    So what you could do is add a config file called 'globalroutes.php' to every module. Then in your global routes.php, get the list of loaded modules, loop over them, include these 'globalroutes.php' files, merge their contents, and return that.

    You can even cache that if you don't want to do that on every page request.
  • ok, thanks !
  • Hello,
    i am in the same situation,

    so i follow the solution of Harro Verton :

    "Then in your global routes.php, get the list of loaded modules, loop
    over them, include these 'globalroutes.php' files, merge their contents,
    and return that."


    My file : modules\admin\config\routes.php

    <?php
    $routes = array(
        'admin' => 'users/admin/login',
        'admin/login' => 'users/admin/login',
        'admin/logout' => 'users/logout',

        'admin/(:segment)' => '$1/admin/$1',
        'admin/(:segment)/(:any)' => '$1/admin/$1/$2',
    );

    $available_modules = \Config::get('website.modules');
    foreach ($available_modules as $module):
        if(\Module::exists($module)) if(!\Module::loaded($module)) \Module::load($module);
    endforeach;

    foreach($modules = \Module::loaded() as $module => $path):
        if(\Config::load($module . '::adminroutes', true)):
            $routes_module = \Config::get($module . '::adminroutes');
            $routes = \Arr::merge($routes_module, $routes);
        endif;
    endforeach;

    return $routes;


    And It work!, i generate an admin route.php on the fly with an adminroutes.php on each module.
    But i don't know how do
    I put this file in cache.



    Other solution (curiously):
    I saw in documentation, the method \Router::add() to manually add new routes at runtime.

    Questions :
    - where can i use it in my code to execute it before Request construct ? In a router() method,or _init() function of my base controller?

    - what is the
    priority of this new route added relative to the module route? It will be prepended to the routes array?

    I hope to be understandable,
    Thanks,
    Brice
  • Router::add() by default appends. You can have it prepend by passing TRUE as third argument.
    Routes in your routes.php config are processed top to bottom in the order in which they are defined.

    Router::add() isn't that useful for main requests, as the only place you could use it is in your app bootstrap (not very logical) or your router config (but there you define routes anyway). It is mainly used to add routes dynamically for HMVC calls, so secondary requests.
  • Thanks Harro for this precisions.

Howdy, Stranger!

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

In this Discussion