【问题标题】:how to call background function from web controller in yii2如何在 yii2 中从 web 控制器调用后台功能
【发布时间】:2015-05-15 14:22:15
【问题描述】:

我有一个控制器功能,可以在数据库中保存一些文件,然后为数据库中的每条记录创建个人 PDF 文件并将其通过电子邮件发送给他。问题是它需要很多时间。我可以从 Web 控制器函数调用控制台控制器并将 id 传递给控制台函数吗?或者有更好的或其他方法来做到这一点?

【问题讨论】:

    标签: php background controller console yii2


    【解决方案1】:

    我认为从 Web 控制器调用控制台命令不会有太大帮助。

    但为此,您可以使用例如this extension

    用法:

    导入类:

    use vova07\console\ConsoleRunner;
    $cr = new ConsoleRunner(['file' => '@my/path/to/yii']);
    $cr->run('controller/action param1 param2 ...');
    

    应用组件:

    // config.php
    ...
    components [
        'consoleRunner' => [
            'class' => 'vova07\console\ConsoleRunner',
            'file' => '@my/path/to/yii' // or an absolute path to console file
        ]
    ]
    ...
    
    // some-file.php
    Yii::$app->consoleRunner->run('controller/action param1 param2 ...');
    

    但我建议使用RabbitMQBeanstalkd 之类的工作队列,这些更适合您的任务。

    【讨论】:

    • 我已经尝试使用它,但它尝试使用 C: 中的 php.exe 文件,而在我的服务器中 C: 中没有这样的文件。我该如何解决这个问题?
    【解决方案2】:

    我认为最好使用具有适当功能/方法的适当类,在公共区域共享它并在不同控制器中调用函数

    普通型号

        namespace common\models;
    
        use Yii;
    
        /**
         * This is the model class for table "c2_common_user_param".
         *
         * @property integer $id
         * @property integer $user_id
         * @property string $land_scope_code
         * @property string $init_lat
         * @property string $init_lng
         * @property integer $init_zoom
         * @property string $google_maps_api_key
         *
         * @property DfenxUser $user
         */
        class UserParam extends \yii\db\ActiveRecord
        {
            /**
             * @inheritdoc
             */
            public static function tableName()
            {
                return 'c2_common_user_param';
            }
    
            /**
             * @inheritdoc
             */
            public function rules()
            {
                return [
                    [['user_id'], 'required'],
                    [['user_id', 'init_zoom'], 'integer'],
                    [['init_lat', 'init_lng'], 'number'],
                    [['land_scope_code'], 'string', 'max' => 4],
                    [['google_maps_api_key'], 'string', 'max' => 255]
                ];
            }
    
            /**
             * @inheritdoc
             */
            public function attributeLabels()
            {
                return [
                    'id' => Yii::t('app', 'ID'),
                    'user_id' => Yii::t('app', 'User ID'),
                    'land_scope_code' => Yii::t('app', 'Land Scope Code'),
                    'init_lat' => Yii::t('app', 'Init Lat'),
                    'init_lng' => Yii::t('app', 'Init Lng'),
                    'init_zoom' => Yii::t('app', 'Init Zoom'),
                    'google_maps_api_key' => Yii::t('app', 'Google Maps Api Key'),
                ];
            }
        }
    

    后端控制器

        <?php
    
        namespace backend\controllers;
    
        use Yii;
        use common\models\UserParam;
        use common\models\UserParamSearch;
    
        use common\models\User;
    
        use yii\web\Controller;
        use yii\web\NotFoundHttpException;
        use yii\filters\VerbFilter;
    
    
        /**
         * UserParamController implements the CRUD actions for UserParam model.
         */
        class UserParamController extends Controller
        {
            public function behaviors()
            {
                return [
                    'verbs' => [
                        'class' => VerbFilter::className(),
                        'actions' => [
                            'delete' => ['post'],
                        ],
                    ],
                ];
            }
    
            /**
             * Lists all UserParam models.
             * @return mixed
             */
            public function actionIndex()
            {
                $searchModel = new UserParamSearch();
                $dataProvider = $searchModel->search(Yii::$app->request->queryParams);
    
                return $this->render('index', [
                    'searchModel' => $searchModel,
                    'dataProvider' => $dataProvider,
                ]);
            }
    
         }
    

    前端控制器

        <?php
    
        namespace frontend\controllers;
    
        use Yii;
        use common\models\UserParam;
        use common\models\UserParamSearch;
    
        use common\models\User;
    
        use yii\web\Controller;
        use yii\web\NotFoundHttpException;
        use yii\filters\VerbFilter;
    
    
        /**
         * UserParamController implements the CRUD actions for UserParam model.
         */
        class UserParamController extends Controller
        {
            public function behaviors()
            {
                return [
                    'verbs' => [
                        'class' => VerbFilter::className(),
                        'actions' => [
                            'delete' => ['post'],
                        ],
                    ],
                ];
            }
    
            /**
             * Lists all UserParam models.
             * @return mixed
             */
            public function actionIndex()
            {
                $searchModel = new UserParamSearch();
                $dataProvider = $searchModel->search(Yii::$app->request->queryParams);
    
                return $this->render('index', [
                    'searchModel' => $searchModel,
                    'dataProvider' => $dataProvider,
                ]);
            }
    
         }
    

    【讨论】:

    • 那么我怎样才能在后台调用函数呢?你能写/链接我一些例子吗?谢谢
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-03-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-04-16
    相关资源
    最近更新 更多