raid/core-request

Raid Core Request Package

dev-main 2024-01-24 22:39 UTC

This package is auto-updated.

Last update: 2024-05-24 23:16:26 UTC


README

This package is responsible for handling all requests in the system.

Installation

composer require raid/core-request

Configuration

php artisan core:publish-request

Usage

use App\Traits\Request\WithUserCommonRules;
use Raid\Core\Request\Requests\FormRequest;

class CreateUserRequest extends FormRequest
{
    use WithUserCommonRules;

    /**
     * Get the validation rules that apply to the request.
     */
    public function rules(): array
    {
        return $this->withCommonRules();
    }
}

How to work this

Let's start with our request class CreateUserRequest.

you can use the command to create the request class.

php artisan core:make-request CreateUserRequest
<?php

namespace App\Http\Requests;

use Raid\Core\Request\Requests\FormRequest;

class CreateUserRequest extends FormRequest
{
    /**
     * Get the validation rules that apply to the request.
     */
    public function rules(): array
    {
        return [];
    }
}

This looks like a normal request class, but it's not it just needs some helper.

The request class MUST extend the FormRequest class.

Now, let's create our common-rules-request trait.

you can use the command to create the common request trait.

php artisan core:make-common-request WithUserCommonRules
<?php

namespace App\Traits\Request;

trait WithUserCommonRules
{
    /**
     * Get the common rules for the request.
     */
    public function commonRules(): array
    {
        return [];
    }

    /**
     * Get the common attributes for the request.
     */
    public function attributes(): array
    {
        return [];
    }
}

The commonRules method is responsible for returning the common rules for the request.

you can define the rules that are common for your request and their attributes.

<?php

namespace App\Traits\Request;

trait WithUserCommonRules
{
    /**
     * Get the common rules for the user.
     */
    public function commonRules(): array
    {
        return [
            'name' => ['string', 'min:2', 'max:255'],
        ];
    }

    /**
     * Get the common attributes for the user.
     */
    public function attributes(): array
    {
        return [
            'name' => __('attributes.name'),
        ];
    }
}

With Common Rules

Now let's go back to our request classes.

<?php

namespace App\Http\Requests;

use Raid\Core\Request\Requests\FormRequest;
use App\Traits\Request\WithUserCommonRules;

class CreateUserRequest extends FormRequest
{
    use WithUserCommonRules;

    /**
     * Get the validation rules that apply to the request.
     */
    public function rules(): array
    {
        return $this->withCommonRules([
            'name' => ['required'],
        ]);
    }
}
<?php

namespace App\Http\Requests;

use Raid\Core\Request\Requests\FormRequest;
use App\Traits\Request\WithUserCommonRules;

class UpdateUserRequest extends FormRequest
{
    use WithUserCommonRules;

    /**
     * Get the validation rules that apply to the request.
     */
    public function rules(): array
    {
        return $this->withCommonRules([
            'name' => ['sometimes'],
        ]);
    }
}

Now both requests inherit the common rules and attributes for all the common rules defined, and each one has its own rules as well.

The withCommonRules method is responsible for merging the common rules with the request rules.

The withCommonRules method accepts an array of rules, and it will merge it with the common rules.

Remember that all the common rules will be inherited by all the requests that use the common rules' trait.


With Only Common Rules

To only merge the common rules with the request rules, you can use the withOnlyCommonRules method.

<?php

namespace App\Http\Requests;

use Raid\Core\Request\Requests\FormRequest;
use App\Traits\Request\WithUserCommonRules;

class CreateUserRequest extends FormRequest
{
    use WithUserCommonRules;

    /**
     * Get the validation rules that apply to the request.
     */
    public function rules(): array
    {
        return $this->withOnlyCommonRules([
            'name' => ['required'],
        ]);
    }
}

The withOnlyCommonRules method is responsible for merging the common rules with the request rules and ignoring all other rules.

This will merge only the name rules with the common rules for name, and ignore all other common rules defined.

And that's it.

License

The MIT License (MIT). Please see License File for more information.

Credits

Security

If you discover any security-related issues, please email instead of using the issue tracker.

About Raid

Raid is a PHP framework created by Mohamed Khedr

and it is maintained by Mohamed Khedr.

Support Raid

Raid is an MIT-licensed open-source project. It's an independent project with its ongoing development made possible.