Reputation: 968
I have a products module in my application which i register at both backend and frontend of my advance application, i want to achieve if a user access the product module from frontend app i want to render a different layout not /myproject/frontend/web/views/layouts/main.php let say i want to render /myproject/frontend/web/views/layouts/productLayout.php. And if an admin officer access the product module from backend app i want to render /myproject/backend/web/views/layouts/main.php. without changing my code it works perfectly in backend app as it uses /myproject/backend/web/views/layouts/main.php but in front end it don't work good as it uses /myproject/frontend/web/views/layouts/main.php instead of /myproject/backend/web/views/layouts/productLayout.php. i tried this in my code in the products module default controller
public $productLayout = '@frontend/views/layouts/productLayout';
public function actionIndex()
{
$this->layout = $this->productLayout;
}
and it works perfectly in frontend but when i try to access from backend it doesn't work as it loads the frontend layout file... any help on this will be good thanks
Upvotes: 4
Views: 2618
Reputation: 1279
You can set it for all actions of a controller with this:
public function beforeAction($action)
{
$this->layout = 'dashboard'; // or '@app/views/layouts/dashboard'
return true;
}
Upvotes: 1
Reputation: 133400
You can test the application id (and eventually set it with a proper unique name in config/main.php) and the set the layout you need .
public function actionIndex()
{
if (Yii::$app->name == 'my_id_for_backend') {
$this->layout == '@backend/views/layouts/main';
} else {
$this->layout ='@frontend/views/layouts/productLayout';
}
}
Upvotes: 3