質問

I'm trying to make a website that has 2 or more layouts, any user can change his/her layout.
I know we can use layouts in controllers like this
public $layout = 'layouts.default'; ... and in method $this->layout->nest('content', $view, $data );

but this is useless for me, it's always default, I mean how can change the value of $layout dynamicly?
for example user views website as default layout but user b views it as black layout.

------------ EDITED

I store layouts in user table, but the problem is how can I add a conditional statement in controller? the $laravel variable which stores the layout name is a property and can be set only once in the code, can not add statement outside any methods to change it.

役に立ちましたか?

解決

You can set a session variable upon user login to contain the name of the layout to use retrieved from the users table. Then you can use it to set the layout for your user or to fallback on a default layout.

e.g.:

upon user login:

Session::put('userlayout', $user->layout);

And in the controller:

  • Laravel 4

    protected $layout = Session::get('userlayout', 'layouts.default');
    
  • Laravel 3

    public $layout = Session::get('userlayout', 'layouts.default');
    

Or if you are using blade:

  • Laravel 4

    @extends(Session::get('userlayout', 'layouts.default'))
    
  • Laravel 3

    @layout(Session::get('userlayout', 'layouts.default'))
    
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top