Love Fuel?    Donate

FuelPHP Forums

Ask your question about FuelPHP in the appropriate forum, or help others by answering their questions.
Few questions about Fuel Modules
  • Hey everyone,

    Just few questions about modules:

    1. Lets assume we have module 'Mymodule' and files:

    /modules/mymodule/classes/controller/mymodule.php
    /modules/mymodule/classes/model/mymodule.php

    Module is loaded by a main config, all working fine.

    Question 1: How can I define another model within this module ? If I add file:
    /modules/mymodule/classes/controller/mymodule2.php

    And class named after Controller_Mymodule2 (all module files are in same namespace)

    Question 2: How can I make instance of model added here :
    /modules/mymodule/classes/model/mymodule2.php

    Model class is named Model_Mymodule2

    Question 3: How can I add more generic classes and static classes and how to make fuel aware of their existence it I copy them to :
    /modules/mymodule/classes/mymoduleclass.php


    Are there Class / Filename / Path conventions I should follow ?

    Question 4: Is there any bootstrap file for a module ? Fuel seems to not load any file named after that, placed inside module directory (it does for packages)?

    It seems that fuel is not aware of these classes existence if they are not named exactly like module name, so that's why I'm raising this questions.

    Thanks.




  • HarroHarro
    Accepted Answer
    1 and 2:

    Every class in a module is in the namespace equal to that module. So Model_Mymodule2 would be accessed as \Mymodule\Model_Mymodule2. So apart from the base namespace, the file/folder structure of a module is identical to app.

    Which sort of answers your question 3(b) too.

    There is no bootstrap for a module. Modules are designed to modularize the app, Packages to modularize the core. It's quite easy to introduce a bootstrap for a module, by overloading the Module class, and adapt the load() method. Look in Package::load() to see how a bootstrap is loaded and parsed.

    I don't understand your 3(a) question, and your final remark.
  • Thanks for your answer.

    Coming back to 3b :

    What I mean is : while creating new classes / models / controllers should I follow any naming conventions ?

    Looks like when I create model file mymodule_myexample.php and define model class as Model_Myexample is all working as expected, but if I try something like mymodule_my_example and class defined as Model_My_Example fuel is not able to locate the resource...

    So this leads to assumption that underscore there has some special meaning, and its not allowed to name classes with use this kind of notation. So that part of the question was about any kind of conventions we need to follow when naming /models / controllers/ classes / filenames etc.

  • See http://docs.fuelphp.com/general/modules.html#/module_namespace and further. So, other than the required namespace, the structure of a module is identical to the structure of the app folder.

    So your example is wrong. If you have a module "mymodule", then all classes in the "classes" folder MUST be namespaced using the "Mymodule" prefix. All other naming is the same, so in case of your model, you have app/modules/mymodule/classes/model/example.php, containing

    namespace Mymodule;
    class Model_Example extends \Orm\Model {}

    so again, identical to a model in app, except for the namespace.

    The underscore is a bit of legacy, allowing you to move files to subdirectories without having to namespace them. I personally namespace everything, so I don't use them.

    As an example, say you have app/classes/model/this/that/other.php.

    This file can contain:

    class Model_This_That_Other {}

    but also

    namespace Model;
    class This_That_Other {}

    or even

    namespace Model\This\That;
    class Other {}

    You could even mix and match that, but I would strongly suggest not doing that, otherwise your code will become a mess:

    $a = Model_This_That_Other::forge();

    vs

    $b = \Model\ThisThat\Other::forge();

    you don't want to mix the styles!

Howdy, Stranger!

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

In this Discussion