Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel - Route pattern cannot reference variable name more than once

I am working on a legacy code, a project built with Laravel 5.2, and I am getting an error:

Route pattern "/api/v0/taxonomy/{term}/{{term}}" cannot reference variable name "term" more than once.

For this route:

/post/106

This is are my routes:

Route::group(['prefix' => 'api'], function() {
    Route::group(['prefix' => 'v0'], function () {
        Route::get('route/{a?}/{b?}/{c?}/{d?}', 'DynamicRouteController@resolve');
        Route::get('id/{id}', 'DynamicRouteController@resolveId');
        Route::get('search', 'SearchController@search');
        Route::resource('taxonomy/{term}','TaxonomyController');
    });
});

Not sure, why am I getting this error?

like image 421
Leff Avatar asked Dec 13 '22 22:12

Leff


2 Answers

When you define a route as a resource then Laravel seems to create all the routes necessary for your resource: GET, POST, PATCH, DELETE.

So you would just need to define Route::resource('taxonomy','TaxonomyController'); or Route::resource('taxonomy.post','TaxonomyPostController');

Check docs

like image 188
ka_lin Avatar answered May 09 '23 12:05

ka_lin


You just need rename your route parameter like this:

Route::resource('taxonomy/{term}','TaxonomyController', ['parameters'
=> ['{term}' => 'your_name']]);

be careful!!!!. you need insert bracket. this will cause wrong result:

Route::resource('taxonomy/{term}','TaxonomyController', ['parameters'
=> ['term' => 'your_name']] );

attention: 'your_name' should not be same as your parameter so this method make wrong result.

Route::resource('taxonomy/{term}','TaxonomyController', ['parameters'
    => ['{term}' => 'term']});
like image 38
اسماعیل زارع Avatar answered May 09 '23 10:05

اسماعیل زارع