Skip to content

Commit ac1de0a

Browse files
committed
fix: ensure Filter receives current HTTP request when resolved from DI container
Fixes #30 - Request parameters not included in filter object pulled from DI container When using dependency injection in controllers like: public function index(PostFilter $postFilter) The filter methods were not being called because Laravel's DI container was creating a new empty Request instance instead of using the current HTTP request. Changes: - Add bindIf() in FilterableServiceProvider to ensure Request::class resolves to the current HTTP request ($app['request']) - Add comprehensive test suite for DI container resolution scenarios The fix ensures that DI-resolved filters behave identically to manually instantiated filters using new PostFilter(request()).
1 parent d5e35d7 commit ac1de0a

2 files changed

Lines changed: 444 additions & 0 deletions

File tree

src/Filterable/Providers/FilterableServiceProvider.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace Filterable\Providers;
44

55
use Filterable\Console\MakeFilterCommand;
6+
use Illuminate\Http\Request;
67
use Spatie\LaravelPackageTools\Package;
78
use Spatie\LaravelPackageTools\PackageServiceProvider;
89

@@ -18,6 +19,30 @@ public function configurePackage(Package $package): void
1819
->hasCommand(MakeFilterCommand::class);
1920
}
2021

22+
/**
23+
* {@inheritdoc}
24+
*/
25+
public function packageRegistered(): void
26+
{
27+
$this->registerFilterBindings();
28+
}
29+
30+
/**
31+
* Register contextual bindings for Filter classes.
32+
*
33+
* This ensures that when a Filter subclass is resolved from the DI container,
34+
* it receives the current HTTP request instance rather than an empty Request.
35+
*/
36+
protected function registerFilterBindings(): void
37+
{
38+
// Bind Request class to resolve to the current request from the container.
39+
// This ensures Filter subclasses get the active HTTP request instead of an empty one.
40+
// The rebinding allows the test suite to override with $app->instance() if needed.
41+
$this->app->bindIf(Request::class, function ($app) {
42+
return $app['request'];
43+
});
44+
}
45+
2146
/**
2247
* {@inheritdoc}
2348
*/

0 commit comments

Comments
 (0)