Ad

How To Get Subdomain Inside RouteServiceProvider Map() Method While Phpunit Testing?

- 1 answer

Is there a way to get the subdomain while running phpunit test in Laravel?

And more specific in this method:

app/Providers/RouteServiceProvider.php

/**
 * Define the routes for the application.
 *
 * @return void
 */
public function map()
{
    $this->mapApiRoutes();
    $this->mapWebRoutes();

    // inside here get subdomain while running a phpunit test??

So if I make some dummy test like this:

/** @test */
public function user_can_see_welcome_page()
{
    $response = $this->call('GET', 'https://subdomain.domain.com');

    $response->assertStatus(200);
}

I want to get the subdomain inside that map() method of RouteServiceProvider

Ad

Answer

In order to change the domain you can add it to the phpunit.xml to set it globally:

<php>
    <env name="APP_URL" value="https://subdomain.domain.com"/>
</php>

But discussing your problem in the comments, this is the actual solution to your problem:

Multiple route files:

You could split each subdomain in its own map method and route file and register them in your RouteServiceProvider.

for each subdomain:

protected function mapSubDomainRoutes()
{
    Route::group([
        'middleware' => 'web',
        'domain' => 'subdomain.domain.com',
        'namespace' => $this->namespace,
    ], function () {
        require base_path('routes/subdomain.domain.php');
    });
}

Single route file:

Or if you have everything inside one route file you can wrap the routes inside a group:

Route::group(['domain' => ['subdomain.domain.com']], function () {
    // domain specific routes
});
Ad
source: stackoverflow.com
Ad