Reputation: 32028
According to Lavarel documentation, if I use subdomain routing for multiple subdomains, I have to pass the subdomain as the first argument of callbacks functions and controllers methods:
Route::domain('{sub}.example.org')->group(function () {
Route::get('a', function ($sub) { /* ... */ });
Route::get('b', function ($sub) { /* ... */ });
Route::get('c/{c}', function ($sub, $c) { /* ... */ });
Route::get('d/{d}', function ($sub, $d) { /* ... */ });
});
In other words, I have to carry the $sub
variable everywhere. Assuming I don't care about its value, can I avoid this and just do something like this(this does not work for multiple argument):
Route::domain('{sub}.example.org')->group(function () {
Route::get('a', function () { /* ... */ });
Route::get('b', function () { /* ... */ });
Route::get('c/{c}', function ($c) { /* ... */ });
Route::get('d/{d}', function ($d) { /* ... */ });
});
If I do this, $c
and $d
will be the value of the subdomain.
Assuming I don't care about the subdomain value and I have many routes, is there a way to ignore it?
Upvotes: 1
Views: 849
Reputation: 1037
We had a similar thing with a dynamical prefix. Our solution was:
First we unset the parameter if it existed in the "App/Http/Controllers/Controller.php", which is the class your controllers most likely extend.
public function callAction($method, $parameters)
{
if (isset($parameters['clientident'])) {
unset($parameters['clientident']);
}
return parent::callAction($method, $parameters);
}
This will unset the parameter for every function call in every controller and should do away with your problem.
But because we needed it in our route() function, we created a "App/Http/helpers.php" file for
function route($name, $parameters = [], $absolute = true)
{
if (!isset($parameters['clientident'])) {
// If the given value is not an array, wrap it in one.
$parameters = Arr::wrap($parameters);
if (Auth::check()) {
$temp = ['clientident' => Auth::user()->getClientIdent()];
}
else if (request()->clientident) {
$temp = ['clientident' => request()->clientident];
}
else {
$temp = ['clientident' => 'general'];
}
$parameters = array_merge($parameters, $temp);
}
return app('url')->route($name, $parameters, $absolute);
}
and added the new helpers file in the "bootstrap/autoload.php"
require __DIR__.'/../app/Http/helpers.php';
require __DIR__.'/../vendor/autoload.php'; << this line was already here
This works because Taylor wrote an check in front of every helper function if it is already set, so it's pretty easy to override them
if (! function_exists('route')) {
function route($name, $parameters = [], $absolute = true)
{
return app('url')->route($name, $parameters, $absolute);
}
}
is the code in the helpers.php in the Illuminate/Foundation directory.
I guess the route function has to be handled a little bit different in your case. You should be able to get the subdomain with domain helper functions and add it there as parameter back again, which is most likely much easier than our if elseif else case above.
Upvotes: 1