Laravel 4 blade templates causing FatalErrorException? - laravel

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(); ?>

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..

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');
}
}

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

Related

How to create template in cakephp 3.x

I'm a new student in CakePHP 3 please resolve my problem.
This is my controller file:
DirectUseController.php
<?php
class DirectUseController extends AppController {
function index() {
$this->layout = 'directuse';
}
}
?>
This is my layout file:
directuse.ctp
<!DOCTYPE html>
<html>
<head>
<title>
<?= $this->fetch('title') ?>
</title>
</head>
<body>
Bootstrap | Foundation | Materilize
<br><br>
Copyright
<br><br>
</body>
</html>
This is my index file in folder of direct use
index.ctp
<section id="mainBody">
hello
</section>
and my folder structure is:
What am I missing?
Your layout should presumably include this somewhere:
echo $this->fetch('content');
If that doesn't solve your problem, you're going to have to be more specific about what the problem is.
CakePHP prioritizes convention over configuration, so try to change your Controller name to DirectusesController, change also your layout folder's name (to DirectUses) and maybe your Model too (cake bake can easily help you), I don't know your [table name in the database] but it should be plural and in lowercase (directuses) (if you don't use database that's another story)
For your template Greg Schmidt is right
Inside your layout file you have to use this:
<?= $this->Flash->render(); ?>
<?= $this->fetch('content'); ?>
I prefer to add the <html> and <body> tags inside the default.ctp or directuse.ctp layout. On this way you do not have to rebuild your html every time. This will make your code a lot cleaner.

An error when passing a parameter in a route laravel 5.2

this is my code
route.php
Route::get('test/{id}','testController#test');
testController.php
public function test($id){ return('test'); }
test.blade.php
<!DOCTYPE html>
<html>
<head>
<title>a</title>
</head>
<body>
<img src="myPath/myImage.jpg">
</body>
</html>
The result: the image cant be load
note: when i deleted the 'id' parameter it has worked perfectly
What is the error message you are getting?
You will also probably want to change the return value of testController#test to something along the lines of this so you can actually pass the variable to the view.
return view('test', [
'id' => $id,
])
You may also want to have env('APP_DEBUG',true) turned on to get verbose output
the solution : i have changed the value of the src attribute
<img src="{{URL::to('myPath/myImage.jpg')}}">
You need to use:
href="{{ URL::to('yourfile')}}". Also you should place your images in laravel public folder. If you will create folder img in your public folder then you need to something like this: href="{{ URL::to('img\imgname.png')}}".

Site URL CodeIgniter Not Working

I have Controller on CI like this
class Testing extends CI_Controller {
//put your code here
public function xx() {
$this->load->helper('url');
$this->load->view('testing');
}
public function linkURL() {
$this->load->helper('url');
$data['test'] = "testing123";
$this->load->view('xxx_view', $data);
}
}
I'm running on function linkURL and call view xxx_view, the code on view like this
<html>
<head>
<meta charset="UTF-8">
<title></title>
</head>
<body>
<?php
$segment = array('Testing', 'xx');
?>
Link
</body>
view call a href and using helper site_url to call Controller Testing and function xx. But the link is not working. I am already capture on firebug and link looks like weird. The Link on href contain *http://::1*. How to solved that link
You can print_r($_SERVER) in your controller and check it. or You can use
$config['base_url'] = 'http://localhost:8081/your-project/'
In my opinion, seem to you sent data test to view xxx_view but not using this variable. try echo $test and I see in url using xxx not xx
You need to autoload URL helper in your config/autoload.php.This time you are loading URl helper in function. That will not work for your view.

Laravel 4 content yielded before layout

I am using a fresh build today of Laravel 4.
I have a dashboardController
class DashboardController extends BaseController {
protected $layout = 'layouts.dashboard';
public function index()
{
$this->layout->content = View::make('dashboard.default');
}
}
I have a simple route
Route::get('/', 'DashboardController#index');
I have a blade layout in views/layouts/dashboard.blade.php
For the sake of saving everyone from all of the actual HTML ill use a mock up.
<html>
<head>
<title></title>
</head>
<body>
#yield('content')
</body>
</html>
I have a default blade file in views/dashboard/ that has the following (edited for simplicity)
#section('content')
<p>This is not rocket science</p>
#stop
For some reason the content gets generated before the layout.
I am using a different approach to set the layouts globally to routes using a custom filter. Put the following filter into the app/filters.php
Route::filter('theme', function($route, $request, $response, $layout='layouts.default')
{
// Redirects have no content and errors should handle their own layout.
if ($response->getStatusCode() > 300) return;
//get original view object
$view = $response->getOriginalContent();
//we will render the view nested to the layout
$content = View::make($layout)->nest('_content',$view->getName(), $view->getData())->render();
$response->setContent($content);
});
and now instead of setting layout property in the controller class, you can group the routes and apply the filter as shown below.
Route::group(array('after' => 'theme:layouts.dashboard'), function()
{
Route::get('/admin', 'DashboardController#getIndex');
Route::get('/admin/dashboard', function(){ return View::make('dashboard.default'); });
});
When creating the views, make sure to use the #section('sectionName') in all the sub views and use #yield('sectionName') in the layout views.
I find it easier to do my layout like this for example. I would create my master blade file like so
<html>
<body>
#yield('content');
</body>
</html
And in the blade files that I want to use the master at the top i would put
#extends('master')
then content like so
#section('content')
// content
#stop
Hope this helps.
When you use controller layouts, i.e. $this->layout->..., then you get access to data as variables, not sections. So to access content in your layout you should use...
<html>
<head>
<title></title>
</head>
<body>
<?php echo $content; ?>
</body>
</html>
And in your partial, you would not use #section or #stop...
<p>This is not rocket science</p>

Codeigniter loading library

i have a controller like this one :
<?php if( ! defined('BASEPATH')) exit ('No direct script acces allowed');
class Halaman extends CI_controller{
function __controller(){
parent::controller;
$this->load->helper(array('url','form'));
$this->load->library('table');
}
function index(){
$this->load->library(array('form_validation'));
$this->load->view('view_halaman');
}
function daftar(){
$this->load->model(url);
}
}
and i have a views like this one
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Pembagian menggunakan validasi!!</title>
</head>
<body>
<h1>Daftar Ulang</h1>
<?php echo form_open('halaman/daftar'); ?>
<?php $data=array(
array('Field','isi data'),
array('nama',form_input('user','tulis username')),
array('password',form_password('pass','password')),
array('email',form_input('email','tulis email di sini'))
);
echo $this->table->generate($data);
?>
<?php echo form_close(); ?>
<p><br/>Page rendered in {elapsed_time} seconds</p>
</html>
enter code here
what i get is this one :
where is my mistake ? im sorry i am really newbie in codeigniter. thanks a lot .
$table is not a member of the view object. So you can't call it from the view template. You need to move the echo $this->table->generate($data); to the controller, and either assign it to a view variable or just echo it from the controller.
I think the problem is that the library wasn't loaded (that's the error in the red lined box). This happends when you don't follow the CI library name conventions. The Table library should be in application/libraries/Table.php and class must be someting like:
class Table {
// Your code
}
http://codeigniter.com/user_guide/general/creating_libraries.html
Also when you load the library, use the name with capital T:
$this->load->library('Table');
Regards,

Resources