laravel - website development in php framework

45

Upload: swaam-tech

Post on 15-Apr-2017

3.233 views

Category:

Internet


4 download

TRANSCRIPT

Page 1: Laravel - Website Development in Php Framework
Page 2: Laravel - Website Development in Php Framework

Laravel The PHP Framework For Web Artisans

Presented by : Syeda Aimen BatoolDeveloper @ Swaam Tech

Page 3: Laravel - Website Development in Php Framework

We will discuss:• Laravel Features• Composer and Laravel Setup• Directory Structure• Routing• Controller & Model• CRUD Examples• Database Migrations• User Authentication• Controller to View Communication• Basic Commands• Databases (Basic Queries)• Form Validation• Blade Template Engine

Quick Guide On Laravel

www.swaam.com

Page 4: Laravel - Website Development in Php Framework

FeaturesLaravel is an open source MVC PHP Framework under MIT license. Bundles are packages which you can download to add particular functionality in your web application to save coding and time. Eloquent ORM provides a simple ActiveRecord implementation for working with database.Class Auto loading assures that correct components are loaded at correct time.Unit testing allows users to easily create and run unit tests to ensure application stability.

Quick Guide On Laravel

www.swaam.com

Page 5: Laravel - Website Development in Php Framework

Requirements• Apache or any other compatible web server• PHP Version should be 5.5.9 or greater • PDO PHP Extension should be enabled PDO is by default enable in php 5.1.0 or greater. You can manually activate by uncommenting statement below in php.ini by removing semicolon at beginning extension=php_pdo.dll

• OpenSSL PHP Extension should be enabled to manually activate OpenSSL extension uncomment the line extension=php_openssl.dll by removing the semicolon at beginning

• okenizer PHP Extension should be enabledThis extension is by default enabled in php versions 4.3.0 or greater

Quick Guide On Laravel

Page 6: Laravel - Website Development in Php Framework

MVC Layers• Model (Eloquent ORM)

Model represents the logical structure of an application e.g. list of database record

• View (Blade Engine) View displays the data user see on screen such as buttons, display boxes etc.

• Controller Controller represents the classes connecting the view and model, it helps model and view to communicate with each other

Quick Guide On Laravel

www.swaam.com

Page 7: Laravel - Website Development in Php Framework

Composer and Laravel Setup:• A dependencies management tool.• Download Composer from here.• Run setup.• Browse php.exe file under xampp/php/php.exe.• After successful installation; open your cmd. open cmd execute composer.phar to check if composer is successfully installed

• Download the Laravel installer by writing given command in cmd.Composer global require “laravel/installer=~1.1”

• Create a new project by running following command in cmd.Composer create-project laravel/laravel --prefer-dist Installing…

Quick Guide On Laravel

www.swaam.com

Page 8: Laravel - Website Development in Php Framework

Hello Laravel • After installation hit “http://localhost/laravel/public/” in your

browser• Remove public from your url by following this:– Go to D:\xampp\htdocs\laravel\public– Cut index.php and .htaccess file and paste here: D:\xampp\htdocs\laravel – Open index.php and change bootstrap path: ../../bootstrap/ to ../bootstrap/ in whole file– Now hit your url without public “http://localhost/laravel/”– Congratulations ! You have successfully setup Laravel.

Quick Guide On Laravel

www.swaam.com

Page 9: Laravel - Website Development in Php Framework

Directory Structure:• Routes are available under app directory:

D:\xamp\htdocs\laravel\app\Http\routes.php

• Controllers are available at:D:\xamp\htdocs\laravel\app\Http\Controller

• User Authentication is available at:D:\xamp\htdocs\laravel\app\Http\Controllers\Auth

• All your assets and views are available at:D:\xamp\htdocs\laravel\resources

• Models are available at:D:\xamp\htdocs\laravel\app

Quick Guide On Laravel

www.swaam.com

Page 10: Laravel - Website Development in Php Framework

My First Routes:All routes are available at D:\xamp\htdocs\laravel\app\Http\routes.php

• Default Route:A root looks like => Route::get('/home', 'WelcomeController@index');Where ‘/home’ is you will enter in url 'WelcomeController is your application controller Index is a function in your controllerHitting ‘/home’ in url will invoke your controller and call function index

– Route::get(‘Home', ‘HomeController@index');

– Route::post(‘application/create', ‘ApplicationController@create');

– Route::patch(‘application/update', ‘ApplicationController@update');

Quick Guide On Laravel

www.swaam.com

Page 11: Laravel - Website Development in Php Framework

Named Routes:• Giving a specific name to a route:

Route::get('songs',['as'=>'songs_path‘ , 'uses'=>'SongsController@index']); where songs_path Is name specified to this particular route, we can use this name in our app instead of

writing route. e.g. <a href="{{ route('songs_path')}}"> will be a hyperlink to this route.

• Just an other way:$router -> get('songs',['as'=>'songs_path','uses'=>'SongsController@index']);We can define routes in a way above also.

Quick Guide On Laravel

www.swaam.com

Page 12: Laravel - Website Development in Php Framework

Make Controller

• Open cmd and write php artisan make:controller SongController

• Controller is created with following default functions: I. create()II. store(Request $request)III. show($id)IV. edit($id)V. update(Request $request, $id)VI. destroy($id)

Quick Guide On Laravel

Page 13: Laravel - Website Development in Php Framework

Make Model

• Write following command in cmd: php artisan make:model Song

• Model will be downloaded under app directory

Quick Guide On Laravel

Page 14: Laravel - Website Development in Php Framework

Make Database Migration

• Write following command in cmd php artisan make:migration create_songs_table --create=songs Find your migration here D:\xampp\htdocs\laravel\database\migrations

• It has two functions up and down.• Up function contains the description of your database table fields. • Down function contains the query to drop your database table.

Quick Guide On Laravel

Page 15: Laravel - Website Development in Php Framework

Run Migration• Before running migration open .env file from your project’s root directory.• Setup your database name and credentials:

• After defining your table fields in UP function; run following command in cmd: php artisan migrate

• Your table is now created in database after successful run of above command.

Quick Guide On Laravel

Page 16: Laravel - Website Development in Php Framework

Define Your Routes

• You can not perform any action without defining your application routes.

• Define all your routes in your route file.

Quick Guide On Laravel

Page 17: Laravel - Website Development in Php Framework

User Authentication

• These lines help you to authenticate user in laravel Route::controllers([ 'auth' => 'Auth\AuthController', 'password' => 'Auth\PasswordController' ]);

URL above will be accessed by logged users only.

Quick Guide On Laravel

www.swaam.com

Page 18: Laravel - Website Development in Php Framework

Songs ListingQuick Guide On Laravel

Page 19: Laravel - Website Development in Php Framework

Insert a Song

• An object of model song is created.• Value are assigned to fields and then saved in table.

Quick Guide On Laravel

Page 20: Laravel - Website Development in Php Framework

Insert a Song

• Another way

Quick Guide On Laravel

Page 21: Laravel - Website Development in Php Framework

Insert a Song Form Quick Guide On Laravel

Page 22: Laravel - Website Development in Php Framework

Quick Guide On Laravel

Page 23: Laravel - Website Development in Php Framework

Songs ListingQuick Guide On Laravel

Page 24: Laravel - Website Development in Php Framework

Update a Song

Quick Guide On Laravel

Page 25: Laravel - Website Development in Php Framework

Updated Songs Quick Guide On Laravel

Page 26: Laravel - Website Development in Php Framework

Delete a Song

Quick Guide On Laravel

Page 27: Laravel - Website Development in Php Framework

Updated Record

Quick Guide On Laravel

Page 28: Laravel - Website Development in Php Framework

Controller to ViewPassing data to view: return view('songs.show',compact('song'));

where ‘song’ variable contains your data, compact method will send your data to the view named show under songs directory.

Quick Guide On Laravel

www.swaam.com

Page 29: Laravel - Website Development in Php Framework

Success / Failure Message from Controller• Passing success/failure message

• Another waySetting message to a specific file create under songs directory.

Quick Guide On Laravel

Page 30: Laravel - Website Development in Php Framework

Display Message in View

Quick Guide On Laravel

Page 31: Laravel - Website Development in Php Framework

Commands You Must Know:• To make a controller php artisan make:controller SongController

• To make a migration php artisan make:migration create_Songs_table --create=songs

• To make a model php artisan make:model Song

• Checking request parameters (Debugging) dd(\Request::get('lyrics')); dd(\Request::input();)

Quick Guide On Laravel

www.swaam.com

Page 32: Laravel - Website Development in Php Framework

DATABASE INTERACTION

Quick Guide On Laravel

www.swaam.com

Page 33: Laravel - Website Development in Php Framework

Databases Laravel SupportsCurrently four databases are supported by laravel:• MySQL• Postgre• SQLite• SQL Server

Quick Guide On Laravel

www.swaam.com

Page 34: Laravel - Website Development in Php Framework

CRUD • Saving a new record write the following lines of code in controller $song = new Song; // Song is your model, $song is object of class Song $song->title = ‘First Song'; $song->save(); // saving your data

• Saving a new record (another way) in controller $song = Song::create([title' => First song']);

• Retrieve the song by the attributes, or create it if it doesn't exist in controller $song = Song::firstOrCreate([‘title' => First song']);

Quick Guide On Laravel

www.swaam.com

Page 35: Laravel - Website Development in Php Framework

CRUD Continued • Updating a model $song = Song::find(1); $song->title = ‘2nd song '; $song->save();Find model by id and update the title field with new title.

• Delete a record $song = Song::find(1); $song->delete();

Quick Guide On Laravel

www.swaam.com

Page 36: Laravel - Website Development in Php Framework

Some Common Queries • Get all data $result = Student::all();

• Get a single record $song = DB::table(‘songs')->where('name', ‘First song')->first();

• Getting a single value from a row $lyrics = DB::table(‘songs')->where('name', First song')->value(‘lyrics');

• Get a list of column values $titles = DB::table(‘songs')->lists('title'); foreach ($titles as $title) { echo $title; }

Quick Guide On Laravel

www.swaam.com

Page 37: Laravel - Website Development in Php Framework

Forms in LaravelLaravel4 contained a form helper package which is removed from LaravelIf form helper is not included by default Open cmd and write composer require "illuminate/html":"5.0.*"

Then add the service provider and aliases Open /config/app.php and update as follows:'providers' => [ ... 'Illuminate\Html\HtmlServiceProvider', ], ‘aliases' => [ ... 'Form'=> 'Illuminate\Html\FormFacade', ‘HTML'=> 'Illuminate\Html\HtmlFacade', ],

Quick Guide On Laravel

www.swaam.com

Page 38: Laravel - Website Development in Php Framework

Form Validation $this->validate($request, [ 'title' => 'required|max:2', ‘lyrics' => 'required|min:10', ]);

You can validate your fields using validate function.

Don’t forget to include form helper!

Quick Guide On Laravel

www.swaam.com

Page 39: Laravel - Website Development in Php Framework

Quick View of Blade Template • Laravel officially use Blade Template engine for views. • File is saved with .blade.php extention • Rich syntax of blade templates is sync with Phpstorm latest

version.

Quick Guide On Laravel

www.swaam.com

Page 40: Laravel - Website Development in Php Framework

Blade Template Syntax TripA blade layout<html><body> @section('sidebar') This is the master sidebar. @show

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

Quick Guide On Laravel

www.swaam.com

Page 41: Laravel - Website Development in Php Framework

Blade Template Syntax Trip@extends('layouts.master')

@section('sidebar') <p>This is appended to the master sidebar.</p>@stop

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

@section defines the content section for our page, such as header, footer, left bar etc. which you can yield as in previous slide.

Quick Guide On Laravel

www.swaam.com

Page 42: Laravel - Website Development in Php Framework

Control Structures• Conditional Statements@if ($var == 1 ) value is one! @elseif ($var == 2) Value is two!@else Zero value ! @endif

@unless (Auth::check()) You are not signed in.@endunless

Quick Guide On Laravel

www.swaam.com

Page 43: Laravel - Website Development in Php Framework

Loops in Blade@for(…) // stuff to do @endfor

@while(condition)//stuf to do @endwhile

@foreach($loops as $loop)// stuff to do@endforeach

Quick Guide On Laravel

www.swaam.com

Page 44: Laravel - Website Development in Php Framework

Thank You.

Page 45: Laravel - Website Development in Php Framework

Get in Touch Explore Our Services

We’ve helped several clients with industries like

Email: [email protected] Web Address: www.swaam.com