Love Fuel?    Donate

FuelPHP Forums

Ask your question about FuelPHP in the appropriate forum, or help others by answering their questions.
Working with Hybrid Controllers and Modules
  • New to Fuel, getting ramped up, going through tutorials, examples, etc.  Things going pretty smooth except starting to struggle with getting my app going.

    A first task for me is building an admin panel, so I have created an Admin module and copied the welcome controller from base app into it to make sure things are working correctly, which it is.  Running into some issues/questions that I cant readily find the answer to when switching to Hybrid controllers in the module instead of the standard base one.. I am getting this error:

    Fuel\Core\FuelException [ Error ]: The requested view could not be found: template

    I saw some posts about issues if you implement the before() method and dont call parent the template/views wont be available, but not implementing that at the moment.

    Another question is around where some examples use the ->template reference.., such as:

    $this->template->content = "Hello World!";

    While others utilize the style:

    \Response::forge(\View::forge('welcome/index'));

    A high percentage of my controllers.., if not all of them will be Hybrids, wondering what some best practices are around modules/hybrid controllers, and if there are any good examples.., general docs are really light on Hybrid.

    Thanks in advance
  • After some testing and looking through core code see that even though I was returning a Response object that things still wanted a "template.php" file to be in place, thats what was causing the error.

    Next trying to have json be the default response.  Seem to be running into same experience talked about here:

    http://fuelphp.com/forums/discussion/10747

    From documentation, set the "default_format" to json, but still need to either use .json on end of calls, or set the protected $format property on the class.

    From an REST/API perspective, I don't fully agree that json should only be the default response on AJAX calls, lots of scenarios (mobile GET requests, third party libraries accessing programatically, etc) where you want JSON formatted response and dont want to have to use the .json extension on the end

    Anyone wanting to have the default_format param take effect and doesn't want to have to set the $format param on each controller can set the 'ignore_http_accept' => true, which config recommends to do to speed up requests if you are not using the ACCEPT header
  • HarroHarro
    Accepted Answer
    The Hybrid controller is, as it's name implies, a combination of the template and the REST controller.

    It is designed so that it serves HTML to normal requests, and json/xml/... to REST requests. The challenge is when you call the REST API through the browser, the controller doesn't know in what format to return.

    Because of this you will have to supply a page template (the name of a view or a View object) like you do for Controller_Template, and you will have to tell the REST methods what format you want returned if it can't determine it automatically. If an action is called via a mobile browser, how should the controller "detect" that you want json returned, and not html?

    If you always want to return json, you have to use the $format property, there is no other way to tell the controller you want a particular format returned.

    FuelPHP is a community driven framework, so we're open to suggestions. If you have a way of improving the code, please let us know, create an issue on github to discuss it, or send us a pull request with your improvements.

Howdy, Stranger!

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

In this Discussion