What are you actually trying to accomplish?
Check out the RouteServiceProvider, you could add an additional mapping (for example with a testing.php) and only call that method if your environment is "testing".
Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.
Hi there, I'd like to create a route in testing, I tried to inject the route to the service container but it didn't work for me, any idea:
## TestCase
protected function setUp()
{
parent::setUp();
$this->app->get('router')->get('/fifi', function() {
return 'Hello World';
});
}
#RouteTest
public function test_route()
{
$this->get('/fifi')->assertStatus(404); // Returns 404
}
Any idea?
Short answer: you can't create routes by calling the router in that way.
Laravel loads the application routes by looking at the RouteServiceProvider or any equivalent classes, this means that all the routes MUST to be defined into a service provider.
Take a look at the config/app.php and you'll see something like this:
/*
* Application Service Providers...
*/
...
App\Providers\RouteServiceProvider::class,
...
Let's see what Laravel documentation says about the Service Providers:
Service providers are the central place of all Laravel application bootstrapping. But, what do we mean by "bootstrapped"? In general, we mean registering things, including registering service container bindings, event listeners, middleware, and even routes. Service providers are the central place to configure your application.
https://laravel.com/docs/providers
Your code does not work because it's telling Laravel to create something that should be ready when the application is bootstraped via service providers, this is a prior step.
protected function mapWebRoutes()
{
if (! App::environment('testing')) {
return ;
}
Route::middleware('web')->namespace($this->namespace)->group(base_path('routes/testing.php'));
}
Best, Ahmad
Please or to participate in this conversation.