Answer the question
In order to leave comments, you need to log in
What problems can arise: router?
Good time!
Actually the whole question is above :-)
class Router
{
...
...
...
private function __clone() {}
private function __construct() {}
public static function getInstance()
{
if (null === self::$instance) {
self::$instance = new self();
}
return self::$instance;
}
public function init()
{
$this->_getUrl();
if (empty($this->_url[0])) {
$this->_loadDefaultController();
return false;
}
$this->_loadExistingController();
$this->_callControllerMethod();
return true;
}
...
...
...
Answer the question
In order to leave comments, you need to log in
Singleton is clearly superfluous there. And if the route is not defined, display 404, not the default controller.
Added:
When requesting "/this-page-I-don't-have" you need to show 404. And in your case, all links to non-existent pages will display the default controller.
The singleton is needed to access the instance from anywhere in the application. If this were the main application container, then yes, that would make sense. But for a router that always runs in the same place in the application, a singleton is not needed. You just called the router once and you don't need it anymore: ( new Router() )->init();
Where is the description of the _getUrl() function?
Either I'm blind, or you disguised it under these three dots.
Simply, if your url is divided by slashes, then I know where the error can hide (wang).
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question