Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

MoFish's avatar

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()
                ]);
            }]);
        }
   }
0 likes
8 replies
Vilfago's avatar

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

rin4ik's avatar

use page and router

 Route::get($page->uri, ['as' => $page->name, function () use ($page, $router)  {
MoFish's avatar

Hi rin4ik,

I tried this before but it mentions

** Undefined variable: router **

Sorry, i am missing something simple here i'm sure....

rin4ik's avatar
protected function mapWebRoutes(Router $router)
 $router->get($page->uri, ['as' => $page->name, function () use ($page, $router)  {
MoFish's avatar

Hi rin4ik,

Thanks for taking the time to help.

I have tried that prior and run into the following issue:

Type error: Too few arguments to function App\Providers\RouteServiceProvider::mapWebRoutes(), 0 passed in C:\xampp\htdocs\laravel\app\Providers\RouteServiceProvider.php on line 41 and exactly 1 expected
rin4ik's avatar
rin4ik
Best Answer
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()
                ]);
            }]);
MoFish's avatar

Thanks rin4ik,

Fixed the issue.

Please or to participate in this conversation.