5

So when you define a resource controller in a wildcard subdomain group route similar to this:

Route::group(array('domain' => '{subdomain}.example.com'), function() {
  Route::resource('users', 'UserController');
});

on RouteServiceProvider

$router->model('user', 'App\User');

and on the UserController show method:

public function show($user)
{
    return $user;
}

what i get is the subdomain name instead of the user resource. This is because the subdomain parameter is passed to controller methods and i would have to change them like this:

public function show($subdomain, $user)
{
    return $user;
}

I simply don't want to add the subdomain parameter to each and every controller method in my app because i am not going to do anything with it. I use the subdomain parameter in a middleware to do some configuration changes.

How can i do it so the subdomain doesn't get passed to controllers as parameter?

2
  • Were you able to find any solution for this? Commented Aug 3, 2015 at 14:31
  • 1
    I gave up and ended up including the subdomain parameter on every controller method. Came up to be handy for some cases though. Especially when i needed to pass the parameter to the views. Commented Nov 16, 2017 at 20:15

2 Answers 2

1

I'm aware that this question is somewhat old (and potentially stale) although I stumbled across this post when searching for an answer to another route model binding question.

To avoid requiring the subdomain, you can specify that Laravel forgets that route parameter.

You could either do this in a middleware (which checks for the subdomain too), like so:

$request->route()->forgetParameter('subdomain');

Alternatively, using your code snippet, it would look something along the lines of this:

Route::group(array('domain' => '{subdomain}.example.com'), function() {
  Route::forgetParameter('subdomain');
  Route::resource('users', 'UserController');
});

However, I'd strongly recommend that the process is moved into a middleware, as it doesn't feel right putting that in the routes file.

Sign up to request clarification or add additional context in comments.

Comments

0

In your controller function you can ignore the $dubdomain if you do not use it and make sure you typehint the User like this

public function show(User $user)
{
    return $user;
}

5 Comments

Thank you Margus. That makes sense but it doesn't feel like a good way of doing it. It may cause confusion for nested resources. I prefer to simply make router ignore the subdomain parameter.
I just tested and you can ignore the $subdomain if you do not use it. As you have User then you need to typehint it to be resolved.
I get Argument 1 passed to App\Http\Controllers\UserController::show() must be an instance of App\User, string given when i typehint User. Assuming that string is the subdomain passed.
It must be something related to model binding then and needs deeper investigation. I am able to typehint and get resolved Eloquent Model values and skip other url params.
@MargusPala how to solve it if there is a model binding on route service provider? Perhaps you have this $router->bind('users', function($id) { return \App\User::findOrFail($id); }); and on methods there is already the type hint doSomething(User $user) { ... again laravel asks for object and the subdomain is taking place of it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.