Laravel is a free and open-source PHP web framework like CodeIgniter and Zend. It is built on the Model-View-Controller (MVC) architectural pattern. It has very good documentation and training material. The community is also significant enough. I find it to be much easier than Zend and about as easy to use as CodeIgniter. Better training material and documentation makes Laravel easier for beginner than CodeIgniter.

Installation

There are plenty of videos, tutorials, and documentation to help you with installation. I would recommend that you start with the official site http://www.laravel.com. Be sure that you are following instruction for the right version. The installation instructions for version 4.x and 5.x are very different.

Understanding Routing

Routing means defining which code needs to be called for which URL. The code for this is very simple. Go to app/Http/routes.php. See the following code

Route::get('/', function() {
  return view('welcome');
});

This code defines which code will be called when you go to the site’s index. Suppose you go to xyc.com, it will show you the output of a view called welcome. This is the default page you see when you run Laravel. More on view later.

Add the following code to app/Http/routes.php

Route::get('/text', function() {
  return 'sample text';
}

Save file and run Laravel. When you go to your site/text in your browser, you should “sample text”. Basically, we defined a route and assigned it some text to display.

Introducing Views

A view contains code which generates the displayed output. Views are located in resources/views directory. Open resources/views/welcome.blade.php. This view contains the css and HTML used to generate the index page. Find the following code near the bottom of the file.

<div class="title">Laravel 5</div>

and change it to

<div class="title">Hello World</div>

Save file and run. You should see Hello World in the browser on the index page.

Create a new file with the name about.blade.php in the same directory as welcome.blade.php. Copy paste the entire code from welcome.blade.php to about.blade.php. Change the text from “Laravel 5 to” “About Us”. Save the file. Next go to app/Http/routes.php and add the following code.

Route::get('/aboutus', function() {
  return view('about');
});  

Save file and run. On your site/aboutus page, you should see “About Us”. So we created a new view and and new route to get to it.

Introducing Controllers

Controllers process incoming requests, handle user input and interactions, and execute application logic. Let’s create a controlller.

Open app/Http/routes.php and add the following code to create a route for our controller:

Route::get('ctest', 'MyController@index');

MyController is the name of the controlller. index is the function we are calling. ctest means that this controller is called from site/ctest.

Go to app/Http/Controllers and create a file MyController.php. Add the following code in this file:

<?php namespace App\Http\Controllers;

class WelcomeController extends Controller {
  /**
   * Create a new controller instance
   * @return void
   */
  public function __construct() {
    $this->middleware('guest');
  }

  /**
   * Show the application welcome screen to the user
   * @return Response
   */
  public function index() {
    //return view('welcome');
    return 'hello world!';
  }
}

Note that the index function can return a string or a view. Save and run. You will see “hello world!”. If comment our 3rd last line and uncomment the line above it, the controller will call the welcome view. The controller is supposed to take input, apply relevant logic and then call the relevant view for display.

Passing Arguments to Views

Views embed dynamic content from controller into its static content. Following example shows how to pass variables to views.

app\Http\routes.php

<?php
Route::get('test/passargument', 'TestController@passargument');

This code specifies that yoursite.com/test/passargument should call passargument() method of TestController.php

app\Http\Controllers\TestController.php

<?php namespace App\Http\Controllers;

class TestController extends Controller {
  public function __construct() {
    $this->middleware('guest');
  }

  /**
  * pass arguments to the view
  * @return Response
  */
  public function passargument() {
    $people = ['Alice', 'Bob', 'Cathy'];
    return view('test', compact('people'));
  }
}

In passargument, we create a simple array and then pass it to the view test i.e. test.blade.php

resources\views\test.blade.php

<html>
<head>
    <title>Learning Laravel</title>
</head>
<body>
    <ul>
      @foreach ($people as $person)
        <li>{{ $person }}</li>
      @endforeach
    </ul>
</body>
</html>

The foreach loop runs through the array and prints its output in a list. To see the output, go to yoursite.com/test/passargument

Creating sections

Websites have sections such as header and footer that occur in almost every page. It makes sense to isolate these sections into separate files which could be imported into every page, rather than having to add code for the entire section on every page. In blade @yield, @section, and @include allow us to do this.

@yield: is a placeholder for a section @section: defines the section content @include: renders a view into another view @extends adds content to a view

**resources/views/layout/default.blade.php

<!doctype html>
<html>
<head>
<!-- css and js files -->
</head>
<body>
<div class="container">
  <!-- page header -->
  <header> @include('layout.header') </header>
  <!-- page content -->
  <div class="contents"> @yield('content') </div>
  <!-- page footer -->
  <footer> @include('layout/footer') </footer>
</div>
</body>
</html>

In this page, we are defining a page that will import 2 views (header, footer) and a section for content.

**resources/views/layout/header.blade.php

<div class="header">
  <a href="{!!URL::to('/')!!}">Home</a> | <a href="{!!URL::to('/contact')!!}">Contact Us</a>
</div>

In this view, we simply created two links.

**resources/views/layout/footer.blade.php

<div class="footer">
  &copy; All rights reserved
</div>

A view with copyright message.

resources/views/pages/home.blade.php

@extends('layout/default')
@section('content')
Welcome to Home Page!
@stop

This is the home page. Note that it adds content layout/default.blade.php by providing content for section “content”. The content of the section is all the text in between @section() and @stop.

resources/views/pages/contact.blade.php

@extends('layout/default')
@section('content')
You can contact me at 999-999-9999 or someemail@somesite.com
@stop

Similarly, this page creates the contact us page.

app/Http/routes.php

Route::get('/', function () {
    return view('pages/home');
});

Route::get('contact', function() {
  return view('pages/contact');
});

Run the code and you will see two pages (home, contact) with the same header and footer but different content.