CodeIgniter Forums
Override Router - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: CodeIgniter 4 (https://forum.codeigniter.com/forumdisplay.php?fid=28)
+--- Forum: CodeIgniter 4 Support (https://forum.codeigniter.com/forumdisplay.php?fid=30)
+--- Thread: Override Router (/showthread.php?tid=78923)



Override Router - fogartylee - 03-28-2021

Hi folks!

I'm trying to override the router.

From what I can make out, the best way to do this is to edit the service first.

So, in my app/Services.php, I have the following:-

PHP Code:
<?php

namespace Config;

use 
CodeIgniter\Config\BaseService;

class 
Services extends BaseService
{
 
    public static function router(RouteCollectionInterface $routes nullRequest $request nullbool $getShared true)
    {
        if ($getShared) {
            return static::getSharedInstance('router'$routes$request);
        }

        $routes $routes ?? static::routes();
        $request $request ?? static::request();

        return new Router($routes$request);
    }


The function is copied directly from the system services file. So, in theory, as it's unchanged, it should work as usual?

However, I now get the following error:- 

Argument 1 passed to Config\Services::router() must be an instance of Config\RouteCollectionInterface or null, instance of CodeIgniter\Router\RouteCollection given, called in /var/www/vhosts/creative-spectrum.co.uk/public_html/micronet/system/CodeIgniter.php on line 807