Estoo
Estoo

Reputation: 96

Laravel Package Development - Need to extend the FormRequest class, but don't want to require the entire laravel framework package

Introduction

I am developing a Laravel package that will generate request rules based on the EditorJS blocks present in the request. To offer maximum developer convenience, i offer a EditorJSFormRequest class that extends the FormRequest class. This class builds the rules for defined block types, and can then be used as a regular FormRequest class inside a controller function.

My Problem

The FormRequest class comes from Illuminate\Foundation\Http\FormRequest, which unfortunately isn't a standalone package, but requires the entire laravel/framework package. To me, requiring the entire framework seems like a bad practice, and i wonder if there is any other way. If not, i am already doubting if my solution is coded in a logical manner, meaning i also wonder if i need to change the entire code.

Context

Given that i am starting to doubt if i am on the right path, i want to offer some context to give you an idea what i am doing, this is the EditorJSFormRequest class code:

namespace FurisonTech\LaraveditorJS;

use Illuminate\Foundation\Http\FormRequest;

abstract class EditorJSFormRequest extends FormRequest
{
    protected array $editorJSFieldRuleBuilders = [];

    /**
     * EditorJSFormRequest constructor.
     * @param array<EditorJSRequestFieldRuleBuilder> $editorJSFieldRuleBuilders
     */
    public function __construct(array $editorJSFieldRuleBuilders = [])
    {
        parent::__construct();
        $this->editorJSFieldRuleBuilders = $editorJSFieldRuleBuilders;
    }

    /**
     * Get the validation rules that apply to the request.
     * @return array
     */
    final public function rules(): array
    {
        $rules = [];

        // Build rules for each Editor.js field
        foreach ($this->editorJSFieldRuleBuilders as $builder) {
            $rules = array_merge($rules, $builder->buildRules($this));
        }

        // Merge with additional rules
        return array_merge($rules, $this->additionalRules());
    }

    /**
     * Additional validation rules.
     * @return array
     */
    abstract protected function additionalRules(): array;
}

Now a developer can create a class that extends EditorJSFormRequest instead of FormRequest, like:

class SaveArticleRequest extends EditorJSFormRequest
{

    private const ALLOWED_EMBED_SERVICES = ['youtube', 'twitter', 'instagram', 'facebook', 'imgur'];

    public function __construct()
    {
        parent::__construct([
            new EditorJSRequestFieldRuleBuilder('article', [
                'table' => new TableBlockRulesSupplier(200, 20, 255, 3),
                'header' => new HeaderBlockRulesSupplier(255, null),
                'paragraph' => new ParagraphBlockRulesSupplier(2500, null),
                'image' => new ImageBlockRulesSupplier(255, 10),
                'audioPlayer' => new AudioPlayerBlockRulesSupplier(12),
                'embed' => new EmbedBlockRulesSupplier(self::ALLOWED_EMBED_SERVICES, 255, 5),
                'list' => new ListBlockRulesSupplier(100, 500, null),
            ])
        ]);
    }

    /**
     * Determine if the user is authorized to make this request.
     */
    public function authorize(): bool
    {
        return false;
    }

    protected function additionalRules(): array
    {
        return [];
    }
}

Is it okay to require the laravel/framework package for this cause or should i do this another way?

Upvotes: 1

Views: 61

Answers (1)

puklipo
puklipo

Reputation: 538

Use orchestra/testbench to develop packages for Laravel.
https://github.com/orchestral/testbench

Use illuminate/support to restrict the Laravel version.

This is the minimum knowledge required for package developers.

  "require": {
    "php": "^8.1",
    "illuminate/support": "^10.0||^11.0"
  },
  "require-dev": {
    "orchestra/testbench": "^8.0||^9.0"
  },
  "extra": {
    "laravel": {
      "providers": [
        "My\\Package\\MyServiceProvider"
      ]
    }
  },

FormRequest is always present in Laravel, so you don't need to worry about it.

Lumen no longer needs to be supported.

Upvotes: 0

Related Questions