Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/drupal/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 覆盖laravel';AuthController中的s 5.2身份验证方法_Php_Laravel_Authentication_Laravel 5_Laravel 5.2 - Fatal编程技术网

Php 覆盖laravel';AuthController中的s 5.2身份验证方法

Php 覆盖laravel';AuthController中的s 5.2身份验证方法,php,laravel,authentication,laravel-5,laravel-5.2,Php,Laravel,Authentication,Laravel 5,Laravel 5.2,我仍然对拉威尔很不熟,所以我不知道该怎么做: 我被登录系统卡住了。我试图了解如何防止非活动用户使用该应用程序 编辑 对于非活动用户,我指的是那些在0上具有记录字段“活动”的用户。因此,我需要实现一个检查来验证邮件、密码以及活动字段 我制作了表和auth系统,如文档中所述,我将此方法放置在AuthController中: public function authenticate() { dd('hi'); } 它完全被忽略了。它似乎永远不会被触发。我错过什么了吗 除了之前的方法之外,我的控

我仍然对拉威尔很不熟,所以我不知道该怎么做: 我被登录系统卡住了。我试图了解如何防止非活动用户使用该应用程序

编辑 对于非活动用户,我指的是那些在0上具有记录字段“活动”的用户。因此,我需要实现一个检查来验证邮件、密码以及活动字段

我制作了表和auth系统,如文档中所述,我将此方法放置在AuthController中:

public function authenticate()
{
   dd('hi');
}
它完全被忽略了。它似乎永远不会被触发。我错过什么了吗

除了之前的方法之外,我的控制器看起来像原来的Laravel的5.2 AuthController。没有进行其他更改,因为根据文件,没有提及其他更改

我的测试: 我还搜索了一个名为authenticate的方法。但是没有找到任何方法(使用php storm)。所以我的问题是:

如果它是一种特性,那么不应该声明该方法吗?所以我可以通过声明一个同名的方法来覆盖它

重要文件: Routes.php
为了防止来宾用户(未经身份验证)访问某些路由,您应该在
路由中保护这些路由。php

// All the routes inside the group are accessible for logged in users only
Route::group(array('before' => 'auth'), function() {
    Route::get('myroute', 'Controller@action'); 
});

// For routes accessible for everybody
Route::get('login', function(){
    return View::make('login');
});
为了防止来宾用户(未经身份验证)访问某些路由,您应该在
routes.php中保护这些路由:

// All the routes inside the group are accessible for logged in users only
Route::group(array('before' => 'auth'), function() {
    Route::get('myroute', 'Controller@action'); 
});

// For routes accessible for everybody
Route::get('login', function(){
    return View::make('login');
});
解决了的 我自己设法做到了这一点。我不确定这是正确的方法,但无论如何它是有效的

我决定创建一个中间件,在成功登录后,检查用户“活动”字段是否为1。如果没有,它将重定向到特定页面

public function handle($request, Closure $next)
{
    //  If the user is not logged in
    if(!Auth::user()){
        return redirect('login');
    }
    //  If the user is inactive
    if(!Auth::user()->active){
        return redirect('inactive');
    }

    return $next($request);
}
解决了的 我自己设法做到了这一点。我不确定这是正确的方法,但无论如何它是有效的

我决定创建一个中间件,在成功登录后,检查用户“活动”字段是否为1。如果没有,它将重定向到特定页面

public function handle($request, Closure $next)
{
    //  If the user is not logged in
    if(!Auth::user()){
        return redirect('login');
    }
    //  If the user is inactive
    if(!Auth::user()->active){
        return redirect('inactive');
    }

    return $next($request);
}

首先,您需要删除身份验证路由,但在此之前,请在终端中运行
php artisan-route:list
。复制与身份验证相关的路由并将其粘贴到路由文件中

更改每个路由中的控制器,使其指向应用程序中自己的AuthController

接下来,在你的控制器(页面顶部)中加入以下2个特性,再加上2个类

use Illuminate\Foundation\Auth\ThrottlesLogins;
use Illuminate\Foundation\Auth\AuthenticatesAndRegistersUsers;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
然后把这些特点列在你班的第一名

use AuthenticatesAndRegistersUsers, ThrottlesLogins;
目前,身份验证功能仍应正常工作

然后,更改登录方法以接受活动标志。将以下内容添加到新控制器中

/**
* [login description]
* @param  Request $request [description]
* @return [type]           [description]
*/
public function login( Request $request, $guard )
{
    $this->validateLogin( $request );
    $throttles = $this->isUsingThrottlesLoginsTrait();

    if ( $throttles && $lockedOut = $this->hasTooManyLoginAttempts( $request ) ) {
        $this->fireLockoutEvent( $request );
        return $this->sendLockoutResponse( $request );
    }
    $credentials = $this->getCredentials( $request );
    if ( Auth::guard( $guard )->attempt( [ 'active' => 1 ] + $credentials, $request->has( 'remember' ) ) ) {
        return $this->handleUserWasAuthenticated( $request, $throttles, $guard );
    }

    // check to see if they can login without the active flag
    if( Auth::guard( $guard )->attempt( $credentials ) ) {
        // log them back out and send them back
        Auth::guard( $guard )->logout();
        return redirect()->back()
            ->withInput( $request->only( $this->loginUsername(), 'remember' ) )
            ->withErrors([
                'active' => 'Your account is currently not active',
            ]);
    }
    if ( $throttles && ! $lockedOut ) {
        $this->incrementLoginAttempts( $request );
    }
    return $this->sendFailedLoginResponse( $request );
}
在你自己的控制器中使用这些特性,将来会给你更多的灵活性


关于

首先,您需要删除身份验证路由,但在此之前,请在终端中运行
php artisan route:list
。复制与身份验证相关的路由并将其粘贴到路由文件中

更改每个路由中的控制器,使其指向应用程序中自己的AuthController

接下来,在你的控制器(页面顶部)中加入以下2个特性,再加上2个类

use Illuminate\Foundation\Auth\ThrottlesLogins;
use Illuminate\Foundation\Auth\AuthenticatesAndRegistersUsers;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
然后把这些特点列在你班的第一名

use AuthenticatesAndRegistersUsers, ThrottlesLogins;
目前,身份验证功能仍应正常工作

然后,更改登录方法以接受活动标志。将以下内容添加到新控制器中

/**
* [login description]
* @param  Request $request [description]
* @return [type]           [description]
*/
public function login( Request $request, $guard )
{
    $this->validateLogin( $request );
    $throttles = $this->isUsingThrottlesLoginsTrait();

    if ( $throttles && $lockedOut = $this->hasTooManyLoginAttempts( $request ) ) {
        $this->fireLockoutEvent( $request );
        return $this->sendLockoutResponse( $request );
    }
    $credentials = $this->getCredentials( $request );
    if ( Auth::guard( $guard )->attempt( [ 'active' => 1 ] + $credentials, $request->has( 'remember' ) ) ) {
        return $this->handleUserWasAuthenticated( $request, $throttles, $guard );
    }

    // check to see if they can login without the active flag
    if( Auth::guard( $guard )->attempt( $credentials ) ) {
        // log them back out and send them back
        Auth::guard( $guard )->logout();
        return redirect()->back()
            ->withInput( $request->only( $this->loginUsername(), 'remember' ) )
            ->withErrors([
                'active' => 'Your account is currently not active',
            ]);
    }
    if ( $throttles && ! $lockedOut ) {
        $this->incrementLoginAttempts( $request );
    }
    return $this->sendFailedLoginResponse( $request );
}
在你自己的控制器中使用这些特性,将来会给你更多的灵活性


问候

已解决

若您键入php artisan route:list,您将在通过post方法登录时看到该列表,用户将其信息帐户发布到LoginController中的登录操作。因此,我通过在Auth\LoginController中覆盖登录方法解决了这个问题,如下所示:

public function login(Request $request)
{
    $credentials = $request->only('email', 'password');
    $credentials['active'] = 1;
    if (Auth::attempt($credentials)) {
        // Authentication passed...
        return redirect()->intended('/');
    }
}

已解决

若您键入php artisan route:list,您将在通过post方法登录时看到该列表,用户将其信息帐户发布到LoginController中的登录操作。因此,我通过在Auth\LoginController中覆盖登录方法解决了这个问题,如下所示:

public function login(Request $request)
{
    $credentials = $request->only('email', 'password');
    $credentials['active'] = 1;
    if (Auth::attempt($credentials)) {
        // Authentication passed...
        return redirect()->intended('/');
    }
}

你好我想我没有抓住问题的重点。刚刚编辑。我试图做的是在登录时执行第三次检查。这是记录的活动字段。我不确定这项检查是否必须在身份验证后进行,或者我必须在身份验证后检索用户数据并在那里阻止他…啊,我明白了。您是否删除了traits
使用AuthenticatesAndRegistersUsers、ThrottlesLogins来自控制器?我从这里得到了完全相同的控制器-
authenticate
方法被调用我以前尝试过这个方法,我得到了以下错误:Route.php第280行中的ReflectionException:Class App\Http\Controllers\Auth\AuthController不存在。我认为这与路线有关,但我不知道怎么回事。你能不能也发布你的
routes.php
?嗨!我想我没有抓住问题的重点。刚刚编辑。我试图做的是在登录时执行第三次检查。这是记录的活动字段。我不确定这项检查是否必须在身份验证后进行,或者我必须在身份验证后检索用户数据并在那里阻止他…啊,我明白了。您是否删除了traits
使用AuthenticatesAndRegistersUsers、ThrottlesLogins来自控制器?我从这里得到了完全相同的控制器-
authenticate
方法被调用我以前尝试过这个方法,我得到了以下错误:Route.php第280行中的ReflectionException:Class App\Http\Controllers\Auth\AuthController不存在。我想这和路线有点关系,但我不知道怎么回事。你也可以发布你的
routes.php
?哇!谢谢我要这样做!哇!谢谢我要这样做!