Love Fuel?    Donate

FuelPHP Forums

Ask your question about FuelPHP in the appropriate forum, or help others by answering their questions.
I am totally lost with the Hybrd Controller
  • Okay so i recently started to study backbone, so i digg in to the rest controller docs.
    Found out if i want to use the template and the rest i need the Hívrid controller.

    So my problem is if i specify the rest format in my controller to json it ignores it, second what i dont get if i specify my view in the content just the view is shown but the rest not.

    I know im doing something wrong. so my code

    <?php

    class Controller_Tasks extends Controller_Hybrid {
        
        protected $format = 'json';

        public function action_index()
        {
           $task =  Model_Task::find('all');
           return $this->response($task);
        }

    this way i get my results back in json format

    but if i do it this way to pas my resutls to the view

    class Controller_Tasks extends Controller_Hybrid {
        
        protected $format = 'json';

        public function action_index()
        {
           $tasks = Model_Task::find('all');
           $task = $this->response($tasks);
           $this->template->content = View::forge('welcome/index', array('task' => $task));

        }

    the view is not loaded, so im a bit confused here, and please be gentle.

    So could someone give me a hint what i am doing wrong or give me and example, how to pass my json results to my view?

    thank you,
  • You're using a template content when you don't have a template set. Since you're not using it, that call will do nothing. Also, if you want the JSON data from $task, using $this->response() makes the app send the JSON as the content, setting it as a variable of the view does nothing but send the response object into the view (which is probably not what you want).

    Are you trying to embed JSON data in the HTML so that you can use it in the page? Are you trying to create a JSON response for a AJAX call? A more detailed example of what you're trying to achieve might help get you a good answer.

    To make your example here do something, try this:

    class Controller_Tasks extends \Controller_Hybrid {
        public function action_index() {
            $tasks = Model_Task::find('all');
            \Response::forge(\View::forge('welcome/index', array('task' => json_encode($task)));
        }
    }

Howdy, Stranger!

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

In this Discussion