What is Laravel ?
What is ?
• Laravel is MVC PHP framework created by Taylor Otwell
in 2011
• Free open-source license with many contributors
worldwide
• Has powerful features, saving us time
• Uses Symfony packages
Features
• Eloquent ORM (object-relational mapping) – implements
ActiveRecord
• Query builder – helps you to build secured SQL queries
• Restful controllers – provides a way for separating the
different HTTP requests (GET, POST, DELETE, etc.)
• Blade template engine – combines templates with a data
model to produce views
• Migrations – version control system for database, update your
database easier
• Database seeding – provides a way to populate database
tables with test data used for testing
• Pagination – easy to use advanced pagination functionalities
• Forms security – provides CSRF token middleware, protecting
all the forms
Let’s install Laravel
• Laravel uses Composer to manage its
dependencies
• Composer is dependency management tool for
PHP, like a library full of books
• NOT like Yum or apt
• Per project tool (vendor folder), not per system
The structure
app/Http folder contains the
Controllers, Middlewares and
Kernel file
All the models should be located in
app/Models folder
The service providers that are
bootstrapping functions in our app are
located in app/Providers folder
All the config files are located in
app/config folder
Database folder contains the
migrations and seeds
The public folder is the actual folder
you are opening on the web server.
All JS / CSS / Images / Uploads are
located there.
The resources folder contains all the
translations, views and assets
(SASS, LESS, JS)
that are compiled into public folder
The routes folder contains all the
routes for the project
All the logs / cache files are located in
storage folder
The vendor folder contains all the
composer packages (dependencies)
Laravel Request Cycle
Artisan !
• Artisan is command-line interface
for Laravel
• Commands that are saving time
• Generating files with artisan is
recommended
• Run php artisan list in the console
Middleware
• The middleware is mechanism
for filtering the HTTP requests
• Laravel includes several
middlewares – Authentication,
CSRF Protection
• The auth middleware checks if
the user visting the page is
authenticated through session
cookie
• The CSRF token protection
middleware protects your
application from cross-site
request forgery attacks by
adding token key for each
generated form
•
Blade
• Blade is the powerful template engine provided by
Laravel
• All the code inside blade file is compiled to static
html file
• Supports plain PHP
• Saves time
• Better components mobility, extend and include
partials
Eloquent & Database
• The Eloquent ORM (Object-relational mapping) provides simple ActiveRecord
implementation for working with the database
$article = new Article;
$article->title = ‘Article title’;
$article->description =
‘Description’;
$article->save();
INSERT INTO `article` (`title`, `description`) VALUES (‘Article title’,
‘Description’);
• Each table has its own “Model”. You can use the model to read, insert, update
or delete row from the specific table
• Let’s check one model
Forms security
Always use the CSRF token protection that Laravel provides in forms you
create, the hackers will not be able to spam your forms and database