redrom
redrom

Reputation: 11642

CakePHP admin routing for REST API

frontend of my CakePHP app is based on the REST API, each controller has user and admin methods.

I would like to run admin method on controller with classic Cake view. But if i type into browser URL like this:

http://myproject.loc/admin/cards/add/

I get only:

{
code: 500,
name: "View file api/app/View/Cards/json/admin_add.ctp" is missing.",
message: "View file api/app/View/Cards/json/admin_add.ctp" is missing.",
url: "/admin/cards/add/"
}

Question is:

How can i set right URL for admin routes? (without json prefix)

Upvotes: 0

Views: 681

Answers (2)

floriank
floriank

Reputation: 25698

Read Json and XML Views in CakePHP and read it properly. A common mistake is that people forget to add the RequestHandler component to their components array. The component will take care of getting the right layout set for you and the data serialized.

If you really want to insist on removing the .json suffix from the URL but calling the same URL via browser and AJAX you'll have to make sure that you request the right content type. Check the headers you send.

I'm not sure if the RequestHandler will send you the right response if you only request json (application/json) in the header without using the suffix in the URL, it should work, give it a try.

Upvotes: 1

bhushya
bhushya

Reputation: 1317

Here is the ref.

http://book.cakephp.org/2.0/en/views/json-and-xml-views.html

You need to add following line into the your controller function

I hope your encoding it to the json in the following way

$arrayData = array(...);//as example
class PostsController extends AppController {
    public function index() {
        $this->autoRender = false;
        $this->layout = null;
        $this->set(compact('posts', 'comments'));
    }
}

// View code - app/View/Posts/json/index.ctp
foreach ($posts as &$post) {
    unset($post['Post']['generated_html']);
}
echo json_encode(compact('posts', 'comments'));

Upvotes: 0

Related Questions