laravel 5.4不应该静态调用Request :: session()

时间:2017-04-10 21:25:38

标签: laravel-5.4

我正在使用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

为什么?

1 个答案:

答案 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\RequestIlluminate\Http\Request是两个不同的类,第一个是Facade,第二个是实际的请求类。我的建议是使用辅助函数request(),你可以减少混淆。