0

I have the following controller:

App::import('Controller', 'Users');
class EmployeesController extends  AppController
{

}

Now another StackOverflow question said to do the following:

<?php
//Import controller
App::import('Controller', 'Posts');

class CommentsController extends AppController {
    //Instantiation
    $Posts = new PostsController;
    //Load model, components...
    $Posts->constructClasses();

    public function index($passArray = array(1,2,3)) {
        //Call a method from PostsController with parameter
        $Posts->doSomething($passArray);
    }
}
?>

However if I try to copy this to my code that my code looks like this:

App::import('Controller', 'Users');

class EmployeesController extends  AppController {

    public $name = 'Employee';
    $Users = new UsersController;

I get a syntax error and if I run it anyway I get a fatal error.

So my question is how do I call a function from another controller?

Community
  • 1
  • 1
Marc Rasmussen
  • 19,771
  • 79
  • 203
  • 364
  • 4
    What you are doing there is awefully wrong. You may never include controllers in others like so. Use models or components to share functionality. Also drop the $name stuff. In your case its declared wrong anyway. You also dont App::import() in CakePHP2.x. PS: You should always mention the exact cakephp version you are using. – mark Aug 09 '13 at 09:40

1 Answers1

3

You don't.

Read the accepted answer from the question you linked to, it's on point. And, Mark Story (core CakePHP contributor) endorses it in the comments.

I don't know exactly what your requirements are, but you almost certainly want to move the logic into your model. In MVC, always think 'fat models, skinny controllers'.

If you have more questions, ask in the comments or update your question, and I'll update my answer with more info.

joshua.paling
  • 13,762
  • 4
  • 45
  • 60
  • I am also a cakePHP core dev - but not Mark Story (but Mark Scherer^^). – mark Aug 09 '13 at 09:41
  • Yeah I know... I wrote my answer before seeing your comment... ?? – joshua.paling Aug 09 '13 at 09:43
  • So my logic is actually an add function that first adds the user into the user table then should return an id so that i can insert it into the employee table would this be possible from the model? im sorry but i am quite new to cake. – Marc Rasmussen Aug 09 '13 at 09:46
  • Ahhh Mark I see what you're saying. I'm referring to the comments in the accepted answer of the question he linked to. Mark Story (not you) endorses the accepted answer of that question. – joshua.paling Aug 09 '13 at 09:47
  • Marc Rasmussen, it sounds like first you need to let us know specifically what you're trying to do - like, from a top level point of view, and then you can get advice on the best way to go about it. But yes, it's possible in the model. If you want, jump on #cakephp IRC channel and ask there... – joshua.paling Aug 09 '13 at 09:49
  • @joshua.paling i think il post a new question explaining exactly what i want to do from model to view – Marc Rasmussen Aug 09 '13 at 09:51
  • @joshua.paling ive created a new question if you have time please have a look at http://stackoverflow.com/questions/18144184/cakephp-working-with-two-controllers-models – Marc Rasmussen Aug 09 '13 at 10:02