3

Brief:

I have a custom routes group with a dynamic prefix:

Route::prefix('{nickname}')->group(function () {
    Route::get('/', function($nickname) {
        return view('profile');
    })->where(['nickname' => '[a-z]+']);

    Route::get('/edit', function($nickname) {
        return view('profile.edit');
    })->where(['nickname' => '[a-z]+']);
});

As you can see, on each route I check the prefix correctness through a regex.

Note: I also used ->where(['nickname' => '[a-z]+']) to routes group and got an error.

Error message:

Call to a member function where() on null

Question:

How can I solve the problem with checking only once?

Karl Hill
  • 12,937
  • 5
  • 58
  • 95
Andreas Hunter
  • 4,504
  • 11
  • 65
  • 125

2 Answers2

6

Route::group has attributes paramether. One of available paramethers is where.

Route::group([
    'prefix' => '{nickname}',
    'where' => ['nickname' => '[a-z]+']
], function ($nickname) {
    Route::get('/', function($nickname) {
        return view('profile');
    });
    Route::get('/edit', function($nickname) {
        return view('profile.edit');
    });
});

More about Laravel routes here

Egretos
  • 1,155
  • 7
  • 23
0

use this hope it will help you

Route::group(['prefix' => '{nickname}','where' => ['nickname' => '[a-z]+']],function ($nickname) {

    Route::get('/', function($nickname) {
        return view('profile');
    });

    Route::get('/edit', function($nickname) {
        return view('profile.edit');
    });

});
Shailendra Gupta
  • 1,054
  • 6
  • 15