Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

multiple prefix with the same route group

Tags:

im writing a fairly simple website for a school ... this website has news , articles , video clips ... etc

the way it works is in the home page we present visitor with some lessons like

>math  >geography  >chemistry  

user selects 1 on these and website contents changes based on the user selection

for example if user selects math he will see news , article , videos about math and so on ... right now this is what im doing (pleas ignore syntax errors)

Route::group(['prefix'=>'math'], function () {     Route::get('/news', 'NewsController@index')->name('news_index');     Route::get('/article', 'ArticleController@index')->name('article_index'); });  Route::group(['prefix'=>'geography'], function () {     Route::get('/news', 'NewsController@index')->name('news_index');     Route::get('/article', 'ArticleController@index')->name('article_index'); });  Route::group(['prefix'=>'chemistry'], function () {     Route::get('/news', 'NewsController@index')->name('news_index');     Route::get('/article', 'ArticleController@index')->name('article_index'); }); 

basically repeating all links for each prefix .... but as the links grow it will become more and more unmanageable ... is there any better way to do this ? something like

Route::group(['prefix'=>['chemistry','math' , 'geography' ], function () {     Route::get('/news', 'NewsController@index')->name('news_index');     Route::get('/article', 'ArticleController@index')->name('article_index'); }); 

------------------------- update -------------

i've tried this

$myroutes =  function () {     Route::get('/news', 'NewsController@index')->name('news_index');     Route::get('/article', 'ArticleController@index')->name('article_index'); };  Route::group(['prefix' => 'chemistry'], $myroutes); Route::group(['prefix' => 'math'], $myroutes); Route::group(['prefix' => 'geography'], $myroutes); 

and it works fine , the problem is the last prefix gets attached to all the internal links

for example if i click on math

my links will be

site.com/math/news

but all the links on the loaded page like

<a href="{{route('article_index')"> link to article </a> 

look like

site.com/geography/article

basically link get the last mentioned prefix regardless of currently selected one

like image 393
hretic Avatar asked Aug 22 '17 04:08

hretic


People also ask

What is prefix in route?

A route announcement is sometimes referred to as a 'prefix'. A prefix is composed of a path of AS numbers, indicating which networks the packet must pass through, and the IP block that is being routed, so a BGP prefix would look something like: 701 1239 42 206.24.

What is Route grouping?

Route groups allow you to share route attributes, such as middleware, across a large number of routes without needing to define those attributes on each individual route.

What is prefix in laravel routes?

Path prefixes are used when we want to provide a common URL structure. We can specify the prefix for all the routes defined within the group by using the prefix array option in the route group.


2 Answers

Why not do it this way:

$subjects = [     'chemistry', 'geography', 'math' ];  foreach ($subjects as $subject) {     Route::prefix($subject)->group(function () {         Route::get('news', 'NewsController@index')->name('news_index');         Route::get('article', 'ArticleController@index')->name('article_index');     }); } 

I know this is an elementary way do to it. Yet you can easily add subjects, it is clear and effortless to understand.

Update

As pointed in the comments it could be convenient to name the route as per subject, here is how to do this:

$subjects = [     'chemistry', 'geography', 'math' ];  foreach ($subjects as $subject) {     Route::prefix($subject)->group(function () use ($subject) {         Route::get('news', 'NewsController@index')->name("{$subject}_news_index");         Route::get('article', 'ArticleController@index')->name("{$subject}_article_index");     }); } 
like image 148
louisfischer Avatar answered Oct 09 '22 16:10

louisfischer


I think it's better to do:

Route::get('/news/{group}', 'NewsController@index')->name('news_index')->where('group', 'math|geography|chemistry'); 

And then just put condition on the controller function whether it is geography/math/chemistry/etc.

Don't you think?

like image 25
Wreigh Avatar answered Oct 09 '22 16:10

Wreigh