[英]how to call background function from web controller in yii2
I have a controller function that save some file in DB and then create personal PDF file for every record in DB and email it to him . 我有一个控制器功能,可以在DB中保存一些文件,然后为DB中的每个记录创建个人PDF文件,然后通过电子邮件将其发送给他。 The problem is that it take to much time.
问题在于这需要很多时间。 Can I call console controller from the web controller function and pass the id to the console function?
我可以从Web控制器功能调用控制台控制器并将ID传递给控制台功能吗? Or there is a better or other method to do this?
还是有更好的方法或其他方法可以做到这一点?
I think is better you use a proper class with proper function/metohd , share this in common area and invoke the function in the different controllers 我认为最好使用具有适当功能/方法的适当类,在公共区域共享此内容并在不同控制器中调用该功能
common model 普通模型
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'),
];
}
}
backend controller 后端控制器
<?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,
]);
}
}
frontend controller 前端控制器
<?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,
]);
}
}
I don't think calling console command from web controller will help too much. 我认为从Web控制器调用控制台命令不会有太大帮助。
But for this purpose you can use for example this extension . 但是为此,您可以使用此扩展名 。
Usage: 用法:
Imported class: 进口舱:
use vova07\console\ConsoleRunner;
$cr = new ConsoleRunner(['file' => '@my/path/to/yii']);
$cr->run('controller/action param1 param2 ...');
Application component: 应用组件:
// 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 ...');
But I recommend to use work queues for that like RabbitMQ or Beanstalkd , these are more suitable for your task. 但是我建议为RabbitMQ或Beanstalkd等使用工作队列,它们更适合您的任务。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.