1

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.

rmobis
  • 26,129
  • 8
  • 64
  • 65
Pars
  • 4,932
  • 10
  • 50
  • 88
  • How are you trying to get the user to select the layout? Is it a one time change, or will it be related to their user account forever? I'd suggest using a session variable, or set it up to store the layout in the user table that you're using. Once you have those, just add a conditional statement to test what layout to use in the controller. – user2658774 Sep 04 '13 at 18:55

1 Answers1

4

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'))
    
Faramarz Salehpour
  • 591
  • 1
  • 3
  • 14
  • thanks, but I am using laravel 3.2 it seems your blade syntax is laravel 4 could you write it in laravel 3 syntax. thanks again. – Pars Sep 04 '13 at 22:04
  • 1
    @aliA I think for laravel 3.2, it should be like: `public $layout = Session::get('userlayout', 'layouts.default');` and `@layout(Session::get('userlayout', 'layouts.default'))` – Faramarz Salehpour Sep 04 '13 at 22:20
  • That worked. thank you. in persian language : dastet dard nakone agha faramarz:) – Pars Sep 04 '13 at 22:25