3. History
- DHH released first version of ruby on rails on 2004
- Some of php frameworks got inspired by RoR
- In 2010 taylor become dissatisfied with CodeIgniter as
They were slow to catch up new features like namesapces
4. History
- Laravel 1 (June 2011) included built in support for
(authentication - routing - models - views - ..)
- Laravel 2 (September 2011) included support for
(controllers - blade template engine - IOC - .. )
- Laravel 3 (February 2012) included support for
(CLI Artisan - events - migrations - bundles - ..)
5. History
- Laravel 4 (May 2013 ) taylor rewrote framework from
scratch and developed set of components under code name
Illuminate , and pulled majority of components from
symfony also included support for ( seeding - soft delete - ..)
- Laravel 5 (Feb 2015) new directory tree structure and
included support for (socialite - elixir - dotenv -...)
6. Composer
- Composer is a dependency manager like npm for node or
ruby gems for ruby .
- Download From here
https://getcomposer.org/download/
- Make composer globally
https://getcomposer.org/doc/00-intro.md#globally
8. Directory Structure
- Laravel Version 5.4 you will see :-
- app/ --> where your application go like ( models controllers)
- bootstrap/ --> files laravel uses to boot every time
- config/ --> configuration files like (database configuration)
- database/ --> where (migrations - seeds - factories) exist
- public/ --> directory where server points to it when serving
request it also contains (index.php)
- resources/ --> contains (views - Saas -Less files - ..)
- routes/ --> all routes definitions (Api - console -http)
9. Directory Structure
- storage/ --> where cache logs compiled files exist
- tests/ --> where unit & integration test exist
- vendor/ --> composer packages
- .env --> defines environments variables
- .env.example --> same as above ,it should be copied when
cloning other projects
- .gitattributes , .gitignore --> git configuration files
- artisan --> php script to run artisan commands
10. Directory Structure
- composer.json , composer.lock --> contains project
dependencies
- package.json --> like composer.json for frontend assets
- phpunit.xml --> configuration for php unit
- readme.md --> markdown for laravel introduction
- server.php --> it’s a server that emulate apache
mod_rewrite
- webpack.mix.js --> used for compiling and mixing frontend
assets
13. Blade
- Blade is inspired by .NET’s Razor engine .
- echo data :- {{ $post }} instead of <?php echo $post ; ?>
- conditions :-
@if ($post->name == ‘firstPost’)
// do some stuff
@endif
14. Blade
- looping :-
@foreach($posts as $post) //do some stuff @endforeach
- inheritance :- @exnteds(‘layouts.master’)
- define sections :-
@section(‘content’)
<h1> hello from the content
@endsection
15. Blade
- printing sections :- @yield(‘content’)
- including :- @include(‘scripts’)
- Including with parameters :-
@include(‘post.form’,[‘method’ => ‘POST’])
more at https://laravel.com/docs/master/blade#introduction
16. Database & Eloquent
- Laravel ORM is called Eloquent
- Database configuration in .env file or from
config/database.php.
- Laravel migration helps in making database persistent across
multiple machines .
- DB facade used to form query builder object
17. Database & Eloquent
- $post = DB::table(‘posts’)->find(20); //finds a row with id =
20
- $posts = DB::table('posts')->get(); //get all rows in posts table
- $singlePost = DB::table(‘posts’)->where(‘name’ ,
‘FirstPost’)->get(); //where conditions to query
- $firstPost = DB::table(‘posts’)->first(); //gets the first row
18. Database & Eloquent
- DB::table(‘posts’)->insert(
[‘title’ => ‘first post title’ , ‘desc’ => ‘first post desc ‘]
); //inserting a row
- DB::table('posts')->where(‘id’ , 1)->delete(); //deletes a row
- DB::table(‘posts’)->where(‘id’ , 1)
->update( [ ‘title’ => ‘changed title post ] );
//update the post title only
19. Database & Eloquent
- php artisan make:model Post //create a new model class
- Laravel by default gets the plural name of model as a table
name and makes query based on that
- Post::all() //will search in posts table and get all rows
- Post::create([‘title’ => ‘first post’ , ‘desc’ => ‘desc post’);
//this will give a MassAssignmentException unless you
override $fillable
20. Database & Eloquent
- Post::find(25)->update([‘title’ => ‘update post title’)
//updates title for post with id 25
- Post::where(‘votes’, 23)->delete()
//deletes any post have votes with 23
21. Database & Eloquent
- To define a relation in Post model you will define a function in
the class for example i want to say post have many sections .
//in Post model class
public function sections ()
{ return $this->hasMany(Section::class);}
// in controller for example
$postSections = Post::find(23)->sections;
22. Database & Eloquent
- Remember that query results are collection objects
- More at Eloquent
https://laravel.com/docs/master/eloquent#introduction
- More at Collections
https://laravel.com/docs/master/collections#available-meth
ods
23. Request Validation
- Request Life Cycle :-
See index.php first it loads the composers’s autload file .
Then we bootstrap laravel application container and register
some basic service providers like Log Service provider
look at Illuminate/Foundation/Application.php constructor
method .
Finally we create instance of kernel to register providers and
take user request and process it through middlewares &
handle exception then return response
24. Request Validation
- In Controller :-
$this->validate( $request , [
‘title ‘ => ‘required ‘,
‘desc’ => ‘required|max:255’
] ,
[
‘title.required’ => ‘title is required to be filled ‘ ,
‘desc.max’ => ‘description max num of chars is 255 ‘
]);
25. Request Validation
- Another way with request file :-
php artisan make:request PostsStoreRequest
Then in authorize method make it return true .
After that define your rules in rules method .
- validation rules :-
https://laravel.com/docs/master/validation#available-validation-rules
- custom validation messages in request file :-
https://laravel.com/docs/master/validation#customizing-the-error-messages
26. Middleware
- It’s a series of layers around the application , every request
passes through middlewares .
- Middlewares can inspect the request to decorate or reject it
- Also middlewares can decorate the response .
- register the middlewares in app/Http/Kernel.php
- handle($request , ..) method where you handle the request and
choose to pass it for the next middleware or not .
28. Service Container
- Other names for service container
( IOC container , DI container , Application container )
- Dependency Injection (DI) :-
Instead to make object instantiate it’s dependencies
internally , it will be passed from outside .
29. Service Container
- Class User {
function __construct ()
{
$mysqlConnection = new Mysql();
}
}
$user = new User(); // in this way object instantiated it’s
dependencies internally
30. Service Container
- Class User {
protected $dbConnection;
//DB is an interface to easily switch between different db drivers
function __construct (DB $db)
{
$this->dbConnection = $db;
}
}
$mysqlConnection = new Mysql();
$user = new User( $mysqlConnection ); // the user object dependencies
are passed from outside .
31. Service Container
- Service container in laravel responsible for binding &
resolving & AutoWiring
- Try this in controller :-
public function index (Request $request)
{
dd($request);
}
// so how does the $request prints an object without instantiating it !!?
32. Service Container
- In the previous example , the illuminate container see if the
class or method needs a dependencies it will make
AutoWiring
- AutoWiring :- means if the class or method have
dependencies type hinted , then it will use reflection to get
the type hinted instance .
- what is reflection :-
http://php.net/manual/en/book.reflection.php
33. Service Container
- Binding
app()->bind( Car::class , function() {
return new LamboCar();
}
);
- Resolving
app(Car::class) // means when someone asks for instance
from Car::class it will return new LamboCar instance .
34. Service Container
- Service Providers :-
This is the class where you bind services to the laravel app.
- php artisan make:provider TestProvider
- register() :- this is where you bind things into container
- boot() :- called after all register methods in other providers
have been called . //this is where you register events listeners
or routes and do any behaviour .
35. Service Container
- In config/app.php see the providers array .
- Now try to play around with this package :-
https://github.com/mcamara/laravel-localization
36. Thank You
- These slides are based on
Laravel Up & Running Book
http://shop.oreilly.com/product/0636920044116.do
Taylor Otwel Talk At Laracon Online March-2017
Matt Stuffer Talk At Laracon Online March-2017
Laravel Documentation
https://laravel.com/docs/master
37. Author
Ahmed Mohamed Abd El Ftah Intake 36 graduate
- Gmail :- ahmedabdelftah95165@gmail.com
- LinkedIn :- https://goo.gl/b5j8aS