Love Fuel?    Donate

FuelPHP Forums

Ask your question about FuelPHP in the appropriate forum, or help others by answering their questions.
Basic modules example
  • The following link is an update to Jelmers example of a basic introduction to modules and how to get them working:
    Basic modules example
  • Does anyone have an idea on how to create an /modules/account/config/config.php file which takes care of stuff and automaticly being imported by the appllication? I see there is no documentation on this. I created a route but nothing happens. (yes i registered the module in the application) thanks
  • Thanks, Mike. news.php should actually be blog.php. Fixed.
  • Can you be a bit more specific? "which takes care of stuff and automaticly being imported by the appllication" doesn't really help much. What do you want your module to do?
  • Well i want the module to have specific 'default' routes that are defined in the module 's config file. These will be imported when loading the module.
  • You can add a routes.php file to your module's config folder. This will be read and parsed when a module request is detected (the first URI segment == the module name). This implies that all routes defined must match a URI that starts with the module name: There is no module default route. A default route is defined globally, in your applications routes.php.
  • Harro Verton wrote on Sunday 3rd of April 2011:
    You can add a routes.php file to your module's config folder. This will be read and parsed when a module request is detected (the first URI segment == the module name). This implies that all routes defined must match a URI that starts with the module name: There is no module default route. A default route is defined globally, in your applications routes.php.

    I understand, but then the logic is gone. How to load the routes.php from module without having to 'request' a module via url, but just when i add it to the 'modules' list inside the application config.php?
  • Why would you want to do that? If you want to add a global route that points to a module ('test' => 'module/controller/method'), it needs to be in the app's routes.php. You don't want app specific coding in a module, that would ruin portability. If you want module specific routes ('module/test' => 'module/controller/method'), you can do that now using the module routes.php. So I still don't know why you want this. Could you give an example of why you would like to add module routes to the global routes when loading a module, without actually wanting to call the module?
  • Harro Verton wrote on Sunday 3rd of April 2011:
    Why would you want to do that? If you want to add a global route that points to a module ('test' => 'module/controller/method'), it needs to be in the app's routes.php. You don't want app specific coding in a module, that would ruin portability. If you want module specific routes ('module/test' => 'module/controller/method'), you can do that now using the module routes.php. So I still don't know why you want this. Could you give an example of why you would like to add module routes to the global routes when loading a module, without actually wanting to call the module?

    I want the routes inside the module so that the routes can be specified independent from the application that is used. Then ,the application 'can' however 'overrule' the routes if you want.
  • It's currently not possible to add routes like
    return array(
        'main/page' => 'modulename/controller/method'
    );
    
    to a module's routes.php. At the moment, it's not our intention to do so, as that would mean look for and parsing the routes.php of all (loaded) modules, which would quickly become a mess, especially if you start with third-party modules. The only place to put these routes (where you are sure they are available and won't introduce conflicts) is in your app's routes.php. If you feel otherwise, please add a feature request to our bugtracker at [url=http://dev.fuelphp.com,]http://dev.fuelphp.com,[/url] explaining why you need this, and linking to this thread for reference.
  • are modules still valid?
    I don't see anything about them in the current docs?
  • How can I use function(action_index) that is defined in blog.php(controller) in my view (It may be stupid question..!!!)
    I am new in this framework
    please help me
  • Yes they are valid.
    We all are using them :) so go on!
  • ok thank you. Just so I understand, is a module a way to "group" your code into a folder for organization purposes? If you created a blog module on your website abc.com, and then you wanted to use that same code on your other website def.com, you could copy everything in the module/blog folder to the new site?
  • I'm writing this very short tutorial/example for those developers who intend to use modular system for their future projects built on Fuel. First, here's how your module system should look like if you have module "Blog":
    http://i.imgur.com/j6t9a.png Now, let's see what should be inside of blog.php which you have to createin app/modules/blog/classes/controller:
    namespace Blog;
    
    Class Controller_Blog extends [b]\Controller[/b] {    
    
        public function action_index()
        {
            // Here goes index action logic
            $this->render('index');
            // calls for view file index.php located in fuel/app/modules/blog/views/index.php
        }
    
    }
    
    Now, let's add model. Blog.php goes to app/modules/blog/classes/model:
    namespace Blog;
    
    class Model_Blog extends [b]\Model[/b] {
    
        public static function hi()
        {  
            return 'hello world!';
        }
    }
    
    SO, we have a model, but how do we get data from it? Here's an update of blog.php controller:
    namespace Blog;
    
    Class Controller_Blog extends [b]\Controller[/b] {
        public function action_index()
        {
            echo [b]Model_Blog::hi()[/b];
            // This will print 'hello world' from our model
        }
    }
    
  • Steve Wasiura wrote on 03/15/11 1:46 pm:
    ok thank you. Just so I understand, is a module a way to "group" your code into a folder for organization purposes? If you created a blog module on your website abc.com, and then you wanted to use that same code on your other website def.com, you could copy everything in the module/blog folder to the new site?

    That is correct as far as I know. In basic terms, modules are application extensions, whereas packages are fuel extensions.
  • Charlie Black wrote on Saturday 22nd of January 2011:
    I'm writing this very short tutorial/example for those developers who intend to use modular system for their future projects built on Fuel.
    ...
    ...
    Many thanks for your tutorial. I followed the instructions and created "app/modules/blog/views/index.php"
      <h4>hello from module -> BLOG - view -> index.php </h4>
    
    
    Unfortunately, the output is identical when the blog view is called from the main view:
      <h4>hello from module -> BLOG - view -> index.php </h4>
    
    
    I would be grateful to know how to call the module so that the HTML formatting is correct.
  • Managed to find a solution: file: ./fuel/app/classes/controller/john.php
    public function action_index()
    &#123;
        // HURRAY - CRACKED IT
        Fuel::add_module('blog');
        if(true)
        &#123;
              // FORMATS CORRECTLY 
              $tmp = Request::factory('blog', false)->execute();
              $this->template->set_global('blog', $tmp, false);
              unset($tmp);   
        }
        else
        &#123;
              // NOT FORMATTED
              $data['blog'] = Request::factory('blog', false)->execute();
        }
           
        // lots more stuff goes here
        $data = array(); 
        $data['module_title'] = 'John\'s Modules Stuff';
    
        $this->template->content= View::factory('john/v_john', $data);
    }//endfunc
    
    }//endclass
    
    file: ./fuel/app/views/john/v_john.php
      <div class='modules'>
           <h3 class='bga'><?php echo $module_title;?></h3>
    
           <h3>Modules Testing</h3>
           <?php if(1) &#123; echo $blog; } ?>
      </div>
    
    

    http://www.ipaste.org/9g Update:
    http://fuel.johns-jokes.com/modules.html
  • Charly, shouldn't your news.php controller be:
    Class Controller_News extends \Controller {
    

    And your blog.php controller:
    Class Controller_Blog extends \Controller {
    

Howdy, Stranger!

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

In this Discussion