getting started-with-laravel

24
1 Getting started with Laravel Presenter : Nikhil Agrawal Mindfire Solutions Date: 22nd April, 2014

Upload: mindfire-solutions

Post on 27-Aug-2014

741 views

Category:

Software


3 download

DESCRIPTION

Laravel is a PHP MVC based framework. It is as easy as codeigniter, yet provides powerful tools needed for large robust application.It is built on top of symphony components and is inspired by many other frameworks including RoR, Asp .net, Sinatra.This session focuses on the basics things needed to start building application on it.

TRANSCRIPT

Page 1: Getting Started-with-Laravel

1

Getting started with

Laravel

Presenter :Nikhil AgrawalMindfire SolutionsDate: 22nd April, 2014

Page 2: Getting Started-with-Laravel

2

Zend PHP 5.3 Certified

OCA-1z0-870 - MySQL 5 CertifiedDELF-A1-French Certified

Skills: Laravel, cakePHP, Codeigniter, Mysql, Jquery, HTML, css

About me

Presenter: Nikhil Agrawal, Mindfire Solutions

Connect Me:

https://www.facebook.com/nkhl.agrawal

http://in.linkedin.com/pub/nikhil-agrawal/33/318/21b/

https://twitter.com/NikhilAgrawal44

Contact Me:

Email: [email protected], [email protected]: mfsi_nikhila

Page 3: Getting Started-with-Laravel

3

Content

✔ Introduction

✔ Features

✔ Setup

✔ Artisan CLI

✔ Routing

✔ Controller, Model, View (MVC)

✔ Installing packages

✔ Migration

✔ Error & Logging

✔ References & QA

Presenter: Nikhil Agrawal, Mindfire Solutions

Page 4: Getting Started-with-Laravel

4

Introduction

✔ Started by Taylor Otwell (a c# developer).

✔ PHP V>= 5.3 based MVC framework.

✔ Hugely inspired by other web framework including frameworks in other language such as ROR, ASP.NET and Sinatra

✔ Composer-based

✔ Has a command line interface called as Artisan

✔ Current stable version available is 4.1

Presenter: Nikhil Agrawal, Mindfire Solutions

Page 5: Getting Started-with-Laravel

5

Features out-of-the-box

✔ Flexible routing

✔ Powerful ActiveRecord ORM

✔ Migration and seeding

✔ Queue driver

✔ Cache driver

✔ Command-Line Utility (Artisan)

✔ Blade Template

✔ Authentication driver

✔ Pagination, Mail drivers, unit testing and many more

Presenter: Nikhil Agrawal, Mindfire Solutions

Page 6: Getting Started-with-Laravel

6

Setup

✔ Install Composer

curl -sS https://getcomposer.org/installer | php

✔ It is a dependency manager✔ What problem it solves?✔ composer.json, composer.lock files

✔ Install laravel / create-project.

✔ Folder structure

✔ Request lifecycle

✔ Configuration

Presenter: Nikhil Agrawal, Mindfire Solutions

Page 7: Getting Started-with-Laravel

7

Artisan

✔ Artisan is a command-line interface tool to speed up development process

✔ Commands

✔ List all commands : php artisan list

✔ View help for a commands : php artisan help migrate

✔ Start PHP server : php artisan serve

✔ Interact with app : php artisan tinker

✔ View routes : php artisan routes

✔ And many more...

Presenter: Nikhil Agrawal, Mindfire Solutions

Page 8: Getting Started-with-Laravel

8

Routing

✔ Basic Get/Post route

✔ Route with parameter & https

✔ Route to controller action

Route::get('/test', function() { return 'Hello'

})

Route::get('/test/{id}', array('https', function() { return 'Hello'

}));

Route::get('/test/{id}', array('uses' => 'UsersController@login','as' => 'login')

)

Presenter: Nikhil Agrawal, Mindfire Solutions

Page 9: Getting Started-with-Laravel

9

Routing cont..

✔ Route group and prefixes

✔ Route Model binding

✔ Url of route:✔ $url = route('routeName', $param);

✔ $url = action('ControllerName@method', $param)

Route::group(array('before' => 'auth|admin', 'prefix' => 'admin'), function (){//Different routes

})

Binding a parameter to modelRoute::model('user_id', 'User');

Route::get('user/profile/{user_id}', function(User $user){return $user->firstname . ' ' . $user->lastname

})

Presenter: Nikhil Agrawal, Mindfire Solutions

Page 10: Getting Started-with-Laravel

10

Controller

✔ Extends BaseController, which can be used to write logic common througout application

✔ RESTful controller✔ Defining route

Route::controller('users', 'UserController');

✔ Methods name is prefixed with get/post HTTP verbs

✔ Resource controller✔ Creating using artisan

✔ Register in routes

✔ Start using it

Presenter: Nikhil Agrawal, Mindfire Solutions

Page 11: Getting Started-with-Laravel

11

Model

✔ Model are used to interact with database.

✔ Each model corresponds to a table in db.

✔ Two different ways to write queries

1. Using Query Builder

2. Using Eloquent ORM

Presenter: Nikhil Agrawal, Mindfire Solutions

Page 12: Getting Started-with-Laravel

12

Query Builder (Model)

✔ SELECT

✔ $users = DB::table('users')->get();

✔ INSERT

✔ $id = DB::table('user')

->insertGetId(array('email' =>'[email protected]', 'password' => 'mindfire'));

✔ UPDATE

✔ DB::table('users')

->where('active', '=', 0)

->update(array('active' => 1));✔ DELETE

✔ DB::table('users')

->delete();

Presenter: Nikhil Agrawal, Mindfire Solutions

Page 13: Getting Started-with-Laravel

13

Eloquent ORM (Model)

✔ Defining a Eloquent model

✔ Class User Extends Eloquent { }

✔ Try to keep the table name plural and the respective model as singular (e.g users / User)

✔ SELECT

✔ $user = User::find(1) //Using Primary key

✔ $user = User::findorfail(1) //Throws ModelNotFoundException if no data is found

✔ $user = Menu::where('group', '=', 'admin')->remember(10)->get();

//Caches Query for 10 minutes✔ INSERT

✔ $user_obj = new User();

$user_obj->name = 'nikhil';

$user_obj->save();

✔ $user = User::create(array('name' => 'nikhil'));

Page 14: Getting Started-with-Laravel

14

Query Scope (Eloquent ORM)

✔ Allow re-use of logic in model.

Presenter: Nikhil Agrawal, Mindfire Solutions

Page 15: Getting Started-with-Laravel

15

Many others Eloquent features..

✔ Defining Relationships

✔ Soft deleting

✔ Mass Assignment

✔ Model events

✔ Model observer

✔ Eager loading

✔ Accessors & Mutators

Page 16: Getting Started-with-Laravel

16

View (Blade Template)

Presenter: Nikhil Agrawal, Mindfire Solutions

Page 17: Getting Started-with-Laravel

17

Loops

View Composer

✔ View composers are callbacks or class methods that are called when a view is rendered.

✔ If you have data that you want bound to a given view each time that view is rendered throughout your application, a view composer can organize that code into a single location

View::composer('displayTags', function($view){

$view->with('tags', Tag::all()); });

Presenter: Nikhil Agrawal, Mindfire Solutions

Page 18: Getting Started-with-Laravel

18

Installing packages

✔ Installing a new package:✔ composer require <packageName> (Press Enter)

✔ Give version information

✔ Using composer.json/composer.lock files✔ composer install

✔ Some useful packages✔ Sentry 2 for ACL implementation

✔ bllim/datatables for Datatables

✔ Way/Generators for speedup development process

✔ barryvdh/laravel-debugbar

✔ Check packagist.org for more list of packages

Presenter: Nikhil Agrawal, Mindfire Solutions

Page 19: Getting Started-with-Laravel

19

Migration

Developer A

✔ Generate migration class

✔ Run migration up

✔ Commits file

Developer B

✔ Pull changes from scm

✔ Run migration up

1. Install migration

✔ php artisan migrate install

2. Create migration using

✔ php artisan migrate:make name

3. Run migration

✔ php artisan migrate

4. Refresh, Reset, Rollback migrationPresenter: Nikhil Agrawal, Mindfire Solutions

Steps

Page 20: Getting Started-with-Laravel

20

Error & Logging

✔ Enable/Disable debug error in app/config/app.php

✔ Configure error log in app/start/global.php

✔ Use Daily based log file or on single log file

✔ Handle 404 ErrorApp::missing(function($exception){

return Response::view('errors.missing');

});

✔ Handling fatal errorApp::fatal(function($exception){

return "Fatal Error";

});

✔ Generate HTTP exception usingApp:abort(403);

✔ Logging error✔ Log::warning('Somehting is going wrong'); //Info,error

Page 21: Getting Started-with-Laravel

21

References

● http://laravel.com/docs (Official Documentation)

● https://getcomposer.org/doc/00-intro.md (composer)

● https://laracasts.com/ (Video tutorials)

● http://cheats.jesse-obrien.ca/ (Cheat Sheet)

● http://taylorotwell.com/

Presenter: Nikhil Agrawal, Mindfire Solutions

Page 22: Getting Started-with-Laravel

22

Questions ??

Presenter: Nikhil Agrawal, Mindfire Solutions

Page 23: Getting Started-with-Laravel

23

A Big

Thank you !

Presenter: Nikhil Agrawal, Mindfire Solutions

Page 24: Getting Started-with-Laravel

24

Connect Us @

www.mindfiresolutions.com

https://www.facebook.com/MindfireSolutions

http://www.linkedin.com/company/mindfire-solutions

http://twitter.com/mindfires