Love Fuel?    Donate

FuelPHP Forums

Ask your question about FuelPHP in the appropriate forum, or help others by answering their questions.
Using a module
  • Hello there,

    I am new to fuelPHP and was trying to build a small module.
    First attempt: only print some text, so I know it works.

    I have a small Template Controller which shows the layout - works fine.
    In the template I have an content are (<?php echo $content; ?>) where I can assign text ect.
    Now I want to use this variable to load a module but somehow its not working at all..
    I changed the config.php in the app folder:

        'module_paths' => array(
            APPPATH.'modules'.DS
        ),

    and

         'controller_prefix' => '\\Controller\\',

    So fuelPHP should find the module folder. I assumed this prefix also applies to module controllers? (actually
    changing it back to the default value doesnt change anything on the
    output).
    Afterwards I created a module folder with the same structure descript in the docs.
    To load the Module i tried this:

    $this->template->content = \Module::load('Blog');

    The name of the module is indeed "Blog". First question here: Is this method case-sensitiv?
    Second question: Why is this code above returning simply 1?
    As descripted in the docs I used the module controller like a normal controller to load a view:

    [EDIT] I forgot the return[/EDIT]
    return \View::forge('default', $data);

    Where default.php is inside the views folder of the module and $data is the array containing data for the views variables.
    Well this is not working somehow. It only return 1 and not the view. It seems like the module is not even loaded.
    Can someone help me out on this?

    Greetings,


    accname
  • HarroHarro
    Accepted Answer
    You need to understand what a module is, and how it works.

    A module is a completely separate part of your application. It has the same folder structure as your main app folder, with classes, views, etc. It also works the same way as you app.

    When you request a page via the browser, Fuel will check if the first URI segment matches that of a module. If so, it will use the module instead of the app, strips the module name from the URI, and continues, so the second URI segment will become the controller, the third the method, etc. From here it works exactly the same as when you request an app controller.

    Loading a module doesn't do anything, it will only tell the autoloader the module exists, and it will defined the namespace used for the module. Normally you should not have to do this, it is needed when you want to call cross-module, which is not exactly best practice. Your load() call returns 1 because it returns a boolean, true in this case, to indicate the load succeeded.

    Module names, like all folder names in Fuel, should be lowercase. You might get issues routing to it when you use uppercase in the folder names, and classes might not be found.

    If you want to call another controller, you are going to make what is called an HMVC request. Read up on it in the documentation. In your case, it should be something like:

    $this->template->content = \Request::forge('blog/controller/method', false);

    In your blog module's config folder, create a routes.php config file that will block URI access to this controller method, to avoid it being accessible from the browser. Alternatively, you can check for an HMVC call in the method itself using \Request::is_hmvc(), and throw a HTTPNotFoundException if it's not the case.
  • Thank you Harro.
    Now I'm understanding and its working :)

    Greetings,

    accname

Howdy, Stranger!

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

In this Discussion