Love Fuel?    Donate

FuelPHP Forums

Ask your question about FuelPHP in the appropriate forum, or help others by answering their questions.
Call a function within another class from another class within the same module.
  • This may sound simple, but I cannot figure out how to call a function in another class from a different class in the same module. 

    Set up like \api\classes\controller\facebook\functions.php

    I want to call a function in that from a function in,

    \api\classes\controller\service\functions.php.

    Is there a way to do this?
  • In bootstrap.php you add your function class in autoloader :

    Autoloader::add_core_namespace('Api');
    Autoloader::add_classes(array(
    'Api\\Facebook' => APPPATH . 'api/classes/controller/facebook/functions.php'
    ));

    Here, your api folder is in app path, and your classes if named 'Facebook'

    At the top of your functions.php file, use namespace :

    namespace Api;

    Now you can use your function everywhere like : \Api\Facebook::yourFunction();


  • A module doesn't have a bootstrap.

    You call a controller method via a Request, you should not call the method directly. If it's a support method (and not an action), it should not be in a controller but in a separate class, which you can call directly.
  • Yes, i have not see that was in a module
  • Ah, so would it be something like, Request::forge('facebook/functions/function'); ??
  • HarroHarro
    Accepted Answer
    Correct. If it is routable. And don't forget to execute() it, this will only create the request,

    If it is not routable (i.e. there is another route that captures this URI), you need to pass FALSE are parameter to prevent a redirect to another controller by the routing engine.

    Your action_function can return anything, which can be retrieved from the response body. So in a one-liner:

    $result = Request::forge('facebook/functions/function')->execute()->response()->body();
  • Awesome thank you for your continued help!

Howdy, Stranger!

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

In this Discussion