简体   繁体   中英

Yii2 how to launch console?

I am new to Yii2 and web development. Could you please explain me how to launch Yii2 console, because it's not clear from the documentation: http://www.yiiframework.com/doc-2.0/guide-tutorial-console.html . I understood that there is a yii console file where I can configure some commands, but how to I actually launch it?

it's so easy to use , if you don't access SSH you wont lunch console .

go to your yii directory where you can find a file named YII ( without any extaion )

RUN that with php command

php yii yourconntroller-name/your-action-name 

before that you have to create a contorller and action in it

it's a simple sample :

<?php

namespace console\controllers;
use yii\console\Controller;
use common\models\Post;

/**
 * Test controller
 */
class TestController extends Controller {

    public function actionIndex() {

        echo date( 'Y-m-d H:i:s');
        $p = Post::find()->where('status=0 AND  auto_publish_date IS NOT NULL  AND auto_publish_date < NOW( )  ')->all();
        foreach($p as $post){
            echo $post->id."\n";
            $post->status=1;
            date_default_timezone_set('Asia/Tehran');
            $post->time = date( 'Y-m-d H:i:s');
            $post->save();
        }

        echo "....\n".count($p)." posts has been published ...\n";
    }

}

Run in your terminal your_project_path/yii controller/action [options] (make sure yii has execute permissions).

Substitute controller/action by some already shipped commands (ie: migrate/up ), or write your own inside the console application.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM