Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

About Replacing Core Classes! #973

Closed
35598253 opened this issue Mar 26, 2018 · 12 comments
Closed

About Replacing Core Classes! #973

35598253 opened this issue Mar 26, 2018 · 12 comments

Comments

@35598253
Copy link

35598253 commented Mar 26, 2018

I want to Replace \CodeIgniter\Router\Router.php Core Classes,How to operate?

Now my work is:
Myrouter.php:

namespace App\Libraries;
class Myrouter implements \CodeIgniter\Router\RouterInterface
{
Copy Router Classes And edit Variable(Actually, I just want to replace some function)

}

Services.php:
public static function routes($getShared = false)
{
if (! $getShared)
{
return new \Rcmf\Libraries\Myrouter();
}

        return self::getSharedInstance('routes');
}

running error

How can I correct?

@InsiteFX
Copy link
Contributor

InsiteFX commented Mar 26, 2018

You would want to extend the Router Class and not use implements that replaces the Class

See:
Creating Core System Classes

@35598253
Copy link
Author

35598253 commented Mar 26, 2018

Thanks

I know Creating Core System Classes , but It so simple!

namespace App\Libraries;
class MyRoute extends \CodeIgniter\Router\Router
{
public function __construct()
{
parent::__construct();
}

public function autoRoute(string $uri)
{}

}

running error

Declaration of App\Libraries\MyRoute::__construct() must be compatible with CodeIgniter\Router\RouterInterface::__construct(CodeIgniter\Router\RouteCollectionInterface $routes)

How can I correct?

@InsiteFX
Copy link
Contributor

Your library should be named MY_Router not MY_Route

Also if you still get the error then add this to your __construct()
__construct(CodeIgniter\Router\RouteCollectionInterface $routes)

@35598253
Copy link
Author

35598253 commented Mar 26, 2018

Thanks!
I edit it
My_Router.php
class My_Router extends \CodeIgniter\Router\Router
{

public function __construct(\CodeIgniter\Router\RouteCollectionInterface $routes)
{
    parent::__construct();
}

//--------------------------------------------------------------------


//--------------------------------------------------------------------

/**
 * Attempts to match a URI path against Controllers and directories
 * found in APPPATH/Controllers, to find a matching route.
 *
 * @param string $uri
 */
public function autoRoute(string $uri)
{
	$segments = explode('/', $uri);

}

running error
Too few arguments to function App\Libraries\My_Router::__construct(), 0 passed in D:\richcms\App\Config\Services.php on line 27 and exactly
How can I correct?

Services.php
public static function routes($getShared = false)
{
if (! $getShared)
{
return new \App\Libraries\My_Router();
}

        return self::getSharedInstance('routes');
}

@InsiteFX
Copy link
Contributor

This is this definition of the Router Class
public function __construct($routing = NULL)

Try leaving the __construct() blank and call the parent::__construct()

@35598253
Copy link
Author

Sorry, I don't understand what you mean;
class My_Router extends \CodeIgniter\Router\Router
{
public function __construct($routing = NULL) {
// parent::__construct();
}

}
running error
Call to undefined method Rcmf\Libraries\My_Router::cli()

Can give me more detailed code?
thanks!

@InsiteFX
Copy link
Contributor

Aww, sorry I had the wrong project in it was CI 3 I'll switch and take a look at it now brb.

@InsiteFX
Copy link
Contributor

To use one of your own system classes instead of a default one, ensure that the Autoloader can find your class, that your new class extends the appropriate interface, and modify the appropriate Service to load your class in place of the core class.

@35598253
Copy link
Author

35598253 commented Mar 26, 2018

sorry I still don't understand,
I'm sure Autoloader can find My_Router class,
Services.php:
public static function routes($getShared = false)
{
if (! $getShared)
{
return new \Rcmf\Libraries\Myrouter();
}

    return self::getSharedInstance('routes');

}

that your new class extends the appropriate interface
I modified it.
class My_Router extends \CodeIgniter\Router\Routerinterface
running error
Class App\Libraries\My_Router cannot extend from interface CodeIgniter\Router\RouterInterface

I just want to
Replace CodeIgniter\Router\Router function autoRoute()

How can I correct?
Can you give me code directly?

@InsiteFX
Copy link
Contributor

InsiteFX commented Mar 26, 2018

Then in your Class just name the method as the one in the Router Class it will then overload that one.

@35598253
Copy link
Author

sorry I still don't run,

  1. Autoloader can find My Class
  2. new class extends the Router(I want to replace the autoroute() function in Router.php)
  3. autoroute() methods in my class
  4. Service::routes() load new classes

running error
Did I do anything wrong with these steps?

@jim-parry
Copy link
Contributor

This is a conversation that should be happening on our forum. We use github for bug tracking and work packages.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants