1
I’m learning about mvc through a series of video lessons on youtube, so so it’s okay, but the guy got to a point that ended up using array_walk().
I couldn’t understand its workings.
The complete code is this + below and the video is on the link soon after (4 min video only). Further ahead, it is getting the url that the user is trying to access and then checking on the routes already defined.
I stalled because I couldn’t understand this statement:
array_walk($this->routes, function($route) use($url) {
...
}
I searched in the PHP manual and I understood the anonymity function receives a parameter and a key: http://php.net/manual/en/function.array-walk.php (example #1).
In the tutorial I am watching the function is only receiving the parameter or would be the key (I don’t understand well). The use serves to access variables outside the scope, but it is not coming from outside the scope, being passed by parameter in public Function run($url), That would already be out of scope?
array_walk(array, function(parametro/chave?) use(fora escopo?) {
...
}
If someone knows how to use array_walk and can explain its use to me tbm, it would help me a lot.
Complete 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);
}
}
Link Vídeo: https://www.youtube.com/watch?v=o7r1fHI9U4A&index=11&list=PLtxCFY2ITssBl_nihh4HC5-ZlnIPEpVQD
Congratulations, now I understand. Thank you very much. There was only one question regarding this msg "The difference is basically that the array_walk function does not consider the array’s internal pointer, thus ensuring that it is fully traversed in all calls, while the foreach loop only traverses the array from the position of the current pointer to the end of the array (or a break)" I didn’t understand the difference, because basically it will go through 0...end on array_walk and foreach.
– AntonioSantos
In the first two examples, run
next($array)
before theforeach
and ofarray_walk
and see the difference. Thenext
will move the array’s internal pointer in one position, then theforeach
no longer goes through the whole array, but the functionarray_walk
yes.– Woss
Wow, top rs Thanks buddy :D
– AntonioSantos
I have the same problem, I am studying mvc+php with the same course of this question, and in my case although my code is exactly the same as the question, my code does not execute "echo" inside the if. Should I create a new question or can this question be answered here ?
– Gabriel Silva
@Gabrielsilva if the code is the same, echo is not executed because the condition is not met.
– Woss
The condition has to be satisfied, because I know the condition and I wrote a route that exists and yet the if did not find it.
– Gabriel Silva
@Gabrielsilva then ask a new question and elaborate a [mcve] demonstrating the behavior cited.
– Woss
Some of you might be interested in following the link to my question.https://answall.com/questions/420321/rota-n%C3%a3o-found-php
– Gabriel Silva