- Laravel is a popular PHP MVC framework that provides tools like Eloquent ORM, Blade templating, routing, and Artisan CLI to help developers build applications faster.
- Key Laravel features include Eloquent for database access, Blade templating engine, routing system, middleware, and Artisan CLI commands for common tasks like migrations and seeding.
- The document discusses Laravel's file structure, installing via Composer, and provides best practices for coding with Laravel like avoiding large queries and using middleware, validation, and CSRF protection.
Today, we will cover Laravel, its installation, file structure, artisan, routing, middleware, blade, Eloquent ORM, CRUD, and best coding practices.
Laravel is an MVC PHP framework developed in 2011, known for its powerful features like Eloquent ORM, migrations, and Blade template engine.
Laravel dependencies are managed via Composer, using a project-based tool for installing Laravel.
Laravel's file structure includes essential directories: Controllers, Models, configuration files, migrations, public assets, and routes.
Artisan is a command-line tool for Laravel that aids in generating files and optimizing workflow.
Laravel offers a robust routing system with middleware support and request methods. Importance of naming routes and order.
Middleware filters HTTP requests. Key middleware include Authentication and CSRF protection mechanisms.
Blade is Laravel's template engine, converting code into static HTML, supporting PHP code, and enhancing layout management.
Eloquent ORM allows simple database operations via model classes, providing an integrated Active Record approach.
Engagement in a practical task related to CRUD operations in Laravel.
Best practices include using models appropriately, ensuring CSRF protection, database design considerations, efficient queries, and maintaining code comments.
We will learntoday
• WHAT IS LARAVEL
• INSTALL LARAVEL 5 WITH COMPOSER
• FILES STRUCTURE
• WHAT IS ARTISAN AND HOW DOES IT SAVE US TIME
• ROUTING AND ROUTE TYPES
• WHAT IS MIDDLEWARE AND HOW TO USE IT
• WHAT IS BLADE ?
• DATABASE AND ELOQUENT ORM
• CRUD WITH VALIDATION AND DATABASE
CONNECTION (PRACTICAL TASK)
• BEST PRACTICES WHEN CODING IN LARAVEL
3.
• Laravel isMVC PHP framework created by Taylor Otwell in
2011
• Free open-source license with many contributors worldwide
• One of the best frameworks together with Symfony,
CodeIgniter, Yii
• Has powerful features, saving us time
• Uses Symfony packages
What is
5.
• Eloquent ORM(object-relational mapping) – implements Active-
Record
• 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
Features of
6.
• Laravel usesComposer 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
• Install by using the command:
composer create-project [PACKAGE] [DESTINATION PATH] [--FLAGS]
composer create-project laravel/laravel Laravel test
Let’s install
7.
app/Http folder containsthe Controllers, Middlewares and Kernel file
All the models should be located in app/Models folder
All the config files are located in app/config folder
The service providers that are bootstrapping functions in our
app are located in app/Providers folder
Folder Structure
8.
Folder Structure
• Databasefolder 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)
9.
Artisan is command-lineinterface for Laravel
Commands that are saving time
Generating files with artisan is recommended
Run php artisan list in the console
Artisan!
10.
• The bestand easy routing system I’ve seen
• Routing per middleware / prefix or namespace
• Routing per request method (GET, POST,
DELETE, etc.)
• ALWAYS name your route !
• Be careful with the routing order !
Let’s see routing examples
Routing
11.
• The middlewareis mechanism for filtering the HTTP
requests
• Laravel includes several middleware's – Authentication,
CSRF Protection
• The auth middleware checks if the user visiting 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
Let’s create middleware
Middleware
12.
• Blade isthe 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
Let’s take a look at few examples
Blade
13.
ELOQUENT & DATABASE
•THE ELOQUENT ORM (OBJECT-RELATIONAL MAPPING) PROVIDES SIMPLE ACTIVE RECORD
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’);
14.
• EACH TABLEHAS ITS OWN “MODEL”. YOU CAN USE THE MODEL TO READ, INSERT, UPDATE OR
DELETE ROW FROM THE SPECIFIC TABLE
• LET’S CHECK ONE MODEL
Best practices in
•NEVER write queries or model logic inside
the controller! The controller job is to
communicate with the model and pass data to
the view.
17.
Extend and includepartials. For example share
the same form fields on 2 pages – add and edit
Views mobility
18.
Always use theCSRF token protection that Laravel provides in forms you create, the hackers will not
be able to spam your forms and database
Forms security
19.
Be careful withthe database architecture, always use the proper length for specific column and never
forget the indexes for searchable columns
Database architecture
20.
• Avoid thebig query unless you really have
to do it. The big query is hard to debug
and understand.
• You can merge the small queries into one
to save the CPU time on server, but
sometimes the query becomes way too
big.
Big Query
21.
Don’t forget towrite comments for each
method or complicated logic.
The PHPDoc comments are helping the
IDE to autosuggest easier and the
developers to understand the piece of code
Don’t forget the PHPDoc