【问题标题】:Laravel controller method calling other 'helper' methodsLaravel 控制器方法调用其他“帮助”方法
【发布时间】:2015-03-13 12:20:39
【问题描述】:

我的控制器中有一个正常运行的用户登录路由,但是该方法本身很长并且处理了多个问题,例如用户被禁止的可能性、IP 日志记录等。我想分解该方法以便它是更易于管理,我想知道这是否可能? 以下是 'postLogin()' 为路由的 3 种方法:

    public function postLogin()
{
    $validator  =   Validator::make(Input::all(), array(
       'login-username'     =>  'required',
       'login-password'    =>  'required'
    ));

    if($validator->fails())
        return Redirect::back()->withErrors($validator)->withInput();
    else
    {
        $user_attempt    =   Input::get('login-username');
        $pass_attempt    =   Input::get('login-password');
        $auth   =   Auth::attempt(array(
           'username'   =>  $user_attempt,
           'password'   =>  $pass_attempt
        ), true);

        if($auth)
           $this->handleAuth();

        else
            return Redirect::route('home')->with('fail', 'Incorrect username or password, username: ' . $user_attempt . ' pass: ' . $pass_attempt);                  
    }
}

private function handleAuth()
{
    $username       =   Auth::user()->username;
    $banned_info    =   UserBans::getBanned($username);
    $stored_ip      =   Auth::user()->ip_address;
    $current_ip     =   User::getIp();

    if($stored_ip != $current_ip)
        User::updateIp(Auth::user(), $current_ip);

    if(is_null($banned_info))
        return Redirect::intended('/');
    else
        $this->handleBan($banned_info, $current_ip);

}


private function handleBan($banned_info, $current_ip)
{
    if(UserBans::isBanned($banned_info['ban_end']))
    {
        $message    =   "This account is currently banned until: " . $banned_info['ban_end'] . " Reason: " . $banned_info['reason'];

        if($banned_info['ip_address'] != $current_ip)
        {
            $banned_model   =   UserBans::getBannedModel($banned_info['id']);
            User::updateIP($banned_model, $current_ip);
        }

        Auth::logout();
        return Redirect::route('home')->with('fail', $message);
    }

    else
    {
        UserBans::destroy($banned_info['id']);
        return Redirect::intended('/');
    }
}

我发现的问题是主控制器方法将毫无问题地调用辅助方法,但是辅助方法尝试重定向到路由,例如在 handleAuth() 中:

if(is_null($banned_info))
        return Redirect::intended('/');

如果用户没有被禁止并且拥有正确的凭据,则会发生这种情况,通常它会重定向到主页并且您会登录,但是当此方法调用预期时,我会在'postLogin'处留下一个空白页面' 路由网址。如果您刷新页面,您就在家中并已登录。以下是相关路线:

Route::group(array('before' => 'guest'), function()
{ 
    Route::group(array('before' => 'csrf'), function()
    {
        Route::post('/user/login', array('uses' => 'UserController@postLogin', 'as' => 'postLogin'));
    });
});

这可能与 laravel 路由/控制器有关吗?如果没有,您能否就如何处理这种情况提出任何建议?

【问题讨论】:

    标签: php laravel laravel-4 laravel-routing


    【解决方案1】:

    handleAuth()return Redirect::intended('/'); 正在向postLogin() 返回某事。您需要从postLogin() 返回该值。

    所以,将return 添加到postLogin()

    if($auth)
       return $this->handleAuth();
    

    其他修复

    handleAuth(),也加return

    else
       return $this->handleBan($banned_info, $current_ip);
    

    【讨论】:

      【解决方案2】:

      看来你忘了返回 handleBan() 结果

      public function postLogin()
      {
         //...
         if($auth)
            return $this->handleAuth();
         //...
      }
      
      private function handleAuth()
      {
          //...
          if(is_null($banned_info))
              return Redirect::intended('/');
          else
             return $this->handleBan($banned_info, $current_ip);
      
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-06-15
        • 2013-01-30
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多