0

我是 php 新手,我想在我的视图中访问 $router 实例,以便我可以在视图中使用带有 $router->generate() 的路由名称,但我不知道该怎么做? 我像这样使用 Altorouter:

索引.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

public function find($args)
    {
        $post = $this->PostModal->find($args[id]);
        // View
        View::render('post/show', compact('post'));
    }

我的类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';
    }
4

1 回答 1

0

如果你和我有同样的问题,我通过添加解决了

global $router;

在渲染中

于 2020-06-02T18:53:14.283 回答