Php 板材模板引擎-URI扩展与细枝“pathFor”相同?

Php 板材模板引擎-URI扩展与细枝“pathFor”相同?,php,twig,slim,templating,plates,Php,Twig,Slim,Templating,Plates,我正在学习Slim框架的教程。作者使用了Twig,我更喜欢使用Plates模板引擎。我已经能够修改所有课程来使用Plates模板,直到作者开始使用baseUrl和PathForExtensions 我看到Plates有一个名为URI的扩展,我认为它与Twig的pathFor同义 不幸的是,我一辈子都不知道如何启用它。在阅读文档时,我认为下面的代码可以做到这一点,但到目前为止运气不佳 require 'vendor/autoload.php'; $app = new Slim\App([

我正在学习Slim框架的教程。作者使用了Twig,我更喜欢使用Plates模板引擎。我已经能够修改所有课程来使用Plates模板,直到作者开始使用baseUrl和PathForExtensions

我看到Plates有一个名为URI的扩展,我认为它与Twig的pathFor同义

不幸的是,我一辈子都不知道如何启用它。在阅读文档时,我认为下面的代码可以做到这一点,但到目前为止运气不佳

require 'vendor/autoload.php';

$app = new Slim\App([
    'settings' => [
        'displayErrorDetails' => true
    ]
]);

$container = $app->getContainer();

$container['view'] = function ($container) {
    $plates = new League\Plates\Engine(__DIR__ . '/templates');
    $plates->loadExtension(new League\Plates\Extension\URI($_SERVER['PATH_INFO']));
    return $plates;
};

$app->get('/contact', function($request, $response) {
    return $this->view->render('contact');
});

$app->post('/contact', function($request, $response) {
    return $response->withRedirect('http://slim-local.com/contact/confirm');
})->setName('contact');

$app->get('/contact/confirm', function($request, $response) {
    return $this->view->render('contact_confirm');
});

$app->run();
然后在模板中,作者使用pathFor扩展来填充表单的动作参数。我正在尝试使用Plates的URI扩展来执行相同的操作,如下所示:

<form action="<?=$this->uri('contact')?>" method="post">
有人专门使用过这个模板引擎和URI扩展吗?我是不是错了,它基本上就是小树枝的延伸路径的同义词?我应该放弃,只使用树枝吗?谢谢您的建议。

您可以从环境中使用URI

Slim 3示例:

$container['view'] = function ($container) {
    $plates = new \League\Plates\Engine(__DIR__ . '/templates');

    $uri = \Slim\Http\Uri::createFromEnvironment(new \Slim\Http\Environment($_SERVER));
    $plates->loadExtension(new \League\Plates\Extension\URI($uri->__toString()));

    return $plates;
};

令人惊叹的谢谢你快速完美的回答。我还不知道我自己是怎么知道的,但谢天谢地,在StackOverflow上有很多像你这样聪明、乐于助人的人可以帮助我。顺便说一句,toString能完成什么?现在稍微看了一下,我本以为需要使用getPath的一个变体,尽管我仍然不完全理解为什么以及如何工作。是一个PHP神奇的方法。如果要将对象强制转换为字符串,将调用此方法。例如:字符串$uri与此相同:$uri->\uuuuuToString