开发者

Integrating CLI PHP with CakePHP

I have a nice functioning CakePHP 1.3.11 site and I need a scheduled maintenance CLI script to run, so I'm writing it in PHP. Is there any way to make a cake-friendly script? Ideally I could use Cake's functions and Cake's Database models, the CLI requires database access and not much else however. I would ideally like to include my CLI code in a controller and the datasource in a model so I can call the function like any other Cake function, but only from the CLI as a sheduled task.

Searching for CakePHP CLI mostly brings results about CakeBake and cron jobs; this article sounded very helpful but it's for an old version of cake and requires a modified version of index.php. I'm no longer sure how to change the file to make it work in the new version of cakePHP.

I'm on Windows if 开发者_JAVA技巧it matters, but I have complete access to the server. I'm currently planning to schedule a simple cmd "php run.php" style script.


Using CakePHP's shells, you should be able to access all of your CakePHP app's models and controllers.

As an example, I've set up a simple model, controller and shell script:

/app/models/post.php

<?php
class Post extends AppModel {
    var $useTable = false;
}
?>

/app/controllers/posts_controller.php

<?php
class PostsController extends AppController {

var $name = 'Posts';
var $components = array('Security');

    function index() {
        return 'Index action';
    }
}
?>

/app/vendors/shells/post.php

<?php

App::import('Component', 'Email'); // Import EmailComponent to make it available
App::import('Core', 'Controller'); // Import Controller class to base our App's controllers off of
App::import('Controller', 'Posts'); // Import PostsController to make it available
App::import('Sanitize'); // Import Sanitize class to make it available

class PostShell extends Shell {
    var $uses = array('Post'); // Load Post model for access as $this->Post

    function startup() {
        $this->Email = new EmailComponent(); // Create EmailComponent object
        $this->Posts = new PostsController(); // Create PostsController object
        $this->Posts->constructClasses(); // Set up PostsController
        $this->Posts->Security->initialize(&$this->Posts); // Initialize component that's attached to PostsController. This is needed if you want to call PostsController actions that use this component
    }

    function main() {
        $this->out($this->Email->delivery); // Should echo 'mail' on the command line
        $this->out(Sanitize::html('<p>Hello</p>')); // Should echo &lt;p&gt;Hello&lt;/p&gt;  on the command line
        $this->out($this->Posts->index()); // Should echo 'Index action' on the command line
        var_dump(is_object($this->Posts->Security)); // Should echo 'true'
    }
}

?>

The whole shell script is there to demonstrate that you can have access to:

  1. Components that you load directly and that are not loaded through a controller
  2. Controllers (first import the Controller class, then import your own controller)
  3. Components that are used by controllers (After creating a new controller, run the constructClasses() method and then the particular component's initialize() method as shown above.
  4. Core utility classes, like the Sanitize class shown above.
  5. Models (just include in your shell's $uses property).

Your shell can have a startup method that is always run first, and the main method, which is your shell scripts main process and which is run after the startup.

To run this script, you would enter /path/to/cake/core/console/cake post on your command line (might have to check the proper way to do this on Windows, the info is in the CakePHP book (http://book.cakephp.org).

The result of the above script should be:

mail
&lt;p&gt;Hello&lt;/p&gt;
Index action
bool(true)

This works for me, but maybe people who are more advanced in CakePHP shells could offer more advice, or possibly correct some of the above... However, I hope this is enough to get you started.


As of CakePHP 2, the shell scripts should now be saved to \Console\Command. There is good documentation at http://book.cakephp.org/2.0/en/console-and-shells.html

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜