如何使用通用接口类传递不同的自定义请求?

huangapple go评论54阅读模式
英文:

How to pass different custom requests with common Interface class?

问题

为了在不同的存储库类中传递不同的自定义请求,您可以通过以下方式触发CreateTagRequest类的验证:

在您的TagCrudRepository.php文件中,确保传递的$request参数是一个有效的CreateTagRequest实例,如下所示:

namespace App\Repositories;

use App\Http\Requests\CreateTagRequest;
use App\Repositories\Interfaces\CrudInterface;

class TagCrudRepository implements CrudInterface
{
    public function store(array $data, $request): Tag
    {
        // 确保$request是CreateTagRequest的实例
        if (!($request instanceof CreateTagRequest)) {
            // 如果不是,请将其转换为CreateTagRequest实例
            $request = new CreateTagRequest($request->all());
        }

        // 然后可以执行验证和存储操作
        $validatedData = $request->validated();

        // 接下来执行存储操作并返回结果
        // ...

        return $tag;
    }

    // 其他方法...
}

通过这种方式,您可以确保在存储操作中始终使用CreateTagRequest的验证规则。这样,无论哪个自定义请求您传递,都会触发验证。

英文:

Making Interface class for Crud operations I need in some cases to pass custom requests(ex. CreateTagReques)t and I make it with calling class
in controller as :

public function store(Request $request)
{
    try {
        \Log::info(' -1 BEFORE CreateTagRequest::');    // I see this log message
        $tag = $this->tagCrudRepository->store(
            data: $request->only('name'),
            request: new CreateTagRequest($request->all())
        );

that works, but validation method is not called actually, as :

I added log messages in app/Http/Requests/CreateTagRequest.php class :

class CreateTagRequest extends FormRequest
{
    public function authorize(): bool
    {
        return true;
    }

    public function rules(): array
    {
        $request= request();              // I DO NOT SEE THIS MESSAGE :
        \Log::info( '-1 CreateTagRequest $request->all()::' . print_r( $request->all(), true  ) );

and I got error:

: SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'name' cannot be null (Connection: mysql, SQL: insert into `tags` (`name`) values (?))

So validation on CreateTagRequest class is not triggered. How can I trigger it ?

Problem is raised as TagCrudRepository class implements CrudInterface in app/Repositories/Interfaces/CrudInterface.php I have :

use Illuminate\Foundation\Http\FormRequest;

interface CrudInterface
{
    public function store(array $data, $request): Model;
...

and app/Repositories/TagCrudRepository.php :

namespace App\Repositories;

use App\Http\Requests\CreateTagRequest;

use App\Repositories\Interfaces\CrudInterface;
class TagCrudRepository implements CrudInterface
{

    public function store(array $data, $request): Tag
    {
        ...

So having 1 CrudInterface for different repository classes I need to pass different custom requests and I failed in my attempt above...

How can I do it ?

答案1

得分: 2

在你的store方法中,我发现你没有使用formrequest

Public function store(CreateTagRequest $request).

你的接口只能接收通过CreateTagRequest验证后的数据。

在进行上述更改后,你可以将它更改为:

$tag = $this->tagCrudRepository->store(data: $request->validated());

在你的接口中,你可以只传递data参数。

public function store(array $data)

请求不应该在存储库类或服务类中处理。

英文:

The thing I find is that on your store method you are not using formrequest.

Public function store(CreateTagRequest $request). 

You interface can only receive the validated data that came after passing from CreateTagRequest.

After doing above mentioned changes you can change it to.

$tag = $this->tagCrudRepository->store(data: $request->validated());

On your interface you can change and only pass data parameter.

public function store(array $data)

Request is something which shouldn’t be handled on repository class or services class.

huangapple
  • 本文由 发表于 2023年5月25日 19:56:52
  • 转载请务必保留本文链接:https://go.coder-hub.com/76332002.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定