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
0 Comments:
Post a Comment
Note: Only a member of this blog may post a comment.