【问题标题】:laravel 5 authentication from controller来自控制器的 laravel 5 身份验证
【发布时间】:2015-08-06 09:10:22
【问题描述】:

我在 routes.php 中进行身份验证,这不是正确的方法。 现在我正在UserController 中进行身份验证。我遇到了问题。

routes.php 中,我按照以下方式执行此操作。

Route::post('/login',function(){
    $cred = Input::only('username','password');
    if(Auth::attempt($cred)){
        return Redirect::intended('/');
    }else{
        $error = "Username or password is incorrect.";
       return Redirect::to('login', compact('error'));
    }
});

Route::get('/', ['middleware' => 'auth', function(){
    return view('index');
}]);

现在我正在使用控制器UserController

class UserController extends Controller
{
    public function index()
    {
      Route::post('/login',function(){
        $cred = Input::only('username','password');
        if(Auth::attempt($cred)){
          return Redirect::intended('/');
        }else{
          $error = "Username or password is incorrect.";
        }
     });
    }

现在在routes.php

Route::get('index', ['middleware' => 'auth', 'uses' => 'UserController@index']);

但是下面的代码会报错:

MethodNotAllowedHttpException in RouteCollection.php line 201:

【问题讨论】:

    标签: php laravel authentication laravel-5


    【解决方案1】:

    在你的路线中这样做怎么样。

    Route::post('/login', ['uses' => 'UserController@index']);
    

    然后在您的控制器中进行验证。

     public function index()
        {
            $cred = Input::only('username','password');
            if(Auth::attempt($cred)){
              return Redirect::intended('/');
            }else{
              $error = "Username or password is incorrect.";
            }
        }
    

    【讨论】:

    • 感谢您的回答,现在出现此错误Class 'App\Http\Controllers\Auth' not found
    • 需要导入Auth类。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-06-08
    • 1970-01-01
    • 2015-04-25
    • 1970-01-01
    • 2017-02-20
    • 1970-01-01
    • 2015-08-21
    相关资源
    最近更新 更多