Laravel App\Http\Requests\Comment\CreateRequest::authorize()的声明应与App\Http\Requests\Request::authorize()兼容
您好,我正在为我的laravel应用程序创建我的评论系统,但我在尝试评论时出现此错误。。基本上,当一个用户没有登录,他试图评论并提交它时,它应该将他重定向到登录页面,所以我的CreateRequest文件如下Laravel App\Http\Requests\Comment\CreateRequest::authorize()的声明应与App\Http\Requests\Request::authorize()兼容,laravel,laravel-5.2,laravel-request,Laravel,Laravel 5.2,Laravel Request,您好,我正在为我的laravel应用程序创建我的评论系统,但我在尝试评论时出现此错误。。基本上,当一个用户没有登录,他试图评论并提交它时,它应该将他重定向到登录页面,所以我的CreateRequest文件如下 <?php namespace App\Http\Requests\Comment; use App\Http\Requests\Request; use App\Repositories\Image\ImageRepository; class CreateRequest e
<?php
namespace App\Http\Requests\Comment;
use App\Http\Requests\Request;
use App\Repositories\Image\ImageRepository;
class CreateRequest extends Request {
/**
* Determine if the user is authorized to make this request.
*
* @param ImageRepository $image
* @return bool
*/
public function authorize(ImageRepository $image) {
$image = $image->getById($this->route('id'))->first();
if (!$image) {
return false;
}
return auth()->check();
}
/**
* Get the validation rules that apply to the request.
*
* @return array
*/
public function rules() {
return [
'comment' => ['required', 'min:2'],
];
}
/**
* @return \Illuminate\Http\RedirectResponse
*/
public function forbiddenResponse() {
return redirect()->route('login');
}
}
<?php
namespace App\Http\Requests;
use Illuminate\Foundation\Http\FormRequest;
abstract class Request extends FormRequest {
public function authorize() {
return true;
}
}
如果我删除
public function authorize() {
return true;
}
然后,注释起作用,如果用户未登录,用户将被重定向到登录页面,但我的登录不起作用,当我尝试登录时,我将被禁止
我正在这个项目的基础上开发我的应用程序
希望有人能帮助我您已经在抽象请求类中定义了一个
authorize
方法:
public function authorize() {
return true;
}
注意,它不接受任何参数。现在,如果您扩展这个抽象类(就像您对CreateRequest所做的那样),并编写一个authorize
方法,那么您必须使用相同的签名。具体来说:authorize
不能接受任何参数,因为它不在抽象类中
如果要从抽象类中删除
authorize
方法,则此特定错误将消失。但是我不确定这能解决你的问题。我不认为Laravel为authorize
方法提供依赖注入,你不能就这样注入你的存储库
这是解决这两个问题的一种方法:
public function authorize() {
$image = app(ImageRepository::class)->getById($this->route('id'))->first();
...
您已经在抽象请求类中定义了一个
authorize
方法:
public function authorize() {
return true;
}
注意,它不接受任何参数。现在,如果您扩展这个抽象类(就像您对CreateRequest所做的那样),并编写一个authorize
方法,那么您必须使用相同的签名。具体来说:authorize
不能接受任何参数,因为它不在抽象类中
如果要从抽象类中删除
authorize
方法,则此特定错误将消失。但是我不确定这能解决你的问题。我不认为Laravel为authorize
方法提供依赖注入,你不能就这样注入你的存储库
这是解决这两个问题的一种方法:
public function authorize() {
$image = app(ImageRepository::class)->getById($this->route('id'))->first();
...