user5621153
user5621153

Reputation:

Laravel group routing issue

Laravel routing issue while using the contrain on parameter with routing without ->where('id', '[0-9]+'); working but I need to add this.

Route::group(['prefix' => '/{id}'], function () { 
    Route::post('/edit', 'User\InstituteController@editInstitute');
})->where('id', '[0-9]+');

Which causes the following error

Call to a member function where() on a non-object

Upvotes: 3

Views: 555

Answers (2)

Yoram de Langen
Yoram de Langen

Reputation: 5499

where() can only applied on get, resource and post etc. See documentation here and the section Regular Expression Constraints.

So:

get('user/{name}', function ($name) { 
    // code
})->where('name', '[A-Za-z]+');

// post request
$router->post(...)->where(['id' => '[0-9]+', 'name' => '[a-z]+']);

// put request
Route::put(...)->where(...);
// delete request
$router->delete(...)->where(...);

And apply for groups see the awnser of @izupet

For global contraints you could use Route::pattern('id', '[0-9]+') or $router->pattern('id', '[0-9]+');

Or see the Router class: vendor/laravel/framework/src/Illuminate/Routing/Router.php

Upvotes: 0

izupet
izupet

Reputation: 1559

This is the proper way. This way you can define multiple where conditions. Not tested but hope you get the idea.

Route::group([
    'prefix' => '/{id}',
    'where'  => [
      'id' => '[0-9]+'
    ]
  ], 
  function () { 
    Route::post('/edit', 'User\InstituteController@editInstitute');
});

Upvotes: 2

Related Questions