It's simpler than that. Each controller function can act as an API method, so
leave the folder structure as it is.You can detect the json extension by using
the RequestHandler component and adding Router::setExtensions(array('json'));
near the bottom of routes.php. Then you can call
/controller/action/variable.json and it'll be handled as json - et voila you
have an api.
If you want to reach them using a separate URL (/api/function_name.json) then
using admin routing with an 'api' route. Then you api methods would have an api
prefix (e.g. api_index) and would be reached via
/api/controller/action/variable.json.
Here's a good article that explains it much better:
http://www.dereuromark.de/2014/01/09/ajax-and-cakephp/. It's actually about
ajax, but the set up and principles are very close.
On 2 Jul 2014, at 10:49, Hatem Ben Raïs <[email protected]> wrote:
> Hi,
>
> I have an app which requires some Api to expose its services. I am trying to
> group all the Api related controllers in a directory as explained in the doc
> here : http://book.cakephp.org/2.0/en/development/configuration.html
>
> So :
> 1- I created a /app/Controller/Api folder and moved the controllers in it.
> 2- added this code to my bootstrap.php : App::build(array('Controller' =>
> array( DS . 'app' . DS . 'Controller', DS . 'app' . DS . 'Controller' .
> DS . 'Api',), ));
>
> When I try to call one of the actions I get an error saying the controller
> cannot be found :
>
> http://localhost:8888/XXXXX/api/articles.json
>
> returns
>
> {"code":404,"name":"Controller class ArticlesController could not be
> found.","message":"Controller class ArticlesController could not be
> found.","url":"\/XXXX\/api\/articles.json"}
>
>
> Anyone can help me figuring out what is missing please? The doc seems so
> clear...
>
> Thanks!
>
> --
> Like Us on FaceBook https://www.facebook.com/CakePHP
> Find us on Twitter http://twitter.com/CakePHP
>
> ---
> You received this message because you are subscribed to the Google Groups
> "CakePHP" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to [email protected].
> To post to this group, send email to [email protected].
> Visit this group at http://groups.google.com/group/cake-php.
> For more options, visit https://groups.google.com/d/optout.
--
Like Us on FaceBook https://www.facebook.com/CakePHP
Find us on Twitter http://twitter.com/CakePHP
---
You received this message because you are subscribed to the Google Groups
"CakePHP" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/cake-php.
For more options, visit https://groups.google.com/d/optout.