mauricius / laravel-htmx

Laravel helper library for Htmx
MIT License
301 stars 14 forks source link

Usage in middleware #19

Closed PerryRylance closed 4 months ago

PerryRylance commented 4 months ago

I'm trying to establish if a request was made by HTMX in some middleware.

I have installed the package and published the config.

My middleware is receiving a regular Request and not a HtmxRequest so trying to call the helper methods causes an exception to be thrown.

Have I done something wrong here or is this expected?

mauricius commented 4 months ago

I'm afraid that is expected. The parameters of the handle method of the middleware are not resolved out of the Service Container, therefore the package doesn't have any control there.

What you can do is manually wrap the $request like this

public function handle(Request $request, Closure $next)
{
    $htmxRequest = HtmxRequest::createFrom($request);

    return $next($htmxRequest);
}
PerryRylance commented 4 months ago

Thank you!