CakePHP 2.1.1: Run Shell from Controller

I am trying to execute Shell from my controller using an AJAX request.

In my controller:

public function log_import() { $this->autoRender = false; App::import('Console/Command', 'AppShell'); App::import('Console/Command', 'IzigetlogShell'); $job = new IzigetlogShell(); $job->dispatchMethod('main'); echo "REPONSE"; } 

And my shell:

 <?php App::import('Core', 'Controller'); App::import('Controller', 'Suivis'); class IzilogShell extends AppShell { public $uses = array('Suivi'); -- DU CODE -- $this->Suivi = new SuivisController(); $this->Suivi->constructClasses(); $exist_date = $this->Suivi->find('first', array( 'conditions' => array('Suivi.date' => $date_calcul) )); } 

But I always get the same error message: PHP Fatal error: call to the undefined method SuivisController :: find () in C: \ wamp \ www \ iziboxLogs \ app \ Console \ Command \ IzigetlogShell.php on line XX

I tried to execute a shell from the console and got the same error.

Any ideas? Thanks Martin

+4
source share
2 answers

Short answer: you do not!

This is a violation of MVC, which means cake. You need to move your β€œgeneric” code to the model and use this model only in both cases. not the controller (which is the link / logic of the model and the "webbrowser", not your shell). not the shell from inside the controller (since the shell is the link / logic from the model and the "CLI").

So:

  • The model contains all the code.
  • Shell uses the model and its methods
  • The controller uses the model and its method

=> DRY and clean

then you will also not need a single App :: import application (or better, App :: uses).

PS: if you have a lot of non-model code, you can also make Lib in APP / Lib and use it as your regular class.

PPS: public $uses = array('Suivi'); for models in any case, and not for controllers (as, for example, docs speficy).

+2
source

By doing it

 $this->Suivi = new SuivisController(); 

this->Suivi now becoming a SuiviController, not a Suivi model.

But then you use it as a model:

 $exist_date = $this->Suivi->find(...); 

The find() method is a model method, not a controller method. Therefore, if you need to call the find() method, there is no need to initialize the SuivisController at all.

0
source

Source: https://habr.com/ru/post/1447166/


All Articles