I'm learning about mvc through a series of video lessons on youtube, so that's fine, but the guy came to a point that ended up using array_walk ().
I could not understand how it works.
The full code is this + down and the video is on the link soon after (4 min video only). More ahead, it is getting the url that the user is trying to access and then checking on the routes already defined.
I crashed because I could not understand this statement:
array_walk($this->routes, function($route) use($url) {
...
}
I searched the PHP manual and as far as I understand the anonymous function it gets a parameter and a key: link (example # 1).
In the tutorial I'm watching the function is only receiving the parameter or would be the key (I did not quite understand). The use is used to access variables outside the scope, but it is not coming from outside the scope, being passed by parameter in public function run $ url) , would that already be out of scope?
array_walk(array, function(parametro/chave?) use(fora escopo?) {
...
}
Your someone knows how to use the array_walk and can explain their usage to me, it would help me a lot.
Full Code:
namespace app;
class Init
{
private $routes;
//Construtor
public function __construct()
{
$this->initRoutes();
$this->run($this->getUrl());
}
//Criando Rotas
public function initRoutes()
{
$ar['home'] = array('route'=>'/', 'controller'=>'index', 'action'=>'index');
$ar['empresa'] = array('route'=>'/empresa', 'controller'=>'index', 'action'=>'empresa');
$this->setRoutes($ar);
}
//Rotas
public function run($url)
{
array_walk($this->routes, function($route) use($url) {
if($url == $route['route']) {
echo "encontrou!!!";
}
});
}
//Setando rotas na variavel $routes
public function setRoutes(array $routes)
{
$this->routes = $routes;
}
//Pegando url q usuario esta tentando acessar
public function getUrl()
{
return parse_url($_SERVER['REQUEST_URI'],PHP_URL_PATH);
}
}
Video link: link