Reputation: 205
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 . 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? Or there is a better or other method to do this?
Upvotes: 1
Views: 2749
Reputation: 133360
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,
]);
}
}
Upvotes: 1
Reputation: 33538
I don't think calling console command from web controller will help too much.
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.
Upvotes: 2