How to filter wildcard subdomains? Simple and faster.

Submitted by neftaliyagua - 6 years ago

This a simple way to use wilcard routing, for specific options.

Route::domain('{state}.domain.tld')->group(function () {
      Route::get('/', function ($state) {
        return "index of: ".$state;
      })->where('state', 'distrito-capital|amazonas|anzoategui|apure|aragua|barinas|bolivar|carabobo|cojedes|delta-amacuro|falcon|guarico|lara|merida|miranda|monagas|nueva-esparta|portuguesa|sucre|tachira|trujillo|vargas|yaracuy|zulia|federales-federales');
      Route::get('/Otro', function ($state) {
        return "Other action for:".$state;
      })->where('state', 'distrito-capital|amazonas|anzoategui|apure|aragua|barinas|bolivar|carabobo|cojedes|delta-amacuro|falcon|guarico|lara|merida|miranda|monagas|nueva-esparta|portuguesa|sucre|tachira|trujillo|vargas|yaracuy|zulia|federales-federales');
    });

    Route::domain('.domain.tld')->group(function () {
      Route::get('/', function () {
        return "index";
      });
      Route::get('/Otro', function () {
        return "other";
      });
    });
    // Another universal route
    Route::get('/another', function () {
        return "another";
    });

//  Result: 
//    http://venezuela.gob.ve -> index
//    http://venezuela.gob.ve/Otro -> other
//    http://venezuela.gob.ve/algo -> algo
//    http://lara.venezuela.gob.ve -> index for: lara
//    http://lara.venezuela.gob.ve/Otro -> Other for: lara
//    http://lara.venezuela.gob.ve/algo -> algo
//    http://yaracuy.venezuela.gob.ve -> index for: yaracuy
//    http://yaracuy.venezuela.gob.ve/Otro -> Other for: yaracuy
//    http://yaracuy.venezuela.gob.ve/algo -> algo
//    http://another.venezuela.gob.ve -> Error 404
//    http://another.venezuela.gob.ve/Otro -> Error 404
//    http://another.venezuela.gob.ve/another -> another