lepikhinb / momentum-modal

MIT License
442 stars 26 forks source link

Allow swapping the modal implementation #66

Open ragulka opened 11 months ago

ragulka commented 11 months ago

This PR leverages the Laravel Service Container to create new instances of the Modal class. This allows developers to customize the Modal class by binding their own implementation to the container.

A concrete use case is to customize the Modal::handleRoute method to add additional middleware to the base route. For example, I need to forget a specific route parameter after a middleware runs, but by default Momentum Modal does not apply middleware to the base route at all (except for resolving the route parameters).

This change will not affect existing usage at all, but will make it easier (possible) to extend the existing functionality.

what-the-diff[bot] commented 11 months ago

PR Summary

darkons commented 11 months ago

I have a similar problem. In my case I am using the stancl/tenancy package and I need to apply the middleware for tenant identification on the base route.

Also, I think it could be very useful to add a config file to the package where you can add middlewares that you want to be always running:

// config/momentum-modal.php
'middlewares' => [
    Stancl\Tenancy\Middleware\InitializeTenancyByDomain::class,
    AnotherRandomMiddleware::class,
]
protected function handleRoute(Request $request, Route $route): mixed
    {
        /** @var \Illuminate\Routing\Router */
        $router = app('router');

        foreach (config('momentum-modal.middlewares') as $middleware) {
            $instance = App::make($middleware);

            if (method_exists($instance, 'handle')) {
                $instance->handle($request, fn () => $route->run());
            }
        }

        $middleware = new SubstituteBindings($router);

        return $middleware->handle(
            $request,
            fn () => $route->run()
        );
    }