Love Fuel?    Donate

FuelPHP Forums

Ask your question about FuelPHP in the appropriate forum, or help others by answering their questions.
Modules
  • Hi guys,

    I've just started to use modules, so I have a very newbie question.

    I created my module folder 'gmtest', with the normal structure, like this :

    • classes
      • controller
      • model
      • view

    • config
    • lang
    • tasks
    • views

    In the controller folder of the module I have my widget.php controller, like this :

    namespace Gmtest;

    class Controller_Widget
    extends \Controller_Base_Public
    {
       
    public static function say($text)
        {
            return 'The text is : ' . $text;
        }

    }

    If I am in a different controller, from the admin area let's say, and I want to call the static method from my module,(my widget class), it works only if I do it like this :

    Module::load('gmtest');
    echo \Gmtest\Controller_Widget::say('hello World!');

    What should I do, in order to call it like this :

    echo \Gmtest\Widget::say('hello World!');

    ??

    Of course that a solution would be to make the namespace Gmtest\Controller and to rename the class from Controller_Widget to Widget...but is not what I want, as the doc gives us the example like this : 

    namespace Mymodule;

    class Controller_Widget { #code }

    A second question is also related to namespaces.

    For a better understanding, if I am inside my module, and I call another class from the SAME module (of course with the same namespace), I don't have to use the '\', but if I call from my module a standard Fuel class, I have to use it, correct ?

    Thanks for your answers.

  • SyntaxlbSyntaxlb
    Accepted Answer
    Hi,

    For your first question :

    In all my project i use controller classes for Request URI (or HMVC Request), if it's just a static method like "say('hello world!');", i use helper class

    Just create your class in your module folder "classes/widget.php" :

    namespace Gmtest;

    class Widget

    {
       
     public static function say($text)
        {
            return 'The text is : ' . $text;
        }

    }


    And call him : \Gmtest\Widget::say('hello world!');


    For your second question : 

    I think yes it's correct :-)

  • Note: I think it's better for architecture to place your class in a folder "helper" :-)
  • HarroHarro
    Accepted Answer
    The idea behind modules is to create loose coupling between functional application blocks, which in turn will allow you to re-use your modules.

    Given this, hardcoded calling between controllers (in a module or not) is a bad idea.

    As Syntaxlb already wrote, use an HMVC request, preferably a routed one. If your module controller, check if it's an HMVC call if you don't want to method to be available from the browser. And handle the possibility of a 404 on the calling side.

    So something like (untested!):

    class Widget
    {
        public function before()
        {
            // capture calls from the browser
            if ( ! \Request::is_hmvc())
            {
                throw new \HttpNotFoundException();
            }
        }  
    }

    // ------------------------------------------------------------------------------------

    namespace Gmtest;

    class Controller_Widget extends \Widget
    {
        public static function action_say($text)
        {
            return 'The text is : ' . $text;
        }
    }


    // ------------------------------------------------------------------------------------

    class Controller_Test extends \Controller_Base_Public
    {
        public function action_test()
        {
            // fetch the widget
            try
            {
                $result = \Request::forge('gmtest/widget/say')->execute(array('This is a test'))->response()->body();
            }
            catch (\HttpNotFoundException $e)
            {
                \\ gmtest module not installed, or controller not found
            }

            return $result;
        }
    }
  • Hi guys,

    I have a module, called 'events'.

    How can I wrote the line in the routes.php of my app, in order to route a specific url to my module.

    So, if the user types : www.mywebsite.com/events

    I want to rout to my module events/controller/events/index()

    I've tried like this, but it doesn't work :

    'events' => 'events::events/index'

    The events module is auto loaded in the config.php

    Thanks.
  • HarroHarro
    Accepted Answer
    For modules, the first segment of the URI is the module name. So without the route, www.mywebsite.com/events should already resolve to events/events/index, which is the default module route.

    The correct syntax would be

    'events' => 'events/events/index'
  • I've tried it and it doesn't work.

    Where should I put the line, in app/config/routes.php or in events/config/routes.php ?
  • tried both, and it doesn't work :(
  • sorry, my mistake, I forgot to put the namespace Events to my controller. Works !

Howdy, Stranger!

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

In this Discussion