I had write a Router with anonymous function callback.
Sample
$this->getRouter()->addRoute('/login', function() {
Controller::get('login.php', $this);
});
$this->getRouter()->addRoute('^/activate/([a-zA-Z0-9\-]+)$', function($token) {
Controller::get('activate.php', $this);
});
for smaller code, i want to move it to an Array.
I had write a Routing class with following methdos:
<?php
namespace CTN;
class Routing {
private $path = '/';
private $controller = NULL;
public function __construct($path, $controller = NULL) {
$this->path = $path;
$this->controller = $controller;
}
public function getPath() {
return $this->path;
}
public function hasController() {
return !($this->controller === NULL);
}
public function getController() {
return $this->controller;
}
}
?>
And my array has the routing paths with the new class:
foreach([
new Routing('/login', 'login.php'),
new Routing('^/activate/([a-zA-Z0-9\-]+)$', 'activate.php');
] AS $routing) {
// Here, $routing is available
$this->getRouter()->addRoute($routing->getPath(), function() {
// SCOPE PROBLEM: $routing is no more available
if($routing->hasController()) { // Line 60
Controller::get($routing->getController(), $this);
}
});
}
My current problem is (see the comments), the $routing
variable is on the anonymous function not available.
Fatal error: Call to a member function hasController() on null in /core/classes/core.class.php on line 60
how i can solve this problem?