A controller without a model

Published on and tagged with cakephp  faq  programming  tip

If you want to create a controller which does not use a model, you have to create your controller in the following way:

// tested with CakePHP r1892
class MyController extends AppController
{
    // var $uses = null; works too
    var $uses = array();

    function index()
    {		
    }
}

If you omit the “var $uses = array();” you will get a “missing model” error.

8 comments baked

  • Troy Schmidt

    What happens if you need to define the uses array so that this controller has access to others? That is why I have always done the $useTable = false; in the model for MVC’s that don’t have actual tables tied to them.

  • ChrisC

    As a newbie to CakePHP this is an interesting idea. Can you (or anyone) propose a concrete example where this would be beneficial to explain how this fits into the scheme of things?

  • JMG

    I wonder… I’ve got a Controller without any associated Model, I do not specify anything like $uses or such, and yet it just works without requesting any model…

  • cakebaker

    @Troy: What do you mean with “if you need to define the uses array so that this controller has access to others”?

    @ChrisC: An example could be a simple static web presence without a database.

    @JMG: You are right, it works in the way you described in RC3. But you have to use $uses when using the latest version from trunk and in the coming RC4.

  • Troy Schmidt

    I mean. I need to define $uses(‘Model1’, ‘Model2’, etc) So, I couldn’t use this method in that case as my $uses would actually be set to something. So, the method that works ALL the time is to setup the Model with the var $useTable = false;

    And static pages is a good example. Another good example is an administration panel. It doesn’t have it’s own table, but needs to access all the others. So, that is why $uses would need to be actually set to something and you would have to use what I described above.

  • ChrisC

    Thanks Troy, the concept of an admin panel makes perfect sense to me.

  • Nate

    Troy: Sounds like you answered your own question. If you use the method above, then your controller will have no models. If you define $uses, and set it to an array of custom models, the controller will load those models and no others.

    So where’s the confusion?

  • cakebaker » Model without table

    […] Related post: A controller without a model […]

Bake a comment




(for code please use <code>...</code> [no escaping necessary])

© daniel hofstetter. Licensed under a Creative Commons License