frenchbaguette
frenchbaguette

Reputation: 1339

One route working and another not working

I have these two routes:

Route::get('delete/{user}', array('as' => 'delete', 'uses' => 'AdminController@getDeleteUser'));
Route::get('delete/{article}', array('as' => 'delete', 'uses' => 'AdminController@getDeleteArticle'));

If I put 'delete/{user}' first then its working. If I put it below 'delete/{article}' them working delete article. How can I make them both work ?

Upvotes: 1

Views: 41

Answers (2)

Nehal Hasnayeen
Nehal Hasnayeen

Reputation: 1223

anything between curly braces is a wildcard parameter, so declaring a route with 'delete/{user}' means anything after 'delete/' will work same as that route, so to achieve what you want do like this

Route::get('delete/user/{id}', array('as' => 'deleteUser', 'uses' => 'AdminController@getDeleteUser'));
Route::get('delete/article/{id}', array('as' => 'deleteArticle', 'uses' => 'AdminController@getDeleteArticle'));

also you can use regular expression for same route like this

Route::get('delete/{user}', function ($name) {
    return "a";
})->where('name', 'regular expression here');
Route::get('delete/{article}', function ($id) {
    return "b";
})->where('id', 'regular expression here');

Upvotes: 0

Ovidiu Badita
Ovidiu Badita

Reputation: 143

You should redesign your API. As it looks, you would do better if you would have routes such as

Route::get('user/delete/{id}', array('as' => 'deleteUser', 'uses' => 
    'AdminController@getDeleteUser'));
    Route::get('article/delete/{id}', array('as' => 'deleteArticle', 'uses' => 'AdminController@getDeleteArticle'));

You had 2 named routes with the same name.

Upvotes: 1

Related Questions