将流明 5.2 与刀片模板引擎一起使用?

Posted

技术标签:

【中文标题】将流明 5.2 与刀片模板引擎一起使用?【英文标题】:using lumen 5.2 with blade template engine? 【发布时间】:2016-10-02 05:03:33 【问题描述】:

我按照official documentation of laravel 使用刀片创建模板。我正在尝试制作我的第一个模板但不起作用。

1)存储在resources/views/default.blade.php --->

<html>
<head>
    @include('includes.head')
</head>
<body>
<div class="container">

    @include('includes.header')


    <div id="main" class="row">

            @yield('content')

    </div>

</div>
</body>
</html>

2)存储在resources/views/home.blade.php -->

@extends('layouts.default')
@section('content')
    I am the Home Page!
@endsection

3)存储在boostrap/app.php -->

$app->get('/', function ()
    return view('home');
);

4) localhost:8000/ 返回这个错误 -->

哎呀,好像出了点问题。 FileViewFinder.php 第 137 行中的 2/2 ErrorException:未找到视图 [layouts.default]。 (查看:/home/vagrant/lumen/resources/views/home.blade.php)

in FileViewFinder.php line 137
at CompilerEngine->handleViewException(object(InvalidArgumentException), '1') in PhpEngine.php line 44
at PhpEngine->evaluatePath('/home/vagrant/lumen/storage/framework/views/96985f6d91158d600b1d1b64b5a3060d84415fda.php', array('__env' => object(Factory), 'app' => object(Application))) in CompilerEngine.php line 59
at CompilerEngine->get('/home/vagrant/lumen/resources/views/home.blade.php', array('__env' => object(Factory), 'app' => object(Application))) in View.php line 149
at View->getContents() in View.php line 120
at View->renderContents() in View.php line 85
at View->render() in Response.php line 53
at Response->setContent(object(View)) in Response.php line 199
at Response->__construct(object(View)) in RoutesRequests.php line 643
at Application->prepareResponse(object(View)) in RoutesRequests.php line 505
at Application->callActionOnArrayBasedRoute(array(true, array(object(Closure)), array())) in RoutesRequests.php line 479
at Application->handleFoundRoute(array(true, array(object(Closure)), array())) in RoutesRequests.php line 376
at Application->Laravel\Lumen\Concerns\closure(object(Request))
at call_user_func(object(Closure), object(Request)) in Pipeline.php line 52
at Pipeline->Laravel\Lumen\Routing\closure(object(Request)) in CorsMiddleware.php line 6
at CorsMiddleware->handle(object(Request), object(Closure))
at call_user_func_array(array(object(CorsMiddleware), 'handle'), array(object(Request), object(Closure))) in Pipeline.php line 136
at Pipeline->Illuminate\Pipeline\closure(object(Request))
at call_user_func(object(Closure), object(Request)) in Pipeline.php line 32
at Pipeline->Laravel\Lumen\Routing\closure(object(Request))
at call_user_func(object(Closure), object(Request)) in Pipeline.php line 102
at Pipeline->then(object(Closure)) in RoutesRequests.php line 626
at Application->sendThroughPipeline(array('App\Http\Middleware\CorsMiddleware'), object(Closure)) in RoutesRequests.php line 382
at Application->dispatch(null) in RoutesRequests.php line 327
at Application->run() in index.php line 28

FileViewFinder.php 第 137 行中的 1/2 InvalidArgumentException:未找到视图 [layouts.default]。

in FileViewFinder.php line 137
at FileViewFinder->findInPaths('layouts.default', array('/home/vagrant/lumen/resources/views')) in FileViewFinder.php line 79
at FileViewFinder->find('layouts.default') in Factory.php line 165
at Factory->make('layouts.default', array('obLevel' => '1', '__env' => object(Factory), 'app' => object(Application))) in 96985f6d91158d600b1d1b64b5a3060d84415fda.php line 4
at include('/home/vagrant/lumen/storage/framework/views/96985f6d91158d600b1d1b64b5a3060d84415fda.php') in PhpEngine.php line 42
at PhpEngine->evaluatePath('/home/vagrant/lumen/storage/framework/views/96985f6d91158d600b1d1b64b5a3060d84415fda.php', array('__env' => object(Factory), 'app' => object(Application))) in CompilerEngine.php line 59
at CompilerEngine->get('/home/vagrant/lumen/resources/views/home.blade.php', array('__env' => object(Factory), 'app' => object(Application))) in View.php line 149
at View->getContents() in View.php line 120
at View->renderContents() in View.php line 85
at View->render() in Response.php line 53
at Response->setContent(object(View)) in Response.php line 199
at Response->__construct(object(View)) in RoutesRequests.php line 643
at Application->prepareResponse(object(View)) in RoutesRequests.php line 505
at Application->callActionOnArrayBasedRoute(array(true, array(object(Closure)), array())) in RoutesRequests.php line 479
at Application->handleFoundRoute(array(true, array(object(Closure)), array())) in RoutesRequests.php line 376
at Application->Laravel\Lumen\Concerns\closure(object(Request))
at call_user_func(object(Closure), object(Request)) in Pipeline.php line 52
at Pipeline->Laravel\Lumen\Routing\closure(object(Request)) in CorsMiddleware.php line 6
at CorsMiddleware->handle(object(Request), object(Closure))
at call_user_func_array(array(object(CorsMiddleware), 'handle'), array(object(Request), object(Closure))) in Pipeline.php line 136
at Pipeline->Illuminate\Pipeline\closure(object(Request))
at call_user_func(object(Closure), object(Request)) in Pipeline.php line 32
at Pipeline->Laravel\Lumen\Routing\closure(object(Request))
at call_user_func(object(Closure), object(Request)) in Pipeline.php line 102
at Pipeline->then(object(Closure)) in RoutesRequests.php line 626
at Application->sendThroughPipeline(array('App\Http\Middleware\CorsMiddleware'), object(Closure)) in RoutesRequests.php line 382
at Application->dispatch(null) in RoutesRequests.php line 327
at Application->run() in index.php line 28

【问题讨论】:

为什么要使用带有流明的视图?它专为微 api 设计!改用 laravel 【参考方案1】:

选择的答案 (https://***.com/a/37625344/7506001) 不正确。

@joeldg 和 @autista_z 都是正确的。

从 Lumen 5.4 开始,Blade 视图仍然可用且可用,即使它们不再记录在案。

完整示例:

// In routes/web.php: 
$app->get('/', function () use ($app) 
  return $app->make('view')->make('home');
);

// In resources/views/default.blade.php:
<html>
  <head>...</head>
  <body>
    <div id="whatever">
      @yield('content')
    </div>
  </body>
</html>

// In resources/views/home.blade.php:
@extends('default')
@section('content')
  <p>I am the Home Page!</p>
@endsection

【讨论】:

似乎从 Lumen 5.2 和 Lumen 开始就意味着是无状态的,这意味着他们已经删除了会话、CSRF 中间件、cookie 中间件,并使视图不那么突出。对于使用 Lumen 5.2 及更高版本的人,如果他们需要获取与 Lumen 相关的视图文档,则应参考 5.1 版本或尝试从 Laravel 5.2 及更高版本文档中推断。 我认为无状态 API 将模板呈现为 PDF 并让用户下载它是非常好的。这意味着在 Lumen 中仍然有 Blade 的用途。模板不仅适用于网页,您也可以将模板用于其他用途【参考方案2】:

您提到的 URL 是针对 Laravel 而不是 Lumen,Lumen 是一个轻量级的 Laravel,它用于 API 层和后端作业处理。我推荐 Laravel。

【讨论】:

对于小而轻的 cms,你不需要安装 Laravel,有了 Lumen,你可以正常使用刀片。我目前正在研究 Lumen 5.4,刀片运行起来就像一个魅力。 joeldg 和 autista_z 解释得更好。 如here所说,在Lumen 5.2之后,它应该只用于API。 @Khesayed 没有错,请改用 Laravel。【参考方案3】:

函数@extends('name') 在“resources/views”目录中查找文件“name.blade.php”或“name.php” 如果你使用@extends('layouts.default'),这意味着它在目录“resources/views/layouts”(视图中的子文件夹布局)中找到文件文件“default.blade.php”或“default.php”

但是你在“resources/views”中有你的“deafault.blade.php” 所以它应该由 @extends('default')

【讨论】:

非常感谢! :) 感谢您的解释,我明白按照 laravel 的指导方针出了什么问题。但在流明中不起作用。【参考方案4】:

选择的答案是错误的,Lumen默认安装了刀片。 您使用的 return 不起作用。

您的路线需要看起来像

$app->get('/', function () use ($app) 
  return $app->make('view')->make('index');
);

【讨论】:

以上是关于将流明 5.2 与刀片模板引擎一起使用?的主要内容,如果未能解决你的问题,请参考以下文章

Laravel 4 上的刀片模板引擎语法问题

是否可以将 AngularJS 与 Jinja2 模板引擎一起使用?

Smarty模板引擎

Laravel 5.2:如何在刀片模板中设置变量,没有 php 标签?

Smarty 模板引擎简介

PHP模板引擎,Smarty定义