Getting started-with-laravel

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

Transcript of Getting started-with-laravel

1

Getting started with

Laravel

Presenter :Nikhil AgrawalMindfire SolutionsDate: 22nd April, 2014

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

3

Content

✔ Introduction

✔ Features

✔ Setup

✔ Artisan CLI

✔ Routing

✔ Controller, Model, View (MVC)

✔ Installing packages

✔ Migration

✔ Error & Logging

✔ References & QA

Presenter: Nikhil Agrawal, Mindfire Solutions

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

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

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

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

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

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

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

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

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

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'));

14

Query Scope (Eloquent ORM)

✔ Allow re-use of logic in model.

Presenter: Nikhil Agrawal, Mindfire Solutions

15

Many others Eloquent features..

✔ Defining Relationships

✔ Soft deleting

✔ Mass Assignment

✔ Model events

✔ Model observer

✔ Eager loading

✔ Accessors & Mutators

16

View (Blade Template)

Presenter: Nikhil Agrawal, Mindfire Solutions

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

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

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

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

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

22

Questions ??

Presenter: Nikhil Agrawal, Mindfire Solutions

23

A Big

Thank you !

Presenter: Nikhil Agrawal, Mindfire Solutions

24

Connect Us @

www.mindfiresolutions.com

https://www.facebook.com/MindfireSolutions

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

http://twitter.com/mindfires