Чтобы была возможность пользоваться роутом.В каких ситуациях может быть понадобится имя роута?
Например в kohana имя задается, в slim - нет. Причем в мануале не пишут, зачем оно понадобится...
$app->generateUrl('catalog', ('id' => 25));
class Foo
{
private $arr = [];
public function A($bar, $quz)
{
$this->arr[] = [
'key1' => $bar,
'key2' => $quz
];
return $this;
}
public function B($baz = [])
{
//@todo ...
return $this;
}
public function getArr()
{
return $this->arr;
}
}
// как добавить в массив $this->arr это 'key3' => $baz
// через вызов
$obj = new Foo();
$obj->A('value1', 'value2')->B(['key3' => 'value3']);
// ??
print_r($obj->getArr())
Array
(
[0] => Array
(
[key1] => value1
[key2] => value2
[key3] => value3
)
)
class Router
{
private $chain = array();
public function run($route)
{
uasort($this->chain, function ($a, $b) {
$left = $a['priority'];
$right = $b['priority'];
if ($left == $right) {
return 0;
} else {
return $left < $right ? -1 : 1;
}
}
);
foreach($this->chain as $pattern => $r){
if(preg_match($pattern, $route)){
call_user_func($r['controller']);
}
}
}
public function addRoute($pattern, Callable $callback, $priority = 0)
{
$this->chain[$pattern] = array(
'controller' => $callback,
'priority' => $priority
);
return $this;
}
}
class Foo
{
private $arr = [];
private $name;
public function A($name, $bar, $quz)
{
$this->name = $name;
$this->arr[$this->name] = [
'key1' => $bar,
'key2' => $quz
];
return $this;
}
public function B($baz = [])
{
$this->arr[$this->name]['key3'] = $baz;
return $this;
}
public function __destruct()
{
print_r($this->arr)
}
}
$route->add('/', function() {
echo 'HOME';
});
$route->add('/name', function() {
echo 'Name Home';
});
$route->add('/name/.+', function($name) {
echo "Name $name";
});
$route->add('/this/is/the/.+/story/of/.+', function($first, $second) {
echo "This is the $first story of $second";
});
/ работает пишет home
Не выходит каменный цветок...name/ выдает пустую страницу Not Found денвера
RewriteBase почему то не работает...RewriteEngine On
#RewriteBase /tests/PHP-Basic-MVC-Framework/
RewriteCond %{REQUEST_FILENAME} !-f
#only for localhost
#RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .* index.php [L]
AddDefaultCharset utf-8
public function __construct(){
$parts = explode('/',$_SERVER['REQUEST_URI']);
$parts = array_filter($parts);
$this->_controller = ($c = array_shift($parts))? $c: 'index';
$this->_method = ($c = array_shift($parts))? $c: 'index';
$this->_args = (isset($parts[0])) ? $parts : array();
}
$parts[0]
$parts[1]
$parts[2]
Request Object ( [_controller:Request:private] => index [_method:Request:private] => tests [_args:Request:private] => Array ( [0] => PHP-Basic-MVC-Framework [1] => ) )
А когда комментирую код
// $parts = array_filter($parts);
Вот такая проблема.hello world
hello world
hello universe