【问题标题】:Laravel show page according to authentication statusLaravel 根据认证状态显示页面
【发布时间】:2014-12-16 13:15:38
【问题描述】:

有什么办法可以在 routes.php 中做这个配置:

Route::get('/', function(){
if ( Auth::check() === FALSE ) 
 {
   return homeController@guest<--- Specific Controller's method
 } 
 else 
 {
   return homeController@logged <--- Specific Controller's method
 } 
});

我不想使用重定向,因为我想保留mysite.com/ 作为主地址。

【问题讨论】:

    标签: php laravel routing


    【解决方案1】:

    当然,您可以像@Ferticidios 回答那样直接在路由中做所有事情,或者像@maytham 建议的那样只有一种控制器方法。但你也可以完全按照你的要求去做:

    Route::get('/', function(){
        if ( Auth::check() === FALSE ) 
        {
            return App::make('homeController')->callAction('guest', array());
        } 
        else 
        {
            return App::make('homeController')->callAction('logged', array());
        } 
    });
    

    【讨论】:

    • 很好的解决方案!只有它要求第二个参数作为数组,传递一个空的,它就可以工作了!谢谢!
    【解决方案2】:

    你可以这样做:

    Route::get('/', function(){
        if ( Auth::check() === FALSE ) 
        {
           //Do stuff... get data 
           return Response::view('guest')->with($data);
        } 
        else 
        {
           //Do stuff... get data
           return Response::view('logged')->with($data);
        } 
    });
    

    【讨论】:

      【解决方案3】:

      这就是 Laravel 中过滤器的设计目的。内置 Auth 已经启用,或者您可以创建自己的

      Route::get('user', array('before' => 'auth', function()
      {
          return App::make('homeController')->callAction('logged');
      }));
      

      默认过滤器可以在app/filters.php

      中调整

      http://laravel.com/docs/4.2/routing#route-filters

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2018-07-30
        • 1970-01-01
        • 1970-01-01
        • 2021-10-01
        • 1970-01-01
        • 2018-11-21
        • 1970-01-01
        • 2018-09-04
        相关资源
        最近更新 更多