我试图找出为什么我的自定义请求类无法通过我的某个方法调用。
我用php artisan make:request ValidateUserSecretRequest
创建了我的课程。
这会按预期在Http/Requests
文件夹中创建我的自定义请求文件。
但是,在ValidateUserSecretRequest
内调用了Auth\LoginController.php
,我得到了Class App\Http\Controllers\Auth\ValidateUserSecretRequest does not exist
。
这是控制器,删除了不必要的方法:
namespace App\Http\Controllers\Auth;
use Cache;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Contracts\Auth\Authenticatable;
use Illuminate\Foundation\Auth\AuthenticatesUsers;
use App\Http\Requests\ValidateSecretUserRequest;
use App\Http\Controllers\Controller;
class LoginController extends Controller
{
use AuthenticatesUsers;
public function __construct()
{
$this->middleware('guest')->except('logout');
}
public function postValidateToken(ValidateUserSecretRequest $request)
{
// get user id and create cache key
$userId = $request->session()->pull('2fa:user:id');
$key = $userId . ':' . $request->totp;
// use cache to store token to blacklist
Cache::add($key, true, 4);
// login and redirect user
Auth::loginUsingId($userId);
return redirect()->intended($this->redirectTo);
}
我的自定义请求类:
namespace App\Http\Requests;
use Cache;
use Crypt;
use Google2FA;
use App\User;
use App\Http\Requests\Request;
use Illuminate\Validation\Factory as ValidatonFactory;
use Illuminate\Foundation\Http\FormRequest;
class ValidateUserSecretRequest extends FormRequest
{
/**
*
* @var \App\User
*/
private $user;
/**
* Create a new FormRequest instance.
*
* @param \Illuminate\Validation\Factory $factory
* @return void
*/
public function __construct(ValidatonFactory $factory)
{
$factory->extend(
'valid_token',
function ($attribute, $value, $parameters, $validator) {
$secret = Crypt::decrypt($this->user->google2fa_secret);
return Google2FA::verifyKey($secret, $value);
},
'Not a valid token'
);
$factory->extend(
'used_token',
function ($attribute, $value, $parameters, $validator) {
$key = $this->user->id . ':' . $value;
return !Cache::has($key);
},
'Cannot reuse token'
);
}
/**
* Determine if the user is authorized to make this request.
*
* @return bool
*/
public function authorize()
{
try {
$this->user = User::findOrFail(
session('2fa:user:id')
);
} catch (Exception $exc) {
return false;
}
return true;
}
/**
* Get the validation rules that apply to the request.
*
* @return array
*/
public function rules()
{
return [
'totp' => 'bail|required|digits:6|valid_token|used_token',
];
}
}
我试过了:
这到底是怎么回事?
答案 0 :(得分:0)
使用错误App \ Http \ Requests \ ValidateSecretUserRequest;你可以再检查一下。
答案 1 :(得分:0)
你得到Class App\Http\Controllers\Auth\ValidateUserSecretRequest does not exist
,因为它并不存在。错误中的App\Http\Controllers\Auth\
前缀表示它使用当前名称空间。
查看ValidateUserSecretRequest
- 您应该交换User
和Secret
来编写正确的班级名称。
答案 2 :(得分:0)
你写过“使用App \ Http \ Requests \ ValidateSecretUserRequest;”当你使用“ValidateUserSecretRequest”类时,有一个错字。
答案 3 :(得分:0)
你的班级名称错了
你有它
App\Http\Requests\ValidateSecretUserRequest
;
但实际上它是ValidateUserSecretRequest