1

I am using PHP Mailer and i have mail function in app Controller and i want to access it in shell file for crone job.

anyone can guide me how to do this?

Thanks

Mitali Mehta
  • 43
  • 1
  • 5
  • Does it have to be a shell file? PHP can be run through cron jobs, in there you can include all you need. – DickieBoy Apr 04 '13 at 11:07
  • What function is it? Seems like this is a design-failure of yours. This method should not be in the AppController then. – mark Apr 04 '13 at 11:25

4 Answers4

5

Sorry this is so late but if this were here before it would have helped me, so for future viewers:

Borrowing from a slightly different circumstance at CakePHP: best way to call an action of another controller with array as parameter? :

This is working for me so far (Haven't tried more complicated things in a controller yet): In .../app/Console/Command/BillsShell.php:

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

class BillsShell extends AppShell {
    public function main() {
        $billing = new BillingController();
        $billing->constructClasses(); //I needed this in here for more complicated requiring component loads etc in the Controller
        $billing->test();
    }
}

In BillingController.php:

class BillingController extends AppController {
    function test() {
        echo "****Big test!!!*****\n\n"; 
    }
}

For cake 2.1.3:

$> .../app/Console/php cake.php bills

Welcome to CakePHP v2.1.3 Console
---------------------------------------------------------------
App : app
Path: .../app/
---------------------------------------------------------------
****Big test!!!*****
Community
  • 1
  • 1
Bill Ryder
  • 133
  • 1
  • 8
2

You should use Cakephp Shell in order to do something in cron. The question was talked in How to setup cronjobs in cake php? .

EDIT: If you need to use something both in your controller and shell, I would suggest to move it to component. In your shell you can do

App::import('Component', 'Meteor');
$this->Meteor = new MeteorComponent();
$this->Meteor->flash('New York');

In controller

$components = array('Meteor');

public function your_action() {
  // code
  $this->Meteor->flash('Paris');
}
Community
  • 1
  • 1
icebreaker
  • 1,224
  • 17
  • 33
1

It is:

App::uses('CakeRequest', 'Network');
    App::uses('CakeResponse', 'Network');
    App::uses('Controller', 'Controller');
    App::uses('AppController', 'Controller');
    $controller = new AppController(new CakeRequest(), new CakeResponse());

=> $controller is yours

thuclh
  • 169
  • 1
  • 2
  • 5
1

There seems no direct method to call Controller inside Shell official document, where as we can do a work around, where we can call Model from Shell and in the Model we can call Controller.

Shell:

public function main() {
   App::import('Model', 'UserModel');
   $this->UserModel = ClassRegistry::init('UserModel');
   $this->UserModel->callModel();
}

Model:

function callModel($created) {
        App::import('Controller', 'Pages');
        $something = new PagesController;
        $something->callMe();          
}

Controller:

public function callMe(){
    echo "Finally, Controller method is called\n";
}

FYI: Its just a work around.

Captain Sparrow
  • 1,114
  • 17
  • 26
  • Models should not access controller methods, that's not the point of MVC and it's way too complicated to do something really simpler. What if you call a model method that calls the method in the controller being executed? You will have an infinite loop. – Alonso Urbano Feb 02 '18 at 14:38