jhuriez / fuel-module-menu

Module for managing easily the package Fuel LbMenu
3 stars 0 forks source link

Router does'nt work #1

Closed noxify closed 10 years ago

noxify commented 10 years ago

Hi,

I've implement your module in my script and after some changes (rename the backend folder to admin and some controller changes) the admin index view is now visible.

Even when I try to click on the "Add" button I do only a refresh because the button has the link "admin/menu" instead of "admin/menu/add"

I have checked the route.php and updated to this

return array(
    'admin/menu'                    => array('menu/admin/index', 'name' => 'menu_admin_menu'),                          
    'admin/menu/view/:id'           => array('menu/admin/index', 'name' => 'menu_admin_submenu'), 
    'admin/menu/add'                => array('menu/admin/index/add', 'name' => 'menu_admin_add'),                    
    'admin/menu/add/parent/:parent' => array('menu/admin/index/add', 'name' => 'menu_admin_add_to_parent'),
    'admin/menu/add/:id'            => array('menu/admin/index/add', 'name' => 'menu_admin_edit'),                  
    'admin/menu/delete/:id'         => array('menu/admin/index/delete', 'name' => 'menu_admin_delete'),   
);

I have also updated the view button to this:

Action Buttons:

<a href="<?= \Router::get('menu_admin_delete', array('id' => $menu->id)); ?>" class="btn btn-danger btn-circle"><i class="fa fa-trash-o fa-lg"></i></a>
<a href="<?= \Router::get('menu_admin_edit', array('id' => $menu->id)); ?>" class="btn btn-info btn-circle"><i class="fa fa-pencil fa-lg"></i></a>
<a href="<?= \Router::get('menu_admin_submenu', array('id' => $menu->id)); ?>" class="btn btn-warning btn-circle"><i class="fa fa-level-down fa-lg"></i></a>

Add Button for root elements

<a href="<?= \Router::get('menu_menu_add'); ?>" class="btn btn-primary"><i class="fa fa-plus"></i> <?= __('menu.action.create'); ?></a>

Add Button for child elements

<a href="<?= \Router::get('menu_menu_add_to_parent', array('parent' => $menuParent->id)); ?>" class="btn btn-info"><i class="fa fa-plus"></i> <?= __('menu.action.add_link'); ?></a>

Any idea?

jhuriez commented 10 years ago

Hi,

I think you have forget the replace "menu_menu_add" with "menu_admin_add", and same for "menu_menu_add_to_parent".

Simple question, the integration of the module was not difficult ? I try to make the simplest possible integration of this.

noxify commented 10 years ago

Hi,

i have changed the values in the view, but it doesn't work. Did I forgot something?

Yes the integration was very simple. I had only to change the extend part in the class to use my own admin controller. And also some updates in each function to use the same functionality as in my other controllers.

noxify commented 10 years ago

I have also checked the \Router::get() function in the view with

<?php \Debug::dump(\Router::get('menu_admin_add') ); ?>

The Response was "null".

For me it looks like, that the routes.php is not loaded.

If I add the modules routes.php content to the global routes.php it works fine.

Is there maybe a conflict with my routes.php?

A solution can be, that I update all my routes to use something like yours, because, your solution looks a bit better (and more flexible) as my "hardcoded" routes.

My app/config/routes.php is the following:

    '_root_'            => 'main/index',  // The default route
    '_404_'             => 'main/404',    // The main 404 route

    'login'             => 'users/login',
    'logout'            => 'users/logout',
    'register'          => 'users/register',
    'confirm'           => 'users/confirm',
    'confirm/(:any)'    => 'users/confirm/$1',

    'admin/(:segment)'                              => '$1/admin/$1/index',
    'admin/settings/(:segment)'                     => '$1/admin/settings',
    'admin/(:segment)/(:segment)'                   => '$1/admin/$1/$2',
    'admin/(:segment)/(:segment)/(:any)'            => '$1/admin/$1/$2/$3',
noxify commented 10 years ago

I checked also with

<?php \Debug::dump(\Module::loaded() ); ?>

that the menu module is loaded, because the module routes will only be used if the module is loaded / active...

The dump returns the following

Variable #1:
  (Array, 1 element) ↵
     menu (String): "/.../fuel/app/../modules/menu/" (65 characters)

Hmmm...

jhuriez commented 10 years ago

Yes that strange, routes.php from menu module doesn't seems to be loaded.

Try to place a "die('test');" in the routes.php before "return array(..."

noxify commented 10 years ago

I have added the die(), but the page is showing normally.

Also an invalid command like

\Testing::not_exists("abcd");

Will not throw an error.

In my logs is nothing. I have the following setup for logging

'log_threshold'    => Fuel::L_ALL,
noxify commented 10 years ago

Hi,

i analyzed the problem again and found the rootcause.

In my app/config/routes.php i rewrite the urls from

menu/admin/menu/index

to

admin/menu/index

If I open the menu module with the original url, the module routes.php will be loaded.

If I open the menu module with the url which I defined in the app/config/routes.php, the module routes.php will not be loaded.

Any idea to solve the problem?

jhuriez commented 10 years ago

I think FuelPHP loads module (class, route, ..) depending on the first segment..

noxify commented 10 years ago

Hmm.. thats bad :( anyway.. i will play a bit around, maybe i can create a generic solution for all modules based on your routes.php.

Thanks!

jhuriez commented 10 years ago

Or you can use HMVC request, see my post here : http://fuelphp.com/forums/discussion/comment/14658#Comment_14658

But i have no idea if it's a good way.

noxify commented 10 years ago

The answer from Harro sounds like a good solution:

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.

I will try this, maybe it's better to create a file like "moduleroutes.php" in the app/config directory. This file will be written/refreshed after click on the button "Refresh module routes" in the admin panel or via a oil task.

The file will be included in the app/config/routes.php, so we can be sure that the routes are exists and working.

jhuriez commented 10 years ago

I haven't see his post, yes that can work

noxify commented 10 years ago

Hi,

based on the idea from harro, I have customized the the app/config/routes.php. As you can see, i have implement also the cache functionality. (Currently 3 hours)

<?php

try {
    return \Cache::get('routes');
} catch (\CacheNotFoundException $e) {

    $module_routes  = array();
    foreach (\File::read_dir(MODULESPATH) as $module => $module_dir) {            
        \Module::load(substr($module, 0,strlen($module)-1));
        $module_routes = \Arr::merge($module_routes, \Config::load(substr($module, 0,strlen($module)-1).'::moduleroutes'));
    };

    \Cache::set('routes', $module_routes, 3600 * 3);
    return $module_routes;
}

This code requires also the following.

In public/index.php add this line after "COREPATH"

/**
 * The path to the modules directory
 */
define('MODULESPATH', realpath(__DIR__.'/../fuel/modules/').DIRECTORY_SEPARATOR);

This defines the path to the modules (/fuel/modules/).

The routes for each module are now in the "moduleroutes.php" in each module config folder.

Hope this helps.

jhuriez commented 10 years ago

Nice job :-)