Laravel 5 Basic Essentials

Laravel is a popular PHP framework and may programmers and employers like to use a framework. Using a framework encourages decent coding methods and can help keep code organized and secure.

Now that you know that you want to build a web application with Laravel, here are the essentials to get up and running.

Step # 1 Installation

So, you go to Laravel's website or Google around, and you see that a common way to install it is by using composer. What you say? Why I can't I just download a zip and be good to go. Well, you can actually do that by going to github and making a download. For example, you can download Laravel from Guthub at https://github.com/laravel/laravel.

However, using composer is a pretty good idea and it makes it very easy to install other frameworks like Symfony as well. So, let me help straighten you out about composer.

For Windows, you can download composer at https://getcomposer.org/download/. After that, you can open or reopen a command prompt and type composer. You will see that it works. The code below will download laravel and install it to a folder named laravel-composer. You can now build your application here.

C:\xampp\htdocs> composer create-project --prefer-dist laravel/laravel laravel-composer

Step #2 Using the Framework

After that, you will need to know how the framework operates. Let's start by building a static webpage. In order to be able to display any page, you need to set a route for get or post requests. Since this exercise is simple, the focus will be on a get request.

routes.php located at app\Http\routes.php

Open this file and add the following code.

Route::get('test', 'TestController@get_index');

Here is the breakdown. The test parameter is the file url. Thus, the url on a localhost xamp server at home would be http://localhost/laravel5/public/test. The code before the '@' symbol refers to the controller and filename located in the app\Http\Controller folder. Thus, when the url is referred to in the browser, the TestController.php and the TestController class is used. The get_index after the '@' symbol  refers to the method that is called within the class.

So, now, lets make a TestController.php file and write the appropriate class and methods. The simple code is shown below.

<?php
namespace App\Http\Controllers;

use App\Http\Requests;
use Auth;
use Input;
use Illuminate\Support\Facades\Redirect;
use Illuminate\Support\Facades\Validator;
use Illuminate\Support\Facades\View;

class TestController extends Controller
{
    public $restful = true;

    public function get_index()
    {

        $title = "My Test Title";

        $data = "Foo";
        $title = "Bar";

        $myArray = array('apples','oranges');
        
        return View::make('test.index')
            ->with('data', $data)
            ->with('title', $title)
            ->with(array('name' =>"first", 'age' => 'last'))
            ->with('myArray',$myArray);
    }

    public function post_index()
    {
        $title = "Test page";
        $input = Input::all(); // gets all input from forms
        $rules = array('html_begin' => 'required', 'html_end' => 'required');
        $v = Validator::make($input, $rules);
        if ($v->fails()) {

            return Redirect::to('login')->with_errors($v);
        } else {

            $data = array('html_begin' => $input['html_begin'], 'html_end' => $input['html_end']);

            return View::make('test.success')
                ->with('data', $data)
                ->with('title', $title);

        }
    }

}

After the workhorse file which does the heavy lifting is finished, you can make the view file.

View Files

Look for the folder called resources\views. Now, make another folder called test. Within this folder, make a file called index.blade.php. This is the file that is rendered from the TestController. As you can see, the variables and arrays created in the controller are parsed within curly tags. For example, this is a vairable {{ $variable }} and this accesses an array item {{ $array[key] here }}.

<html>

<body><div class="row">
    <div class ="span4 offset4">
        <div>{{ $title }} add variables here {{ $name }} and parsing array is {{ $myArray[0] }} and {{ $myArray[1] }}</div>
        <div class="bs-example well-sm" style="border:1px solid #C1BFBF; width:250px; border-radius:1em; margin: 0 auto; ">


        </div>
    </div>
</div>
</body></html>

Without getting too heavy with theory, these simple points makes it easy to piece together your logic and views. Although only single templates are used in this example, you can create master templates for an application to keep separate files for headers, footers, etc.

@extends('master')
@section('content')

<div class="row">
    <div class ="span4 offset4">
        <div>{{ $title }} add variables here {{ $name }} and parsing array is {{ $myArray[0] }} and {{ $myArray[1] }}</div>
        <div class="bs-example well-sm" style="border:1px solid #C1BFBF; width:250px; border-radius:1em; margin: 0 auto; ">


        </div>
    </div>
</div>

@endsection

Here is explanation about what is happening above. The master template file sits only folder up from the current view file. Thus, this current file extends the master template. The content between the @section tags is displayed in the master.blade.php file because it has a line that reads @yield('content'). The line displays the content from the page in the exact spot.

Conclusion:
So there you have it, a simple web app. The next stage would be to setup a database that you can use for an application. When using a database, you will execute queries within your controller. The syntax is very similar to typical PHP / mySQL queries.