Love Fuel?    Donate

FuelPHP Forums

Ask your question about FuelPHP in the appropriate forum, or help others by answering their questions.
Extend/Overrides Shared Module ?
  • Hi,
    can i extend/overrides shared module ?

    In my config.php :
    'module_paths' => array(
            APPPATH.'modules'.DS,
            SHAREDPATH.'modules'.DS,
        ),


    For example, i have a News Module (shared) in :
    SHAREDPATH.'modules/news'

    Controller :
    SHAREDPATH.'modules/news/classes/controller/admin/news.php'
    :

    namespace News;

    class Controller_Admin_News extends \Admin\Controller_Base
    {
        public function action_index()
        {   
            $this->data['news'] = Model_News::find('all', array('order_by' => array('published_at' => 'desc')));
           
            etc.....
        }
    }

    Can I extend or overrides my News Module (App) with the News Module (shared) ?

    I test this, but no work.

    Controller :
    APPPATH.'modules/news/classes/controller/admin/news.php' :

    namespace News;

    class Controller_Admin_News extends \News\Controller_Admin {
        public function action_index(){
             //Overrides index method
         }

         public function action_new_method(){
        }
    }
  • Depends on what you want to override.

    With that setup, at the module level, a module "news" in APPPATH will be found before one in SHAREDPATH, so this is now you overload modules.

    If you want to overload a single class of a module, you can use the same technique as with extending core classes, define a new class => file map in your app bootstrap.php.

    If you want to overload a single method, you have a problem, because the above method makes the origina class inaccessable, so it can't be loaded anymore.

    For controllers, the preferred way is through routes, so simply map the URI. The original controller has \news\admin\index as URI, just map it to the new controller at  \admin\news\index. And in that case the extension as in your code works fine.
  • I if understood, in your solution "simply map the URI", i would not the same folder structure because i will overload my module. That's right?

    Good structure will be :

    Shared folder :
    modules/news/classes/controller/admin/news.php


    App folder :
    classes/controller/admin/news.php

    Can i use News namespace in this controller ?
  • Mapping the URI, i.e. creating a route for it, will remap at controller action level.

    so for example you can map:
    'admin/news/this' => 'some/controller/index',
    'admin/news/that' => 'some/other/controller/method',
    'admin/news/:any' => 'news/admin/news/index',

    and so on. So what appears as a single environment (admin/news) at the frontend could be completely different in the backend. It is the quickest method to override something, without code changes.

    And no, that controller can not have the "News" namespace. Naming should follow the rules for the "cascading filesystem", the mapping from class to file.

    Basic rule is:

    <base-namespace>\<namespace>\<controller> => <classes filename>

    For App, the base namespace is empty, it lives in the global namespace, for a package or a module, the base namespace is the name of the package or module, ucfirst. Once you have this, replace all underscores and backslashes into DIRECTORY_SEPARATOR, and you have your filename.

    You can use this the other way around too.

    So for classes/controller/admin/news.php, it could contain

    class Controller_Admin_News
    namespace Controller; class Admin_News
    namespace Controller\Admin; class News

    for modules/news/classes/controller/admin/news.php it would be

    namespace News; class Controller_Admin_News;
    namespace News\Controller; class Admin_News;
    namespace News\Controller\Admin; class News;

    You can deviate from the by creating an autoloader classmap in your app/bootstrap.php (there is a commented-out example in there), but then you override on the class level again.

    And if you sub-namespace your controllers, you need to change the configured controller prefix in your config file from "Controller_" to "Controller\\". Which also means this change is for all your controllers.

Howdy, Stranger!

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

In this Discussion