11. The MVC Layers – Eloquent ORM
●
Active record style
●
Easy to use
class Book extends Eloquent
{
protected $table = 'book';
protected $primaryKey = 'book_id';
public $timestamps = false;
}
12. The MVC Layers – Eloquent ORM
●
Some examples of queries:
Book::all();
Book::find(1);
Book::where('name', '=', 'Michael Cheng');
●
Insert / Update
$b = new Book();
$b->title = 'Laravel Basics';
$b->description = 'A very nice book';
$b->save();
$b = Book::find(2);
$b->title = 'Laravel Advanced';
$b->save();
13. The MVC Layers – Eloquent ORM
●
Relationship mapping
public function author()
{
return $this->belongsTo('Author', 'author_id');
}
14. The MVC Layers – Eloquent ORM
●
Supports soft delete
protected $softDelete = true;
// Automatically excludes soft deleted rows
Book::all();
// Include soft deleted rows
Book::withTrashed()->get();
// Include only soft deleted rows
Book::onlyTrashed()->get();
// Undo the soft delete
$b = Book::withTrashed()->where('book_id, '=', 1);
$b->restore();
15. The MVC Layers – Eloquent ORM
●
Supports query scope
public function scopePopular($query)
{
return $query->where('rating', '>', '4');
}
Book::popular()->get();
16. The MVC Layers – Eloquent ORM
●
Supports accessors and mutators
// Accessor
public function getGenderAttribute($value)
{
return ($value == 'm') ? 'Male' : 'Female';
}
// Mutator
public function setNameAttribute($value)
{
$this->attributes['name'] = strtolower($value);
}
17. The MVC Layers – Blade Engine
●
Stock templating engine for Laravel
●
Supports template inheritance and sections
<html>
<head>
<title>@yield('page_title')</title>
@yield('css')
@yield('javascript')
</head>
<body>
Some text
@yield('content')
</body>
</html>
18. The MVC Layers – Blade Engine
@extends('layout')
@section('page_title', 'Login Page')
@section('css')
<link rel="stylesheet" type="text/css" href="mystyle.css" />
@endsection
@section('javascript')
<script type='text/javascript' src='jquery.js'></script>
@endsection
@section('content')
This is the content for a particular page.
@endsection
19. The MVC Layers – Blade Engine
●
Some control structures
@if (.....)
....
@elseif ....
@else ....
@endif
@unless (....)
....
@endunless
21. The MVC Layers – Blade Engine
●
Alternate way to echo variable values
My name is <?php echo $user->name ?>
My name is {{ $user->name }}
22. The MVC Layers - Controller
●
Basic controller
// The controller itself
class AuthController extends BaseController
{
public function showLoginForm()
{
return View::make('auth.loginForm');
}
}
// Routing file
Route::get('/auth', 'AuthController@showLoginForm')
23. The MVC Layers - Controller
●
RESTful controller
// The controller itself
class BookController extends BaseController
{
public function getShowAll()
{
// ..............
}
public function postAdd()
{
// ..............
}
}
// Routing file
Route::controller('/books', 'BookController');
24. The MVC Layers - Controller
Resource controller
–
Generated using Artisan CLI
–
Allows easy RESTful implementation
25. The MVC Layers - Controller
Resource controller
Paths and route names are generated automatically
Verb
Path
Action
Route Name
GET
/resource
index
resource.index
GET
/resource/create
create
resource.create
POST
/resource/
store
resource.store
GET
/resource/{id}
show
resource.show
GET
/resource/{id}/edit edit
resource.edit
PUT / PATCH
/resource/{id}
update
resource.update
DELETE
/resource/{id}
destroy
resource.destroy
28. Authentication – Application Level
●
Many convenience methods
// Attempt to login
Auth::attempt(array('email' => $email, 'password' => $pwd))
// Check if the current user is logged in
Auth::check();
// Access the logged in user
$name = Auth::user()->name;
// Manually login a user with its ID
Auth::loginUsingId(1);
// Log the user out
Auth::logout();
29. Authentication – Application Level
Uses Bcrypt as the default hashing algorithm, with a
default workload of 8
Workload can be changed freely
Algorithm itself can be changed by framework extension
// Workload 8 by default
$hash = Hash::make('password');
// Change workload
$hash = Hash::make('password', array('rounds' => 12));
30. IoC Container
For managing class dependencies
Promotes inversion of control by injecting dependencies at
runtime
Promotes greater flexibility by allowing dependency
implementations to be swapped easily
2 ways for resolving types:
Closure
Automatic Resolution
32. IoC Container
Automatic Resolution
class UserManager
{
private $repo;
public function __construct(UserRepository $repo)
{
$this->repo = $repo;
}
}
$u = App::make('UserManager');
33. IoC Container
Automatic Resolution
class UserManager
{
private $repo;
public function __construct(UserRepositoryInterface $repo)
{
$this->repo = $repo;
}
}
// Interface resolution
App::bind('UserRepositoryInterface', 'DbUserRepository');
// Now we can use the IoC container to resolve the dependencies
$u = App::make('UserManager');
34. Facade
●
●
Provides a static-like syntax to access objects and their
methods in the IoC container
These are facades in action, and they are NOT static
method calls:
Auth::attempt()
Input::get()
Cookie::make()
Event::subscribe()
35. Facade
●
How does it work?
An object is registered into the IoC container
A facade is created to reference that object
An alias is defined to use the facade without importing
its namespace
36. Service Provider
●
●
A great way to add reusable components into your
application
Custom authentication driver, database driver, support
modules etc etc etc...
37. Service Provider
class MongoDBServiceProvider extends ServiceProvider
{
public function register()
{
$this->app->bind('mongodb', function() {
return new MongoDbManager();
});
}
}
// Register the service provider
App::register('MongoDBServiceProvider');
// Now you can use the Mongo DB manager
$mongodb = App::make('mongodb');
$mongodb->connect(....);
$mongodb->add(....);
38. Tips For Learning Laravel
●
●
Documentation is your best friend
Read the API to discover features not mentioned in the
documentation
●
Get used to closures
●
Learn the IoC container and service provider (MUST)