Reputation: 33
I have a project where the user chooses the layout, and saves it in the database, how do I choose to change this layout in real time?
For example; http://www.example.com/username/controller/action/id
Throughout the site I would have with the first parameter the username, which is actually where the system will know which layout it chose.
Can anybody help me?
EDITED
For example; When the user accesses the site I pass the following link: www.example.com/index.php?layout=4545455, so I would be able to know which layout to use, but how do I keep this parameter layout=4545455 on all routes Site? Well if I click on the about menu it will be with the url www.example.com/index.php?r=site/about
Upvotes: 0
Views: 471
Reputation: 33
With the help of all I got the following:
class MainController extends \yii\base\Controller {
public function init()
{
parent::init();
}
public function beforeAction($action) {
if(Yii::$app->request->get('layout')) {
$this->layout = 'set_layout';
}
return parent::beforeAction($action);
}
}
class SiteController extends MainController
{
// code here
}
I've created a main Controller, and all the controls I create will inherit from it. And using the beforeAction ($ action)
method I can change the layout according to whats in the url. (Eg www.example.com/index.php?layout=485121)
Upvotes: 0
Reputation: 1092
You can set layout inside your login controller.
When user log-in successfully, get his layout from database and set the layout as $this->layout = "layout_name"
. Provided that you need to keep layout files ready inside your view folder
Note : Please refer @sm1979's answer for more detail
Upvotes: 2
Reputation: 290
You have mentioned that the user's choice of layout is stored in database. You can use that information right after login and override the default layout in the application component.
Code snippet of login action could be something like this:
....
if ($model->load(Yii::$app->request->post()) && $model->login()) {
//you can use Yii::$app->user->id and get the corresponding layout info
//using something like below, assuming UserLayouts as the model
//corresponding to the table storing user's layout choice
$layout = UserLayouts::find()->where(['user_id' => Yii::$app->user->id])->one();
Yii::$app->layout = $layout->id; //you should fetch the field which is the name of the layout file
//redirect to landing page for member
...
}
This would set the layout for the particular user for all controllers for that particular session, so you would not have to pass the layout info in the URL. Please note this approach will work only if you are not overriding the layout property in each Controller.
This is what Nitin P has also suggested. Only difference is he is suggesting to set $this->layout = "layout_name"
, which I believe will set the layout for that particular controller only and not all controllers. From Yii2 Guide (http://www.yiiframework.com/doc-2.0/guide-structure-views.html#using-layouts):
You may use a different layout by configuring either
yii\base\Application::$layout
oryii\base\Controller::$layout
. The former governs the layout used by all controllers, while the latter overrides the former for individual controllers.
I don't have enough reputation to comment on his answer, so I added new answer.
Upvotes: 1