Mark
Mark

Reputation: 2473

cakephp 2.x Shell using app Controller?

I want a shell script I've got to be able to access appController, is this possible, without copying that code into the AppShell ? If so any pointers ?

Thanks in advance

Upvotes: 1

Views: 2391

Answers (1)

thaJeztah
thaJeztah

Reputation: 29007

In general, shells shouldn't rely on code inside a controller, some pointers (as requested):

  • If the Shell has to perform data-related tasks, move the code to the Model. This is good practice in any case (look up 'Skinny Controllers, Fat Models' on Google)
  • Although not 'standard' supported, you can move the code to a 'Component' (see Components). Components are meant for re-usable portions of code used in Controllers.

If the above options really aren't an option, you will have to manually initialise the AppController. keep in mind that, because you're running from the command line, various things will not be present, e.g. There will be no 'request' and some environment variables (e.g. host name) may not return the expected value!

Manually initialising a Controller

Manually initialising the controller will be something like this;

App::uses('CakeRequest', 'Network');
App::uses('CakeResponse', 'Network');
App::uses('Controller', 'Controller');
App::uses('AppController', 'Controller');

// request/response may be optional, depends on your use
$controller = new AppController(new CakeRequest(), new CakeResponse());
$controller->constructClasses();
$controller->startupProcess();

Upvotes: 4

Related Questions