[英]How to access the Altorouter instance in my views
I'm new to php, I would like to have access to the $router instance in my views so that I can use the route names with $router->generate() in the view but I don't know how to do it?我是 php 的新手,我想在我的视图中访问 $router 实例,以便我可以在视图中使用带有 $router->generate() 的路由名称,但我不知道该怎么做? I use Altorouter like this:
我像这样使用 Altorouter:
Index.php索引.php
$router = new AltoRouter();
$router->map( 'GET', '/post/[:id]/', 'PostController#find' );
$match = $router->match();
if ($match === false) {
throw new \Exception($error->404());
} else {
list($controller, $action) = explode('#', $match['target']);
if (is_callable(array($controller, $action))) {
$obj = new $controller();
call_user_func_array(array($obj, $action), array($match['params']));
} else {
throw new \Exception($error->500());
}
}
PostController.php PostController.php
public function find($args)
{
$post = $this->PostModal->find($args[id]);
// View
View::render('post/show', compact('post'));
}
My class View.php which manages the views我的 class View.php管理视图
public static function render(string $path, array $variables = [])
{
extract($variables);
ob_start();
require 'views/' . $path . '.html.php';
$pageContent = ob_get_clean();
require 'views/layouts/layout.html.php';
}
If you have the same problem as me, I resolved by adding如果你和我有同样的问题,我通过添加解决了
global $router;
in the render在渲染中
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.