我正在使用Laravel 5.4,
我开箱即用了validator()
RegisterController
<?php
namespace App\Http\Controllers\Auth;
use Illuminate\Http\Request;
class RegisterController extends Controller
{
protected function validator(array $data)
{
$validationCode = Request::session()->get('validation_code', '');
return Validator::make($data, [
'name' => 'required|max:255',
'role' => 'required|in:1,2',
'email' => 'required|email|max:255|unique:users',
'password' => 'required|min:6|confirmed',
'validation_code' => 'required|in:' . $validationCode
]);
}
}
方法,如下所示:
Non-static method Illuminate\Http\Request::session() should not be called statically
有一个错误:
12345
为什么?
答案 0 :(得分:1)
改变这个:
$validationCode = Request::session()->get('validation_code', '');
到此:
$validationCode = session()->get('validation_code', '');
//or
$validationCode = request()->session()->get('validation_code', '');
//or
$validationCode = Illuminate\Support\Facades\Request::session()->get('validation_code', '');
//or
$validationCode = \Request::session()->get('validation_code', '');
Illuminate\Support\Facades\Request
和Illuminate\Http\Request
是两个不同的类,第一个是Facade,第二个是实际的请求类。我的建议是使用辅助函数request()
,你可以减少混淆。