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?

2 Answers 2

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

Sign up to request clarification or add additional context in comments.

Comments

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');
    });

});

2 Comments

I first tried this method and then asked a question. @Shaielndra Gupta
Current method return Call to a member function where() on null

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.