Laravel - php 评估 blade compileString

Laravel - php eval with blade compileString

我正在尝试将 blade 模板字符串解析为 php 格式并使用 eval() 将字符串评估为 php代码

 $array = [
     'foo' => 'bar',
     'bar' => 'foo'
 ];
 $content = '@foreach($array as $value){{$value}}@endforeach';
 $blade = Blade::compileString($content);
 $php = eval($blade);

这是我到目前为止的测试代码,它抛出异常

ParseError: syntax error, unexpected '<', expecting end of file

$bladecompileString()

之后的值
<?php $__currentLoopData = $array; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $value): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?><?php echo e($value); ?><?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>

导致此错误的原因是什么? eval() 与 compileString() 将 blade 解析为 php 的方式不兼容吗?

manual 中所述:

The code must not be wrapped in opening and closing PHP tags, i.e. 'echo "Hi!";' must be passed instead of '<?php echo "Hi!"; ?>'. It is still possible to leave and re-enter PHP mode though using the appropriate PHP tags, e.g. 'echo "In PHP mode!"; ?>In HTML mode!<?php echo "Back in PHP mode!";'.

你的blade被包裹在<?php标签中,编译失败。删除封闭的 PHP 标签,但保留插页式标签。