Laravel身份验证系统此页面无法处理请求

时间:2020-08-03 11:49:09

标签: laravel-7 laravel-7.x

当我想打开admin身份验证系统页面时,它将收到以下错误。另外,我正在使用localhost XAMPP服务器。 收到第一个错误: 允许的内存大小为536870912字节已用尽。 现在我找到了解决方案,所以我增加了php.ini文件的内存大小限制。

然后收到第二个错误: 超过了60秒的最大执行时间。 现在我找到了解决方案,因此增加了php.ini文件的最大执行时间。

然后由于该页面无法处理请求而无法打开页面。 有没有人可以解决?

路线:

Auth::routes();
Route::middleware('auth')->group(function (){
    Route::get('/dashboard',function (){
        return view('dashboard');
    });
    Route::get('/faculty', function (){
        $title='Faculty Console';
        return view('faculty-dashboard',compact('title'));
    });
    Route::get('/student', function (){
        $title='Student Console';
        return view('user-dashboard',compact('title'));
    });
    Route::prefix('admin')->group(function () {
        Route::get('/', function (){
            $student=User::all()->where('user_role','student')->count();
            $employee=User::all()->where('user_role','employee')->count();
            $title='Admin Console';
            return view('admin.admin-dashboard',compact('title','student','employee'));
        });
        Route::resource('selection','SelectionController');
        Route::get('selection/{id}/pivot-create/','SelectionController@pivotCreate')->name('pivotCreate');
        Route::get('selection/{id}/pivot-destroy/','SelectionController@pivotDestroy')->name('pivotDestroy');
    });
});

Auth.php

    'defaults' => [
        //'guard' => 'web',
        'guard' => 'user',
        'passwords' => 'users',
    ],

    'guards' => [
        'web' => [
            'driver' => 'session',
            'provider' => 'users',
        ],
        'user' => [
            'driver' => 'session',
            'provider' => 'users',
        ],
        'api' => [
            'driver' => 'token',
            'provider' => 'users',
            'hash' => false,
        ],
    ],

    'providers' => [
        'users' => [
            'driver' => 'eloquent',
            'model' => App\User::class,
        ],
    ],

    'passwords' => [
        'users' => [
            'provider' => 'users',
            'table' => 'password_resets',
            'expire' => 60,
            'throttle' => 60,
        ],
    ],

    'password_timeout' => 10800,

App / User.php

class User extends Authenticatable
{
    use Notifiable;
    protected $guarded = [];
    protected $table='users';
    protected $hidden = ['password', 'remember_token',];
    protected $casts = [
        'email_verified_at' => 'datetime',
    ];
    protected $dates = ['created_at', 'updated_at'];
}

LoginController

class LoginController extends Controller
{
    use AuthenticatesUsers;
    public function username()
    {
        return 'user_name';
    }
    protected function redirectTo(){
        if (Auth::user()->user_role == 'admin' && Auth::user()->status == '1'){
            //alert()->message('Welcome','Admin')->closeOnClickOutside();
            return '/admin';
        }
        elseif (Auth::user()->user_role == 'faculty' && Auth::user()->status == '1'){
            //alert()->message('Welcome','Faculty')->closeOnClickOutside();
        return '/faculty';
        }
        elseif (Auth::user()->user_role == 'student' && Auth::user()->status == '1'){
            //alert()->message('Welcome','Student')->closeOnClickOutside();
            return '/student';
        }
        else
            return redirect('/login');
    }

    public function logout()
    {
        Auth::logout();
        //session()->flash('msg_logout', 'Panel System Successfully Logout');
        alert()->success('You have been logout successfully','GoodBye');
        //session()->flush();
        return redirect('/login');
    }
    public function showLoginForm(){
        $title='TheMSC-Sign-In Panel-System';
        return view('log-in',compact('title'));
    }
}

RegisterController

class RegisterController extends Controller
{
    use RegistersUsers ;
    protected $redirectTo = RouteServiceProvider::HOME;
    protected function validator(array $data)
    {
        return Validator::make($data, [
            'cnic' => ['required','string'],
            'user_name'=>[ 'required','string','unique:users','regex:[a-z\d-]'],
            'user_role'=>[ 'required','string'],
            'email' => [ 'required', 'string', 'email:filter,dns'],
            'password' => [ 'required', 'string', 'confirmed','regex:[a-zA-Z\d $&+,:;=?@#|\'<>.^*()%!-]'],
        ]);
    }

    protected function create(array $data)
    {
        return User::create([
            'cnic' => $data['cnic'],
            'user_name' => $data['user_name'],
            'user_role' => $data['user_role'],
            'email' => $data['email'],
            'password' => Hash::make($data['password']),
        ]);

    }


}

1 个答案:

答案 0 :(得分:1)

您的路线文件中有一些可以更改的地方。

Route::get('/', function () {
    $student = User::all()->where('user_role', 'student')->count();
    $employee = User::all()->where('user_role','employee')->count();
    $title = 'Admin Console';
    
    return view('admin.admin-dashboard', compact('title','student','employee'));
});

更改

$student = User::all()->where('user_role', 'student')->count();
$employee = User::all()->where('user_role', 'employee')->count();

$student = User::where('user_role', 'student')->count();
$employee = User::where('user_role', 'employee')->count();

为什么? User::all

  • 制作select * from users
  • 使用数据库刚刚返回的所有行创建一个Illuminate\Database\Eloquent\Collection对象。

当您与许多用户一起工作时,这可能会占用大量内存。您两次查询。然后,使用->where(...)来过滤Collection,这在与许多用户一起工作时可能会占用大量内存。然后,您使用->count()将其减小为单个值,这意味着您查询并制作为Collection的所有数据都一无是处。

如果只想计数,请让数据库来做。 User::where('user_role', 'student')->count();转换为简单的select count(*) from users where user_role = student