Zahra Badri
Zahra Badri

Reputation: 2034

How can i get some data before route and send it into route in laravel?

I have different domains with different routes in database and I want send every domain to a specific route. How can I do that? In which file I can get data from database that occur before route to decide which controller and method calls. Can I have a variable ($domains) in web.php like this :

foreach ($domains as $domain) {
    Route::group(array('domain' => $domain['domain']), function() use ($domain) {
        Route::get('/', '' . $domain['controller'] . '@' . $domain['method']);
    });
}

Upvotes: 0

Views: 466

Answers (1)

user8555937
user8555937

Reputation: 2387

You should use the service provider (boot) for that. Go to /app/Providers/AppServiceProvider.php and dispatch the following code:

<?php

namespace App\Providers;

use App\MyDomainsModel;
use Illuminate\Support\Facades\Route;

use Illuminate\Support\ServiceProvider;

class AppServiceProvider extends ServiceProvider
{
    /**
     * Register any application services.
     *
     * @return void
     */
    public function register()
    {
        //
    }

    /**
     * Bootstrap any application services.
     *
     * @return void
     */
    public function boot()
    {
        // get all domains
        $Domains = MyDomainsModel::all();

        // register the routes
        foreach($Domains as $Domain)
            Route::group(['domain' => $Domain['domain']] , function()use($Domain) {
                Route::get('/', $Domain['controller'].'@'.$Domain['method']);
            });    
    }
}

Upvotes: 2

Related Questions