The document provides an overview of Laravel, a PHP web application framework. It summarizes Laravel's main features such as routing, controllers, models, views, and migrations. It also describes how to set up Laravel, use the Artisan command line tool, write queries with Eloquent ORM, and install additional packages. The presentation is delivered by Nikhil Agrawal of Mindfire Solutions.
4. 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. 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. 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. 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
9. 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 model
Route::model('user_id', 'User');
Route::get('user/profile/{user_id}', function(User $user){
return $user->firstname . ' ' . $user->lastname
})
Presenter: Nikhil Agrawal, Mindfire Solutions
10. 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. 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
13. 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. 14
Query Scope (Eloquent ORM)
โ Allow re-use of logic in model.
Presenter: Nikhil Agrawal, Mindfire Solutions
15. 15
Many others Eloquent features..
โ Defining Relationships
โ Soft deleting
โ Mass Assignment
โ Model events
โ Model observer
โ Eager loading
โ Accessors & Mutators
17. 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. 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. 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 migration
Presenter: Nikhil Agrawal, Mindfire Solutions
Steps
20. 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 Error
App::missing(function($exception){
return Response::view('errors.missing');
});
โ Handling fatal error
App::fatal(function($exception){
return "Fatal Error";
});
โ Generate HTTP exception using
App:abort(403);
โ Logging error
โ Log::warning('Somehting is going wrong'); //Info,error