1

I'm trying to create Rest API without view and planning to use these api's in angular 2 application. does have any idea about it?

1

3 Answers 3

1

Cake makes this incredibly easy. A few things I have learned building without views.

Set the _serialize variable

$data = ['cheeses' => ['gouda', 'pepper jack', 'cheddar']];
$this->set('responseData', $data);
$this->set('_serialize', 'responseData');

Throw bad request exceptions and other network related exceptions

Cake will render nice json views for you.

Set your accept header when issuing and ajax request to be application/json

You can use cake prefixes for api versions

Look at Stateless Authentication for your api

Sign up to request clarification or add additional context in comments.

Comments

0

In your AppController.php, with these parameters, all of your controllers will be render in json

public function beforeRender(Event $event)
{
     $this->RequestHandler->renderAs($this, 'json');
     $this->response->type('application/json');
     $this->set('_serialize', true);
}

Comments

0

CakePHP will render json easily.

In your Controller,look like something.

protected   $responseBody   =   [];

public function beforeRender(Event $event){

    foreach($this->responseBody as $responseKey=>$response){

       $this->set($responseKey, $response);
    }
    $this->set('_serialize', array_keys($this->responseBody));
}

public function initialize()
{
    parent::initialize();

    $this->RequestHandler->renderAs($this, 'json');
}

public function index(){

    $this->request->allowMethod(['get']);  // Method like post,get..

    $this->responseBody["statusCode"]       =   200;

    $this->responseBody["statusDescription"]        =   ''; //You send any text in json.

    $this->responseBody["data"]  =  []; // All data that you can send.

}

For further informations , You can see CakePHP Cookbook REST API to click here

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.