Love Fuel?    Donate

FuelPHP Forums

Ask your question about FuelPHP in the appropriate forum, or help others by answering their questions.
Different type of actions in same controller
  • Hi guys,
    I have a simple question: I create a scaffold, but I want to add some data with AJAX.
    I work with json, so to do some test I modifies the controller to extend Controller_Rest, like this:
    class Controller_Links extends Controller_Rest {
     public function post_add()
     {
      // Some code ...
      $this->response(array('result' => $response->result, 'message' => $response->message));
     }
    }
    

    Now I have a problem, because I need a "index" action to print list of data in DB. Usually I do did with a controller that extends Controller_Template, but how can I combine this two different type of action? Have I to create a second controller? Thank you!
  • $this->auto_render = false;
    
  • It doesn't work... I did that:
    1) create a controller that extends Controller_Template.
    2) rename the "post_add" action to "action_add" (so I can access it)
    3) add that line before end of function
    4) add a response But it doesn't work... Do you have a simple example of it?
  • I should have been more specific: with that statement you disable the template for the Controller_Template, you still won't have the response function as that's part of the Controller_Rest. You can convert output to the correct format using the Format class though (this is meant for use in a Controller_Template):
    class Controller_Links extends Controller_Template {
        public function action_add()
        {
             // Disable the template
             $this->auto_render = false;
    
             // Some code ...
    
             // And output
             $this->request->body = Format::factory(array('result' => $response->result, 'message' => $response->message))->to_json();
        }
    }
    
  • Hi Jelmer,
    I tried the code you posted yesterday, but it doesn't work. I was able to invoke the right controlle and the right methos, but the ouput is empty and the ajax function doesn't work... You can see my code here: http://scrp.at/ahR This code is about a simple web app to collapse link, link bit.ly. Just to test FuelPHP ;). Thank you!
    EDIT: ok, now it works: I modifies you code from "$this->request->body" to "$this->response->body" :)

Howdy, Stranger!

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

In this Discussion