April 18, 2015

Laravel 5 Routes, Controllers and Blade Views

The Basics


PHP Artisan commands are helper functions to make development with Laravel super fast. It also has a help feature, for example, to display information and useful flags for the make:controller method:
php artisan help make:controller

Routes are located in app/Http/routes.php.

Anatomy of a route is:
Route::get('URI', 'controllerName@controllerMethod');

A controller just provides a way to pass a method, so this will work too:
Route::get('foo', function() 
{
   return "bar";
});

We can also pass a wildcard from a route to a method, and back again:
Route::get('URI/{value}', 'controllerName@valueManipulatorMethod');

Controllers are located in the app/Http/Controllers folder, named as ControllerName.php. One controller per file. Each controller can have many methods.

Create a new controller via Artisan:
php artisan make:controller ControllerName

Views are located in the resources/views folder, named as viewname.blade.php. They can also be organized in subdirectories.

Return a view from a controller method:
return view('viewname');


// These two subdirectory calls are identical:
return view('subdirectory/viewname');
return view('subdirectory.viewname');

Using data from Controllers in Views


A view itself is just plain HTML. No data is generated/manipulated in a view. We can pass variables to a view template though. In the controller:
public function method()
{
   $myVariable = "hello world";
   return view('subdirectory.viewname')->with('greeting', $myvariable);
}

And in the HTML view, using escaped data:
<h1>This is my app that says {{ greeting }} </h1>

And in the HTML view, using unescaped data (can inject unsafe data, such as javascript):
<h1>This is my app that says {{!! greeting !!}} </h1>

Can also pass multiple variables into a view, declared within an array instead. In the controller:
public function method()
{
   return view('subdirectory.viewname')->with([
      'foo' => 'hello',
      'bar' => 'world'
   ]);
}

Alternately, still using arrays:
public function method()
{
   $data = []
   $data['foo'] = 'hello';
   $data['bar'] = 'world' 

   return view('subdirectory.viewname', $data);
}

Alternately, using plain variables:
public function method()
{
   $foo = 'hello';
   $bar = 'world'; 

   return view('subdirectory.viewname', compact('first', 'last');
}

All 3 controller methods above will work in this view:
<h1>This is my app that says {{ foo }} {{ bar }} </h1>

Blade Templating


Put all boilerplate HTML in a master/layout view; let's call it master.blade.php. Insert child views like:
<p>This is the master view</p>
@yield('section1')
@yield('section2')

In a child view:
@extends('master')
@section('section1')
<p>This is a child view paragraph</p>
@stop

Loading the above child page in a browser will display:
This is the master view
This is a child view paragraph

Conditionals can be used within views, using data passed from a controller (note how data is still not created - the conditionals merely reflect what HTML to show):
@if (count($people))
<h3>People I like:</h3>
<ul>
   @foreach ($people as $person)
      <li>{{ $person }}</li>
   @endforeach
</ul>
@endif

It is also possible to construct href links with data passed from a controller:

<a href="/transactions/{{ $transaction->id }}">{{ $transaction->description }} </a>

Or pass data from the view back to the controller as an array:

<a href="/transactions/{{ action('TransactionController@show', [$transaction->id]) }}">{{ $transaction->description }} </a> 

This also works:

<a href="{{ url('/transactions, $transaction->id }}">{{ $transaction->description }} </a> 

No comments :

Post a Comment