【问题标题】:Slim v3 and twig ( View Page displays page not found error)Slim v3 和 twig(查看页面显示页面未找到错误)
【发布时间】:2016-04-29 10:27:16
【问题描述】:

我已经按照 composer 安装了 slim framework 3 和 twig 模板。 当我调用函数http://localhost/elec/helloo/sandesh 时,它会显示 Hello, Sandesh ,如 slim 3 文档所示。

但是当我尝试调用查看页面时(模板文件夹内)。

显示错误页面 Slim Application Error The application could not run because of the following error Error Description

代码工作(显示 hello , {name} 来自函数)

$app = new \Slim\App;
$app->get('/hello/{name}', function (Request $request, Response $response) {
    $name = $request->getAttribute('name');
    $response->getBody()->write("Hello, $name");

    return $response;
}); 

代码错误(从函数调用视图页面时显示错误)

$settings =  [
    'settings' => [
        'displayErrorDetails' => true,
    ],
];

$app = new Slim\App($settings);

// Get container
$container = $app->getContainer();

// Register component on container
$container['view'] = function ($container) {
    return new \Slim\Views\PhpRenderer("templates/");
};

// Render Twig template in route
$app->get('/helloo/{name}', function ($request, $response, $args) {
    return $this->view->render($response, 'view1.html', [
        'name' => $args['name']
    ]);
})->setName('profile');

路径详情

elec>
    >>cache
    >>templates
               >>>view1.html
    >>vender
    >>.htaccess
    >>composer.json
    >>composer.lock
    >>index.php

【问题讨论】:

    标签: php twig slim slim-3


    【解决方案1】:

    在传递模板位置时,您必须提供完整路径,(从正在运行的 index.php 文件的位置开始:

    <?php
        $container['view'] = function ($container) {
            return new \Slim\Views\PhpRenderer(__DIR__ . "/../path/to/templates/");
        };
    

    试试看,祝你好运。

    注意:我使用的是同一行,但使用了 Twig 渲染:

    <?php
        $container['view'] = function ($container) {
            return new \Slim\Views\Twig(__DIR__ . "/../path/to/templates/");
        };
    

    【讨论】:

      【解决方案2】:
      $app = new \Slim\App([
          'settings' => [
              'displayErrorDetails' => true,
          ]
      ]);
      
      // Calling twigview from controller
      $container = $app->getContainer();
      
      // Register component on container
      $container['view'] = function ($container) {
          $view = new \Slim\Views\Twig('templates/views',[
              'cache' => false,
          ]);
      
          $view->addExtension(new \Slim\Views\TwigExtension(
              $container->router,
              $container->request->getUri()
          ));
      
          return $view;
      };
      
      $app->get('/home', function ($request, $response) {
          return $this->view->render($response, 'home.twig');
      });
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2012-11-22
        • 2017-01-15
        • 1970-01-01
        • 2015-06-22
        • 2019-05-24
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多