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

xoctopus's avatar

How extend Illuminate\Routing\Router class?

I'm trying to extend class Illuminate\Routing\Router to be able to modify the toResponse method. So far this is what I have done:

I have created a class called Router that extends class Illuminate\Routing\Router

<?php

namespace App\Http\Responses;

use Illuminate\Routing\Router as BaseRouter;

class Router extends BaseRouter
{
    /**
     * Static version of prepareResponse.
     *
     * @param $request
     * @param $response
     * @return \Symfony\Component\HttpFoundation\Response
     */
    public static function toResponse($request, $response)
    {
        if ($response instanceof HttpResponse) {
            return 'Hello instance of HttpResponse';
        }

        return parent::toResponse($request, $response);
    }
}

I have created a service provider named RoutingServiceProvider with the following structure:

<?php

namespace App\Providers;

use Illuminate\Foundation\Application;
use Illuminate\Routing\Router;
use Illuminate\Support\ServiceProvider;

class RoutingServiceProvider extends ServiceProvider
{
    /**
     * Register services.
     */
    public function register(): void
    {
        $this->app->extend('router', function (Router $router, Application $app) {
            return new \App\Http\Responses\Router($app['events'], $app);
        });
    }
}

Then, I have registered the service provider inside the Kernel file, as follows:

<?php

namespace App\Http;

use Illuminate\Routing\Router;
use Illuminate\Contracts\Foundation\Application;
use App\Providers\RoutingServiceProvider;

class Kernel extends HttpKernel
{
     ....
    public function __construct(Application $app, Router $router)
    {
        $app->register(new RoutingServiceProvider($app));

        parent::__construct($app, $router);
    }

The router instance inside the container is being modified to the class I created, but unfortunately when I try to get the 'router' instance from the container, it is returning my class without the application routes. In fact all the properties of the class are empty.

When I do: dd(resolve('router'))

I get the following:

App\Http\Responses\Router^ {#239 // app/Http/Kernel.php:98
  #events: Illuminate\Events\Dispatcher^ {#26 …5}
  #container: Illuminate\Foundation\Application^ {#3 …40}
  #routes: Illuminate\Routing\RouteCollection^ {#240
    #routes: []
    #allRoutes: []
    #nameList: []
    #actionList: []
  }
  #current: null
  #currentRequest: null
  #middleware: []
  #middlewareGroups: []
  +middlewarePriority: []
  #binders: []
  #patterns: []
  #groupStack: []
}

I would appreciate in advance any help or experience from someone who has tried to do something similar. Thank you so much.

0 likes
1 reply
LaryAI's avatar
Level 58

The issue seems to be that the Router class is not properly bootstrapped with the application's routes. One solution is to override the getRoutes method in your Router class to return the application's routes instead of an empty RouteCollection. Here's an example implementation:

<?php

namespace App\Http\Responses;

use Illuminate\Routing\Router as BaseRouter;

class Router extends BaseRouter
{
    /**
     * Get the route collection for the application.
     *
     * @return \Illuminate\Routing\RouteCollection
     */
    protected function getRoutes()
    {
        if (! $this->routes) {
            $this->routes = $this->app['router']->getRoutes();
        }

        return $this->routes;
    }

    /**
     * Static version of prepareResponse.
     *
     * @param $request
     * @param $response
     * @return \Symfony\Component\HttpFoundation\Response
     */
    public static function toResponse($request, $response)
    {
        if ($response instanceof HttpResponse) {
            return 'Hello instance of HttpResponse';
        }

        return parent::toResponse($request, $response);
    }
}

This implementation overrides the getRoutes method to retrieve the application's routes from the container's router instance. This should ensure that your Router class has access to the application's routes.

Note that you may need to clear your application's cache after making this change to ensure that the new Router class is properly loaded. You can do this by running php artisan cache:clear.

Please or to participate in this conversation.