Laravel 4 blade templates causing FatalErrorException?

喜你入骨 提交于 2019-12-21 17:27:03

问题


I'm getting an unexplained FatalErrorException when trying to implement a simple page layout using blade templating. I'm not sure if it's something I'm doing wrong or Laravel is. I'm following the tutorial on L4's documentation about Templating and my code seems to follow it. Here's my code.

app/routes.php:

<?php
Route::get('/', 'HomeController@showWelcome');

app/views/home/welcome.blade.php:

@extends('layouts.default')
@section('content')
  <h1>Hello World!</h1>
@stop

app/views/layouts/default.blade.php:

<!doctype html>
<html>
  <head>
    <title>The Big Bad Barn (2013)</title>
  </head>
  <body>
    <div>
      @yield('content')
    </div>
  </body>
</html>

app/controllers/HomeController.php:

<?php
class HomeController extends BaseController {
  protected $layout = 'layouts.default';
  public function showWelcome()
  {
    $this->layout->content = View::make('home.welcome');
  }
}

Laravel just throws a FatalErrorException. The output error page says "syntax error, unexpected '?'". The file blade is generating inside the storage/views directory has PHP where the

<?php echo $__env->make('layouts.default')

<?php $__env->startSection('content', array_except(get_defined_vars(), array('__data', '__path')))->render(); ?>; ?>
<h1>Hello World!</h1>
<?php $__env->stopSection(); ?>

回答1:


Yesterday I encountered the same problem you did, however the other answers didn't fix my problem. You've probably figured it out already, but maybe this post prevents others from spending as much time as I did figuring out what is wrong while it's such a small (but frustrating!) thing.

I'm using Notepad++ as text-editor and for some strange reason it had decided to use "MAC format" as the End-Of-Line (EOL) format. Apparently the Blade framework can't cope with that. Use the conversion function (in notepad++ : Edit -> EOL Conversion) to convert to Windows Format and it will work just fine..




回答2:


Your controller should be just returning View::make("home.welcome") instead of attaching it to the layout.

The welcome view then calls the layout so the controller is only concerned about the body template in this case.

Edit for showing example controller:

class HomeController extends BaseController {
  public function showWelcome()
  {
    return View::make('home.welcome');
  }
}



回答3:


Eric already gave the right answer, I just wanted to add that if you want to use

protected $layout = 'layouts.default';

in your HomeController, then leave showWelcome action intact and remove this line

@extends('layouts.default')

from welcome.blade.php file. That should work too.

Regards, Vlad



来源:https://stackoverflow.com/questions/16545086/laravel-4-blade-templates-causing-fatalerrorexception

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