【问题标题】:How to access the Altorouter instance in my views如何在我的视图中访问 Altorouter 实例
【发布时间】:2020-05-24 17:21:48
【问题描述】:

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

Index.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';
    }

【问题讨论】:

    标签: php altorouter


    【解决方案1】:

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

    global $router;
    

    在渲染中

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-07-22
      • 2014-01-10
      • 2011-01-13
      • 1970-01-01
      • 1970-01-01
      • 2013-01-08
      • 2021-06-13
      • 1970-01-01
      相关资源
      最近更新 更多