Laravel控制器方法调用其他'帮助'方法

时间:2015-01-15 06:22:09

标签: php laravel laravel-4 laravel-routing

我在我的控制器中有一个正常运行的用户登录路由但是该方法本身很长并且处理了多个问题,例如用户被禁止的可能性,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路由/控制器?如果没有,你能就如何处理这种情况提出任何建议吗?

2 个答案:

答案 0 :(得分:1)

看起来你忘了返回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);

}

答案 1 :(得分:1)

handleAuth()return Redirect::intended('/');正在将某些内容返回到postLogin()。您需要从postLogin()返回该值。

因此,请在return添加postLogin()

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

其他修正

handleAuth(),同时添加return

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