PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Monday, January 24, 2022

[FIXED] multiple prefix with the same route group

 January 24, 2022     laravel, laravel-5.4, php     No comments   

Issue

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


Solution

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


Answered By - louisfischer
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing