4

I have some controllers in my Cake application, namely servers and users. I want to write a simple API and have a controller called ApiController. Within this controller I want to use both the servers and users models.

I'm very new to Cake, but not MVC in general. From what I've picked up so far Cake will automatically use the servers model in the ServersController controller, I don't know how to explicitly use a model from a certain controller.

Also, I want the API requests to only serve JSON without any HTML markup. I have a default layout that defines the header/footer of all my site pages and that gets outputted when I request an API function as well as the JSON from the view. How can I stop the layout from being output and instead just serve the view?

tereško
  • 58,060
  • 25
  • 98
  • 150
James Dawson
  • 5,309
  • 20
  • 72
  • 126

2 Answers2

14

You need to declare the $uses property in your controller see http://book.cakephp.org/2.0/en/controllers.html#controller-attributes

The $uses attribute states which model(s) the will be available to the controller:

<?php
class ApisController extends AppController{
    public $uses = array(
        'User',
        'Server'
    );
}

Also you don't appear to be following Cake naming conventions where controller names are plural (Apis or Servers) and model names are singular (Api or Server). These names should also be in CamelCase. See http://book.cakephp.org/2.0/en/getting-started/cakephp-conventions.html for more information

Regarding the JSON, there is an Ajax layout you can use to help you server JSON requests. See http://book.cakephp.org/2.0/en/views.html#layouts for more information on how to achieve this.

93196.93
  • 2,601
  • 1
  • 19
  • 17
  • 6
    Alternatively you can use $this->loadModel('Model'); to load models on the fly in case you only need them in certain portions of code. You can then communicate with the models normally (i.e. $this->Model->doSomething();) as if it were their own controller. – Nick Savage Jul 07 '12 at 23:07
  • 1
    Don't you just love Cake so much? – 93196.93 Jul 07 '12 at 23:45
  • It's sped up the work process on the applications I've used it with greatly. Definitely my preferred framework. – Nick Savage Jul 07 '12 at 23:49
  • If you want `ApisController` to still be able to use the model `Api` in the normal `$this->Api` way you need to include it in the `$uses` array: `public $uses = array('Api', 'User', 'Server');`. – Annabel Apr 17 '15 at 21:06
2

Yottatron's answer is spot on, as is Nick Savage's. It's important to know the differences between the different ways to load a model, which is succinctly covered in following comment: https://stackoverflow.com/a/4753244/117413

Personally, I stay away from overloading the global $uses array since it's very rare that I need a reference to all the model objects at a global level (and it's just bad practice to overload it, as per the Cake docs here: https://book.cakephp.org/1.3/en/The-Manual/Developing-with-CakePHP/Controllers.html#components-helpers-and-uses )

Community
  • 1
  • 1
leo
  • 339
  • 2
  • 13
  • I agree with you on the `$uses` global shouldn't need to be done in AppController and on the odd occasion in controllers only say when you need access to a model that isn't associated. – 93196.93 Jul 08 '12 at 23:08