Why don't you pass argument to the function and use it ? Why protected instead of public ? And maybe even the name should be consistent
May 10, 2018
8
Level 1
RouteServiceProvider
Hi All,
i have been following a Laravel tutorial, which routes pages dynamically to the front-end. The tutorial I am following uses a slightly older version of Laravel which I am trying to update into the latest version.
From what I can see the RouteServiceProvider has changed a little which leaves me a little unsure how best to proceed. I'm looking for a little guidance on how this can be updated.
Thanks,
MoFish
Tutorial Code which works:
public function map(Router $router)
{
$router->group(['namespace' => $this->namespace], function ($router) {
require app_path('Http/routes.php');
});
if (! app()->runningInConsole()) {
foreach (Page::all() as $page) {
$router->get($page->uri, ['as' => $page->name, function () use ($page, $router) {
return $this->app->call('SundaySim\Http\Controllers\PageController@show', [
'page' => $page,
'parameters' => $router->current()->parameters()
]);
}]);
}
}
}
My attempt of updating the above - which doesn't work.
protected function mapWebRoutes()
{
Route::middleware('web')
->namespace($this->namespace)
->group(base_path('routes/web.php'));
foreach (Page::all() as $page) {
Route::get($page->uri, ['as' => $page->name, function () {
return $this->app->call('App\Http\Controllers\PageController@show', [
'page' => $page,
'parameters' => $router->current()->parameters()
]);
}]);
}
}
Level 50
give it a try :)
protected function mapWebRoutes()
Route::get($page->uri, ['as' => $page->name, function () use($page) {
return $this->app->call('App\Http\Controllers\PageController@show', [
'page' => $page,
'parameters' => Route::current()->parameters()
]);
}]);
Please or to participate in this conversation.