php laravel 刀片 @include 不起作用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21474638/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 04:10:37  来源:igfitidea点击:

laravel blade @include not working

phplaravellaravel-4bladephp-include

提问by Stacy J

I am trying to include files in my homepage using blade syntax :-

我正在尝试使用刀片语法在我的主页中包含文件:-

@foreach($data as $articles)
    @include(app_path().$articles->path)
    <br />
@endforeach

This is not working.

这是行不通的。

The error says :-

错误说:-

View [/var/www/blogproject/project/app/pages/articles/first-post.php] not found

I even tried including just the first page :-

我什至尝试只包含第一页:-

@include(app_path().'/pages/articles/first-post.php')

But the normal php include is working fine :-

但是正常的 php 包含工作正常:-

<?php include(app_path().'/pages/articles/first-post.php'); ?>

Please help

请帮忙

回答by rmobis

That's because that file is not in the app/viewsdirectory. When you call @include('filename'), Blade automatically looks for any file with that name, inside the apps/viewsdirectory. Also, you're not supposed to write the file extension, as Blade automatically looks for files with .blade.phpand .phpextensions.

那是因为该文件不在app/views目录中。当您调用 时@include('filename'),Blade 会自动在apps/views目录中查找具有该名称的任何文件。此外,您不应该编写文件扩展名,因为 Blade 会自动查找带有.blade.php.php扩展名的文件。

If you want to use files from other directories on the @includetag, add the directory to the paths array, on app/config/view.php. In your case, it'd be something like this:

如果要使用@include标签上其他目录中的文件,请将目录添加到app/config/view.php上的路径数组中。在你的情况下,它会是这样的:

app/config/view.php

应用程序/配置/view.php

<?php

    // ...

    'paths' => array(
        __DIR__.'/../views',
        __DIR__.'/../pages'
    );

Then, you'd call it on blade like so:

然后,您可以像这样在刀片上调用它:

@include('articles/first-post')