DEV Community

marius-ciclistu
marius-ciclistu

Posted on • Originally published at marius-ciclistu.Medium on

Maravel-Framework 10.64 Brings Resolving Events Cache And FormRequest to Maravel


Maravel-Framework

Because Maravel-Framework is a DI centered framework, the efficiency of its container is crucial.

Version 10.64 introduces FormRequest in Maravel Micro-Framework 10.52.25.

To use it with autowiring in the controller’s method , the bellow code must be uncommented in the \App\Application.

/**
 * Uncomment to use \App\FormRequest
 * @inheritdoc
 * Used to avoid Illuminate\Support\ServiceProvider\FormRequestServiceProvider::boot
 */
protected function fireResolvingCallbacks($abstract, $object)
{
    $this->fireCallbackArray($object, $this->globalResolvingCallbacks);

    /** This avoids Illuminate\Support\ServiceProvider\FormRequestServiceProvider::boot */
    if ($object instanceof \Illuminate\Http\FormRequest) {
        \Illuminate\Http\FormRequest::createFrom($this['request'], $object);

        $object->setContainer($this);
    }

    $this->fireCallbackArray(
        $object,
        $this->getResolvingCallbacksForType($abstract, $object)
    );

    /** This avoids Illuminate\Support\ServiceProvider\FormRequestServiceProvider::boot */
    if ($object instanceof \Illuminate\Contracts\Validation\ValidatesWhenResolved) {
        $object->validateResolved();
    }

    $this->fireAfterResolvingCallbacks($abstract, $object);
}
Enter fullscreen mode Exit fullscreen mode

The reason it is commented is to avoid those two ifs executing on every resolve if FormRequest is not used.

Still, the FormRequest can be used without the above code with maravel-rest-wizard or laravel-crud-wizard-free libs via a base Controller class:

    protected function validateCreateRequest(Request $request): array
    {
        $formRequest = ResourceRequest::createFrom($request, new ResourceRequest(resource: $this->label));
        $formRequest->setContainer(\app());// notice the diff vs Maravelith, no ->setRedirector($app->make(Redirector::class));
        $formRequest->validateResolved();

        return $formRequest->validated();
    }
Enter fullscreen mode Exit fullscreen mode

Example for Maravelith/Laravel : https://github.com/macropay-solutions/laravel-crud-wizard-demo/blob/67b47f13ea6bae41698b46f9c788e7476a92883c/app/Http/Controllers/ResourceController.php#L79

This will allow maravel-rest-wizard-decorator or laravel-crud-wizard-decorator-free libs to decorate the error messages keys. If the ValidationException is thrown from the method autowiring, the decorator middleware will not catch it, resulting in undecorated keys being presented on response.

If the FormRequest is NOT used via autowire in Maravelith , the above method can be overridden in \App\Application with the container’s version that does not contain those 2 extra ifs, speeding up the container.

Version 10.64 of Maravel-Framework removed the \Illuminate\Foundation\Providers\FormRequestServiceProvider, leading to faster registration and boot times.

Please note that using $app->beforeResolving, $app->resolving or $app->afterResolving events, the speed of the container decreases because for each resolve, all events are looped to search via “===” or “instanceof/is_subclass_of” for the ones that need to be triggered.

To partially speed up this selection, Maravel-Framework caches (via autowiring:cache coupled with in memory cache) the types of these resolving events as abstract to types as key map:


//Maravelith
<?php return [
    'resolving' => ['Illuminate\\Console\\OutputStyle' => ['Illuminate\\Console\\OutputStyle' => 0]],
    'afterResolving' => ['migrator' => ['migrator' => 0]],
];
//Maravel
<?php return [];
Enter fullscreen mode Exit fullscreen mode

As you can see, the bare bone templates don’t have much here. The Maravelith lacks the FormRequest from resolving and ValidatesWhenResolved from afterResolving that were previously registered in \Illuminate\Foundation\Providers\FormRequestServiceProvider::boot.

Maravelith version 10.52.16 comes with commented VerifyCsrfToken on the web routes. This middleware must be placed on each route that expects it, not on all routes from web. GET routes don’t need it.

protected $middleware = [
    // \App\Http\Middleware\TrustHosts::class,
    \App\Http\Middleware\TrustProxies::class,
    \Illuminate\Http\Middleware\HandleCors::class,
    \App\Http\Middleware\PreventRequestsDuringMaintenance::class,
    \Illuminate\Foundation\Http\Middleware\ValidatePostSize::class,
    //\Illuminate\Foundation\Http\Middleware\ConvertEmptyStringsToNull::class,
];

/**
 * The application's route middleware groups.
 *
 * @var array<string, array<int, class-string|string>>
 */
protected $middlewareGroups = [
    'web' => [
        \App\Http\Middleware\TrimStrings::class,
        \App\Http\Middleware\EncryptCookies::class,
        \Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse::class,
        \Illuminate\Session\Middleware\StartSession::class,
        \Illuminate\View\Middleware\ShareErrorsFromSession::class,
        //\App\Http\Middleware\VerifyCsrfToken::class,
        //\Illuminate\Routing\Middleware\SubstituteBindings::class,
    ],

    'api' => [
        \App\Http\Middleware\TrimStrings::class,
        // \Laravel\Sanctum\Http\Middleware\EnsureFrontendRequestsAreStateful::class,
        //\Illuminate\Routing\Middleware\ThrottleRequests::class . ':api',
        //\Illuminate\Routing\Middleware\SubstituteBindings::class,
    ],
];
Enter fullscreen mode Exit fullscreen mode

Benchmark for this version does not bring big improvements in a Hello World request because the cache does not have much to improve on:

The docs were updated to reflect this change: https://macropay-solutions.github.io/maravel-docs/validation.html

Update 2026.02.27
Version 10.64.5 includes in the cache also the beforeResolving and also it caches the types when they don't have events.

Top comments (0)