Laravel - Blade comments , blade rendering causing page to crash

回眸只為那壹抹淺笑 提交于 2019-11-27 12:17:18

问题


I'm rendering a page that is primarily a form with view::make in Laravel and it is crashing, causing ERR_CONNECTION_RESET. After a long investigation and many red herrings, I started erasing (not commenting) random sections out of the blade file for the view and realized that if I

a) erase 2 of the {{Form}} calls inside this section of the form

b) remove the {{-- and --}} from around this section of the form

    {{--
    <div class="form-row">
      {{ Form::label('foo', 'foo:') }}
      {{ Form::text('foo') }}
    </div>
    <div class="form-row">
      {{ Form::label('foo', 'foo:') }}
      {{ Form::text('foo') }}
    </div>
    <div class="form-row">
      {{ Form::label('foo', 'foo') }}
      {{ Form::text('foo') }}
    </div>
    --}}

the page will render. I am not sure what exactly the cause here is. There are other blocks above and below, although this is a 3-div commented out section which none of the others are.

Anyone have a clue what is causing this? Running on WAMP if that matters.


回答1:


Note: this answer was given for Laravel 4.2, but should still apply. There are some special cases of Blade compilation issues that are dependent on the version of Laravel and/or PHP, so it's best to only use Blade comments for the simplest use-cases.

The solution is to only use Blade comments for simple remarks, or to comment out single-line Blade functions. Do not nest Blade/PHP code inside of Blade comments. Use standard PHP block comments to comment out multiple lines of code within a single comment (PHP, HTML, multiple blade functions, etc.).


Valid Blade Comments:

Single Blade Function:

{{-- Form::text('foo') --}}

Remark:

{{-- Form Section 1 --}}

Invalid Blade Comments:

Incorrect syntax:

{{-- Form::text('foo') --  }} 

"@" Inside of Blade comment

{{-- @Form::text('foo') --}} 

Nested PHP:

{{-- <?php 
echo "foo";
echo "bar
?> --}} 

Nested Blade:

{{-- 
{{ HTML::form("foo") }};
{{ HTML::form("bar") }};
--}} 

Use PHP Block Comments instead. They are still usable in a blade.php file

<?php /* 
{{ HTML::form("foo") }};
{{ HTML::form("bar") }};
*/ ?> 

Alternatively, comment out your Blade one line at a time:

{{-- HTML::form("foo") --}};
{{-- HTML::form("bar") --}};

Internals:

For the OP's code, Laravel's Blade Compiler will generate a temporary PHP file containing the following PHP/HTML:

<?php /* 
    <div class="form-row">
      <?php echo Form::label('foo', 'foo:'); ?>

<?php echo Form::text('foo'); ?>

</div>
<div class="form-row">
    <?php echo Form::label('foo', 'foo:'); ?>

    <?php echo Form::text('foo'); ?>

</div>
<div class="form-row">
    <?php echo Form::label('foo', 'foo'); ?>

    <?php echo Form::text('foo'); ?>

</div>
*/ ?>

The Blade inside of your Blade comments are still being parsed into PHP. The PHP end tags inside of the PHP block-comment is causing the Apache's parser to end early, resulting in some badly-formed PHP/HTML that could be crashing your connection (likely caused by the dangling */ ?>).

?> breaks out of PHP mode and returns to HTML mode, and // or # cannot influence that.

Using any of the aforementioned invalid Blade comments will cause similar compilation issues. Avoid Blade comments for anything other than remarks or commenting Blade functions out one line at a time.




回答2:


I have same problem with laravel 5.1 and PHP 7 (new homestead). The work around was to use this:

<?php /* XXX */?>

instead of this:

{{-- XXX -- }}.



回答3:


I have a similar symptom and it seems to be related to the length of the comment alone. I tested it with a comment that doesn't contain any PHP code or blade statements at all:

{{--
0123456789abcdef
0123456789abcdef
0123456789abcdef
--}}

I kept adding copies of the repeated line until it crashed. The comment was lexically followed by a blade @if statement, and the corresponding <php if(...): ?> did not end up in the compiled template, but the closing <?php endif; ?> did, resulting in a syntactically invalid compiled template.

It seems to be a bug in the blade compiler and I will report it.

The workaround is to split long blade comments with --}}{{--.




回答4:


I have Tried the

Nested PHP:

{{-- <?php 
echo "foo";
echo "bar";
?> --}} 

@TonyArra

While using . It is Not Commenting the Content and prevents from Compiling as HTML

and this is the htmlsource {{-- foobar --}}

Which i have got

Thats Because If You want To Comment the php Code inside Blade

Try this

<!-- @php echo 'hai'; @endphp -->

OR

<!-- <?php echo 'hai'; ?> -->

and try to view the page source




回答5:


Blade comments like this one, were the problem in my case:

{{--    
    @if ($test)
        <div>something</div>
    @else
        <div>something else</div>
    @endif
--}}


来源:https://stackoverflow.com/questions/27830200/laravel-blade-comments-blade-rendering-causing-page-to-crash

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!