Laravel 5 语法错误,意外的“扩展”(T_EXTENDS)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/30631635/
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-09-14 11:42:54  来源:igfitidea点击:

Laravel 5 syntax error, unexpected 'extends' (T_EXTENDS)

phplaravellaravel-5

提问by Santosh Achari

This is the code in my controller:

这是我的控制器中的代码:

class ArticlesController extends Controller {
    public function index()
    {
        $articles = Article::all();
        return view('articles.index',compact('articles'));
    }

}

My App.blade.php

我的 App.blade.php

<!DOCTYPE html>
<html lang="en">
    <head>
        <title>Title</title>
        <meta charset="UTF-8">
        <meta name=description content="">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <!-- Bootstrap CSS -->
        <link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.4/css/bootstrap.min.css" rel="stylesheet" media="screen">
    </head>
    <body>
        <div class="container">
            @yield('content')
        </div>

        @yield('footer')
        <!-- jQuery -->
        <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
        <!-- Bootstrap JavaScript -->
        <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.4/js/bootstrap.min.js"></script>
    </body>
</html>

And index.blade.php

和 index.blade.php

<?php

@extends('app')

@section('content')
    <h1>Articles</h1>
@stop

This is the error that I get:

这是我得到的错误:

FatalErrorException in 5f3efcdeb3d9812b22b5491d0cba9f22 line 3:
syntax error, unexpected 'extends' (T_EXTENDS)

Please help.

请帮忙。



Solution listed below. While the issue is silly, and easy to fix - it's hard to find a straight answer via Google Search. Hence keeping this question.

下面列出了解决方案。虽然这个问题很愚蠢,而且很容易解决 - 很难通过 Google 搜索找到直接的答案。因此保留这个问题。

回答by Santosh Achari

The <?php ?>Tags are not required in .blade.phpfiles. Remove them and it works.

<?php ?>代码并不需要.blade.php文件。删除它们,它的工作原理。

(The blade syntax need not be in php tags. You can however, include PHP code in tags.)

(刀片语法不需要在 php 标签中。但是,您可以在标签中包含 PHP 代码。)

回答by Akshay Khale

The <?php ?>tags are not required in your laravel blade file saved with extension .blade.phplaravel templating engine automatically parse blade commands from these files.

<?php ?>标签不保存扩展您的laravel刀片文件所需.blade.php自动laravel模板引擎从这些文件中解析刀片的命令。

But if you want to include php code in your blade file which is not part of blade engine commands then you will have to use <?php ?>tags.

但是如果你想在你的刀片文件中包含不属于刀片引擎命令的 php 代码,那么你将不得不使用<?php ?>标签。

回答by Aaron

I know this is a bit old but the issue was clearly missed.

我知道这有点旧,但显然错过了这个问题。

In your index.blade.php file your missing the closing php tag "?>".

在您的 index.blade.php 文件中,您缺少 php 结束标记“?>”。

The blade sytanx @extends, @section etc... should be in your html on the outside of any php code block.

Blade sytanx @extends、@section 等...应该位于任何 php 代码块外部的 html 中。

回答by user2094178

When you use @extends, it must be the first piece of code in your blade file.

使用时@extends,它必须是刀片文件中的第一段代码。

A blank space or an empty new line before @extendswill also throw an error.

之前的空格或空的新行@extends也会引发错误。