2

My subdomains are

domain1 = dev1.myapp.com,
domain2 = dev2.myapp.com,
domain3 = dev3.myapp.com
...

Using below code causing problem with first parameter in laravel controller,

> Route::group(array('domain' => '{account}.myapp.com'), function() {
> Route::get('/get_data/{id?}', 'DataController@getData');
> })

I am getting subdomain value(dev1,dev2,dev3) instead of $id value in controller in getData method.

How to update my code to allow all subdomain, without making subdomain as first parameter in each method of controller.

Please share your idea.

9
  • Make sure you've bind {account} with your model. Commented May 21, 2016 at 7:15
  • @RifkiAriaGumelar : You mean in controller's method getData ? Commented May 21, 2016 at 7:19
  • I don't need $account anywhere so should I need to include in all the sub route ? Commented May 21, 2016 at 7:21
  • Let's make it more clear, do you want to get your subdomains based on user id something like 1.myapp.com? Commented May 21, 2016 at 7:24
  • @RifkiAriaGumelar, It will be like dev1.myapp.com/get_data/1 or dev2.myapp.com/get_data/1 Commented May 21, 2016 at 7:28

1 Answer 1

11

Since you don't want to use {account} variable on your controller methods, you can define your routes in a variable and pass it to each your subdomain group, here is the example:

$subdomainRoutes = function () {
    Route::get('get_data/{id?}', function ($id) {
        //
    });
};

Route::group(['domain' => 'dev1.myapp.com'], $subdomainRoutes);
Route::group(['domain' => 'dev2.myapp.com'], $subdomainRoutes);
Route::group(['domain' => 'dev3.myapp.com'], $subdomainRoutes);

EDIT

If your sub domains are dynamic then you can use a middleware, create a middleware something like:

namespace App\Http\Middleware;

use Closure;

class SubDomainAccess
{
    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @return mixed
     */
    public function handle($request, Closure $next)
    {
        $server = explode('.', $request->server('HTTP_HOST'));
        $subdomain = $server[0];

        // check if sub domain exists, replace with your own conditional check
        if (! Account::where('slug', $subdomain)->first()) {
            return abort(404); // or redirect to your homepage route.
        }

        return $next($request);
    }
}

Register your middleware in Kernel.php

'subdomain' => \App\Http\Middleware\SubDomainAccess::class,

Then use it on your routes.php

Route::group(['middleware' => 'subdomain'],  function () {
    Route::get('/get_data/{id?}', 'DataController@getData');
});
Sign up to request clarification or add additional context in comments.

1 Comment

dev1, dev2 & dev3 all are dynamic and there would be many more accounts like this.

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.