Love Fuel?    Donate

FuelPHP Forums

Ask your question about FuelPHP in the appropriate forum, or help others by answering their questions.
Convert Core Controller to Hybrid
  • I have been asked to perform some updates to a site that uses FuelPHP(v 1.6). The current site defines a Base controller that extends the core controller. It is located in the app/classes/base.php file. All other controllers extend Base and are located in the app/classes/controller directory. Because of new functionality that has been requested, I need to change the controllers to hybrid to expose the rest functionality. Since all controllers are derived from Base, I think I should just be able to make Base extend from Controller_Hybrid. I have tried this and get an immediate error when trying to access pages.
    The error says "Creating default object from empty value" at line 67 of COREPATH/classes/controller/rest.php. I would appreciate any help or guidance on how to resolve this issue or if there is a better way to convert to hybrid controllers.

  • HarroHarro
    Accepted Answer
    The Hybrid controller is a combination of the Template and the Rest controller. If the Base would have used Template, then the swap would have been easy and painless.

    But you say the Base controller currently extends \Controller, it makes things more complicated because that means you will have to deal with converting the current way of generating HTML output to using the template controller.

    if your task is to provide the application with a REST API, the best advise is to stay clear of the Hybrid Controller (which is a bad thing imho), but design a separate API with controllers that use Controller_Rest as a base controller. Much easier to maintain, and to upgrade in the future, if you design and version your API correctly.
  • Looked up the 1.6 codebase:

    The line that gives the error is:

            // Some Methods cant have a body
            $this->request->body = null;

    which means $this->request does not contain a valid request object. Which is very weird, since that is created in the constructor of it's parent class, \Controller.

    Does the app by any chance define it's own \Controller class in app/classes, or overload the core class, with a different constructor?
  • I don't know if this is useful, but if you want to use the Controller Hybrid will retaining the behavior of the base controller, you can:

    - Create the view, turn it into a string, and pass it to the client side where you update the html as if it where a one page application.

    or,
    - You can simply return the view as a string.. . You cannot use echo.. must return.
      E.g.: 
    return View::forge('location/userslist', $data)->render();
    (instead of  <<< $this->template->content = View::forge(.....); >>>)

    This way, you may use the Controller_Hybrid functionality with the behavior of a base Controller
  • I think the best approach for me would be to leave everything as is and create the new API using Controller_Rest as the base controller. 
    For the other question related to definition of it's own \Controller class, the constructor does not appear to be overloaded and the base.php file is the only file in the app/classes directory and it just extends the \Controller class.
    For the last comment, I do not need to use the Hybrid Controller, I just needed the REST API and thought Hybrid would be best so that I could keep the existing functionality.
    Thanks for the information.

Howdy, Stranger!

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

In this Discussion