Love Fuel?    Donate

FuelPHP Forums

Ask your question about FuelPHP in the appropriate forum, or help others by answering their questions.
Create REST API
  • Hi everyone,

    I would like to create a web application with REST API.

    I've read the doc and if I understand good, I must be create a "normal" controller for my application and another one for my APi, isn't it ?

    It isn't possible to create the same controller to display views and for the API in the same time ? Something like this :

    public function action_index()
    {
            $posts = Model_Post::find_all();
            if(Request::isJson())
            {
                    return $this->response(array('posts' => $posts));
            }
            return Response::forge(View::forge('posts/index'), $posts);
    }

    Thank you in advance for your response
  • Hello, i have use controllers for many purposes, so i think you could do it, just use a function like that: public function myfunction($data){

    //process

    return $this->response($data);
    }
  • Thank you for your response.

    But you are showing me how to use the rest controller, read the doc. This I know, but that I don't know is if it's possible to use the same controller for the api and for the application. This would say that with a same action, it's possible to detect if the response should be a json or for a view of my application.
  • well i haven't use that way but you can try extending the Hybrid_Controller that take the both functionalities.
  • HarroHarro
    Accepted Answer
    it is good practice to separate your API from your interactive part of the website. Your API also need to follow different design rules, for example towards versioning, that makes mixing functionality difficult.

    I can recommend Phil's book if you want to know more about API design: https://leanpub.com/build-apis-you-wont-hate
  • Ok, I go to read this link.

    Thank you for your response :)

Howdy, Stranger!

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

In this Discussion