简体   繁体   English

Slim v3和树枝(“查看页面显示页面未找到错误”)

[英]Slim v3 and twig ( View Page displays page not found error)

I have installed slim framework 3 and twig template following the composer. 我已经在作曲家之后安装了苗条的框架3和树枝模板。 When i call function http://localhost/elec/helloo/sandesh it displays Hello, Sandesh as followed on slim 3 documentation. 当我调用函数http:// localhost / elec / helloo / sandesh时,它会在苗条3文档中显示Hello,Sandesh,如下所示。

But when i try to call view page( inside templates folder ). 但是当我尝试调用视图页面( 模板文件夹内 )。

It displays an error page Slim Application Error The application could not run because of the following error Error Description 它显示错误页面。Slim Application Error由于以下错误,应用程序无法运行错误描述

Code Worked ( displays hello , {name} from function) 代码工作正常(从函数中显示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;
}); 

Code error ( displays error when called view page from function) 代码错误(从函数调用视图页面时显示错误)

$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');

Path Detail 路径细节

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

When passing the templates location, you have to provide a full path, (starting from the location of the running index.php file: 传递模板位置时,您必须提供完整路径,(从运行的index.php文件的位置开始:

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

try it out, and good luck. 试试吧,祝你好运。

Note: I'm using the same line but with Twig render: 注意:我使用的是同一行,但是使用了Twig render:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM