Love Fuel?    Donate

FuelPHP Forums

Ask your question about FuelPHP in the appropriate forum, or help others by answering their questions.
calling Model of Module from outside?
  • Hi i have problem when i want to call Model of Module from outside , this is my structure code

    default classes
    classes/
    ---controller/
    ---model/
    ------auth.php

    then  my module
    users/
    ---classes/
    ------controller/
    ------model/
    ---------users.php

    how to call Model_Users inside Auth.php ?
  • Using an HMVC call, i.e. a secondary request.

    To maintain loose coupling between the components of your application, you should not call the model directly, but call an "API" controller method in the users module using an HMVC request, have that run the query, and return the results.

    If your application needs to be scalable, you should use the same technique for all I/O, splitting the application's modules into a frontend layer (html generation, user input, data caching if needed) and a service layer (backend I/O, business rule processing, etc).

    Encapsulate your HMVC call in a generic method, so you can switch from a local HMVC request (everything runs on a single webserver) to cURL requests (frontend and service layer are separate webservers) without impact to your code.
  • A request returns a Response object:

    class Controller_Parent
    {
        public function action_index()
        {
            try
            {
                $result = Request::forge('child/index')->execute()->response()->body();
                var_dump($result); // should be an array of Model_Child's...
            }
            catch (\HttpNotFoundException $e)
            {
                // controller could not be found/loaded
            }
        }
    }

    and

    class Controller_Child
    {
        public function action_index()
        {
            // run whatever query you want here, and return the result
            return Model_Child::find('all');
        }
    }

    If you want to pass parameters, you can do that either throw the URI (like you would do in the browser) or pass then via an array in the execute() method of the request. In both cases, you can fetch them like you would in any action, via the method parameters.

    As to access control, I would have separate HMVC controllers, and do an is_hmvc() check in the before() method of that controller. Or alternatively make sure your HMVC isn't routable (so it isn't accessable from the browser) and use Request::forge($uri, false) to bypass the routing engine.
  • okay I had imagine that, if I am not wrong  this code 

    $result = Request::forge('child/index')->execute()->response()->body(); 

    using to request from controller to uri and execute that. my question is , this code using for request as HMVC ? because in controller any condition 

    if( ! Request::is_hmvc())
    {
    // this is the main request
    }
    else
    {
    // this is a HMVC request
    }


    I am still not imagined that , what the slug for we know that is request as HMVC ? 
  • If you have a separate controller for your "service layer", you don't have to check for HMVC requests, as that is all it's receiving. It will keep your code clean as well.

    If the controller is accessable via the browser too, you can prevent browser access by using:

    public function before()
    {
        if ( ! \Request::is_hmvc())
        {
            // fake a 404 so the controller appears not-found
            throw new \HttpNotFoundException();
        }

        parent::before();
    }

Howdy, Stranger!

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

In this Discussion