Sagar Chamling
Sagar Chamling

Reputation: 1068

Sub-domain's Route Name

My web.php looks like

Route::group(
    [
        'domain'     => '{tenant}.' . config('app.url'),
    ],
    function () {
        $this->get('/', 'HomeController@index')->name('home');
    }
);

My HomeController looks like

/**
 * Show the application dashboard.
 *
 * @param $tenant
 * @return \Illuminate\Http\Response
 */
public function index($tenant)
{
    return view('home', compact('tenant'));
}

In app.blade.php file looks like

<a href="{{ route('home', ['tenant', $tenant]) }}">home</a>

Using Sub-domain routing we've to pass wildcard {tenant} value every time in when we use route() else it pops out this error

(3/3) ErrorException
Missing required parameters for [Route: home] [URI: home].

This is redundant all over the controller as well as blade file. Is there any solution to bind the {wildcard} by default ?

Upvotes: 2

Views: 335

Answers (1)

Thomas Van der Veen
Thomas Van der Veen

Reputation: 3226

Make a new function that uses the existing route() function.

Example:

function mdroute ($routeName, $routeData = [])
{
    $tenant = request()->tenant;

    $routeData['tenant'] => $tenant;

    return route($routeName, $routeData);
}

Upvotes: 1

Related Questions