doy2255 2013-09-04 10:36
浏览 27
已采纳

如何在laravel中切换布局?

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.

  • 写回答

1条回答 默认 最新

  • dsk95913 2013-09-04 13:41
    关注

    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'))
      
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
编辑
预览

报告相同问题?

手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部