Laravel

THE PHP FRAMEWORK FOR WEB ARTISANS.

PHP THAT DOESN'T HURT. CODE HAPPY & ENJOY THE FRESH AIR.

 

Installation

  • Via Laravel Installer
    1. http://laravel.com/laravel.phar
    2. First, download the Laravel installer PHAR archive
    3. Make it global accessible
    4. Command: laravel new blog

  • Via Composer
    1. composer create-project laravel/laravel your-project-name
    2. Alternatively download laravel repository from Git

Project Folders

  • APP < YOUR STUFF
  • BOOTSTRAP < STARTUP STUFF
  • PUBLIC < STATIC STUFF: IMG, JS, CSS, ETC.
  • COMPOSER.JSON < OTHER PEOPLE'S STUFF YOU WANT
  • VENDOR < OTHER PEOPLE'S STUFF

project structure

Application Folders

  • app
    • commands
    • config
    • controllers
    • database
    • lang
    • models
    • start
    • storage
    • tests
    • views
    • filters.php
    • routes.php

routing

Routing

File Path: app/routes.php
GET, POST, MATCH, ANY 
Route::get('users', function(){
    return 'Users!';
});
Routes can also be attached to controller classes.
Route::get('users', 'UserController@getIndex');

rOUTING

Route Parameters
Route::get('user/{id}', function($id)
{
    return 'User '.$id;
});
Route::get('user/{name?}', function($name = null)
{
    return $name;
});
Route::get('user/{name}', function($name)
{
    //
})
->where('name', '[A-Za-z]+');

Route::get('user/{id}', function($id)
{
    //
})
->where('id', '[0-9]+');

Resource Controllers

php artisan controller:make PhotoController
Route::resource('photo', 'PhotoController');
Verb Path Action Route Name
GET /resource index resource.index
GET /resource/create create resource.create
POST /resource store resource.store
GET /resource/{resource} show resource.show
GET /resource/{resource}/edit edit resource.edit
PUT/PATCH /resource/{resource} update resource.update
DELETE /resource/{resource} destroy resource.destroy

Views

Master page

File Path: app/views
layout.blade.php
<html>
    <body>
        <h1>Laravel Quickstart</h1>

        @yield('content')
    </body>
</html>
users.blade.php
@extends('layout')

@section('content')
    Users!
@stop

Templates

Controller Layouts
class UserController extends BaseController {

    /**
     * The layout that should be used for responses.
     */
    protected $layout = 'layouts.master';

    /**
     * Show the user profile.
     */
    public function showProfile()
    {
        $this->layout->content = View::make('user.profile');
    }

}

Blade templating

Blade Templating

<!-- Stored in app/views/layouts/master.blade.php -->
<html>
    <body>
        @section('sidebar')
            This is the master sidebar.
        @show

        <div class="container">
            @yield('content')
        </div>
    </body>
</html>
@extends('layouts.master')

@section('sidebar')
    @parent

    <p>This is appended to the master sidebar.</p>
@stop

@section('content')
    <p>This is my body content.</p>
@stop

Other Blade Control Structures

{{{ isset($name) ? $name : 'Default' }}}{{{ $name or 'Default' }}}@{{ This will not be processed by Blade }}@if (count($records) === 1)    I have one record!@elseif (count($records) > 1)    I have multiple records!@else    I don't have any records!@endif
@unless (Auth::check()) You are not signed in.@endunless@for ($i = 0; $i < 10; $i++) The current value is {{ $i }}@endfor
@foreach ($users as $user) <p>This is user {{ $user->id }}</p>@endforeach
@while (true) <p>I'm looping forever.</p>@endwhile

handling forms

forms and html

{{ Form::open(array('url' => 'foo/bar')) }}
    //
{{ Form::close() }}
echo Form::label('email', 'E-Mail Address', array('class' => 'awesome'));
echo Form::text('email', 'example@gmail.com');
echo Form::checkbox('name', 'value', true);

echo Form::radio('name', 'value', true);
echo Form::file('image');
echo Form::select('size', array('L' => 'Large', 'S' => 'Small'), 'S');
echo Form::submit('Click Me!');

validation

$validator = Validator::make(
    array(
        'name' => 'Dayle',
        'password' => 'lamepassword',
        'email' => 'email@example.com'
    ),
    array(
        'name' => 'required',
        'password' => 'required|min:8',
        'email' => 'required|email|unique:users'
    )
);
if ($validator->fails())
{
    // The given data did not pass validation
}
$messages = $validator->messages();
echo $messages->first('email');

Error Messages & Views

Route::get('register', function()
{
    return View::make('user.register');
});

Route::post('register', function()
{
    $rules = array(...);

    $validator = Validator::make(Input::all(), $rules);

    if ($validator->fails())
    {
        return Redirect::to('register')->withErrors($validator);
    }
});
<?php echo $errors->first('email'); ?>

Creating A Migration

Database Configuration path: app/config/database.php
Migrations Files Path: app/database/migrations
php artisan migrate:make create_users_table
public function up()
{
    Schema::create('users', function($table)
    {
        $table->increments('id');
        $table->string('email')->unique();
        $table->string('name');
        $table->timestamps();
    });
}

public function down()
{
    Schema::drop('users');
}
php artisan migrate

Eloquent ORM

class User extends Eloquent {}
Route::get('users', function()
{
    $users = User::all();

    return View::make('users')->with('users', $users);
});
@extends('layout')

@section('content')
    @foreach($users as $user)
        <p>{{ $user->name }}</p>
    @endforeach
@stop


Presented by:
Muhammad Rizwan Arshad

rizwan.arshad [at] nxb [dot] com [dot] pk

Laravel ( An Overview )

By gr8rizwan

Laravel ( An Overview )

  • 853