Yii creates scheduled tasks through console commands_PHP tutorial

WBOY
Release: 2016-07-13 10:34:41
Original
991 people have browsed it

Assume the Yii project path is /home/apps/

1. Create file /home/apps/protected/commands/crons.php

<?<span php

</span><span $yii</span> = '/home/apps/framework/yii.php'<span ;

</span><span require_once</span>(<span $yii</span><span );  

</span><span $configFile</span> = <span dirname</span>(<span __FILE__</span>).'/../config/console.php'<span ;

Yii</span>::createConsoleApplication(<span $configFile</span>)->run(); 
Copy after login

2. Create the required configuration file /home/apps/protected/config/console.php, configure the required components, database connections, logs and other information. The format is similar to the main configuration file main.php

<?<span php  
</span><span return</span> <span array</span><span ( 
    </span>'basePath'=><span dirname</span>(<span __FILE__</span>).DIRECTORY_SEPARATOR.'..', 

    'name'=>'Emergency',

    'import'=><span array</span><span (  
            </span>'application.models.*',
            'application.components.*', 
            'application.extensions.*',<span 
    )</span>, 

    'components'=><span array</span><span (
        </span>'log'=><span array</span><span (
            </span>'class'=>'CLogRouter',
            'routes'=><span array</span><span (
                </span><span array</span><span (
                    </span>'class'=>'CFileLogRoute',
                    'levels'=>'info, warning, error',<span 
                )</span>,<span 
            )</span>,<span 
        )</span>, 
        'db'=><span array</span><span (
            </span>'class'=>'application.extensions.PHPPDO.CPdoDbConnection',
            'pdoClass' => 'PHPPDO',
            'connectionString' => 'mysql:host=xxxx;dbname=xxx',
            'emulatePrepare' => <span true</span>,
            'username' => 'xxx',
            'password' => 'xxx',
            'charset' => 'utf8',
            'tablePrefix' => 'tbl_',<span 
        )</span>,<span 
    )</span>,  
        
    'params' => <span require</span>('params.php'),<span 
);</span>
Copy after login

3. Create a new TestCommand class under /home/apps/protected/commands/ and inherit CConsoleCommand. In TestCommand, you can use the project’s configuration information and Yii’s various methods

<?<span php  
</span><span class</span> TestCommand  <span extends</span><span  CConsoleCommand  
{  
    </span><span public</span> <span function</span><span  run()
    {  
        </span>...<span 
    }  
}</span>
Copy after login

4. Create scheduled tasks

$ crontab -e
Copy after login

Insert

1 * * * * /home/php/bin/php -f /home/apps/<span protected</span>/commands/crons.php Test &
Copy after login

means to execute the contents of the TestCommand class in the first minute of every hour. Similarly, you can create other classes under /home/apps/protected/commands/ and execute them using the command line.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/749285.htmlTechArticleAssume the Yii project path is /home/apps/ 1. Create the file /home/apps/protected/commands/crons .php ? = '/home/apps/framework/yii.php' ( = ( ).'/../config/console.php' ::createConsoleAppli...
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template