Module 5
Module 5
JSON Data Interchange Format: Syntax, Data Types, Object, JSON Schema, Manipulating
JSON data with PHP
• Fourth name-value pair has the name “required” and a value of the array
data type. The array includes the fields we require.
• Modularity
Laravel provides 20 built in libraries and modules which
helps in enhancement of the application. Every module is
integrated with Composer dependency manager which
eases updates.
• Testability
Laravel includes features and helpers which helps in
testing through various test cases. This feature helps in
maintaining the code as per the requirements.
• Configuration Management
A web application designed in Laravel will be running on different
environments, which means that there will be a constant change in its
configuration. Laravel provides a consistent approach to handle the
configuration in an efficient way.
• Query Builder and ORM
Laravel incorporates a query builder which helps in querying databases
using various simple chain methods. It provides ORM (Object Relational
Mapper) and ActiveRecord implementation called Eloquent.
• Schema Builder
Schema Builder maintains the database definitions and schema in PHP
code. It also maintains a track of changes with respect to database
migrations.
• Template Engine
Laravel uses the Blade Template engine, a lightweight template
language used to design hierarchical blocks and layouts with
predefined blocks that include dynamic content.
• E-mail
Laravel includes a mail class which helps in sending mail with rich
content and attachments from the web application.
• Authentication
User authentication is a common feature in web applications.
Laravel eases designing authentication as it includes features such
as register, forgot password and send password reminders.
• Redis
Laravel uses Redis to connect to an existing session and general-
purpose cache. Redis interacts with session directly.
• Queues
Laravel includes queue services like emailing large number of users
or a specified Cron job. These queues help in completing tasks in an
easier manner without waiting for the previous task to be
completed.
• Event and Command Bus
Laravel 5.1 includes Command Bus which helps in executing
commands and dispatch events in a simple way. The commands in
Laravel act as per the application’s lifecycle.
Setting up a Laravel Development
Environment
• The Laravel framework has a few system requirements. All of these requirements are satisfied by
the Laravel Homestead virtual machine, so it's highly recommended that you use Homestead as
your local Laravel development environment.
• However, if you are not using Homestead, you will need to make sure your server meets the
following requirements:
• PHP >= 7.2.5
• BCMath PHP Extension
• Ctype PHP Extension
• Fileinfo PHP extension
• JSON PHP Extension
• Mbstring PHP Extension
• OpenSSL PHP Extension
• PDO PHP Extension
• Tokenizer PHP Extension
• XML PHP Extension
• For managing dependencies, Laravel uses composer. Make sure
you have a Composer installed on your system before you install
Laravel. In this chapter, you will see the installation process of
Laravel.
• You will have to follow the steps given below for installing Laravel
onto your system −
• Step 1 − Visit the following URL and download composer to
install it on your system.
• https://getcomposer.org/download/
• Step 2 − After the Composer is installed, check the installation
by typing the Composer command in the command prompt as
shown in the following screenshot.
• Installing Laravel with the Laravel Installer Tool If you have Composer
installed globally, installing the Laravel installer tool is as sim‐ ple as
running the following command:
composer global require "laravel/installer=~1.1“
Once you have the Laravel installer tool installed, spinning up a new
Laravel project is simple.
Just run this command from your command line:
laravel new projectName
This will create a new subdirectory of your current directory named
projectName and install a bare Laravel project in it.
• Installing Laravel with Composer’s create-project Feature
• Composer also offers a feature called create-project for creating new
projects with a particular skeleton. To use this tool to create a new
Laravel project, issue the follow‐ ing command:
• composer create-project laravel/laravel projectName --prefer-dist
• Just like the installer tool, this will create a subdirectory of your
current directory named projectName that contains a skeleton Laravel
install, ready for you to develop
Start the Laravel service by executing the following command.
php artisan serve
--prefer-dist would try to download and unzip archives of the dependencies using GitHub
or another API when available. This is used for faster downloading of dependencies in most
cases.
• Copy the URL underlined in gray in the above
screenshot and open that URL in the browser. If you see
the following screen, it implies Laravel has been
installed successfully.
The root directory contains the following folders by default:
• app is where the bulk of your actual application will go. Models,
controllers, route definitions, commands, and your PHP domain code all
go in here.
• bootstrap contains the files that the Laravel framework uses to boot
every time it runs.
• config where all the configuration files live.
• database is where database migrations and seeds live.
• public is the directory the server points to when it’s serving the website.
This con‐ tains index.php, which is the front controller that kicks off the
bootstrapping pro‐ cess and routes all requests appropriately. It’s also where
any public-facing files like images, stylesheets, scripts, or downloads go.
• resources is where non-PHP files that are needed for other scripts live.
Views, lan‐ guage files, and (optionally) Sass/LESS and source JavaScript files
live here.
• routes is where all of the route definitions live, both for HTTP routes
and “con‐ sole routes,” or Artisan commands.
• storage is where caches, logs, and compiled system files live.
• tests is where unit and integration tests live.
• vendor is where Composer installs its dependencies. It’s Git-ignored
(marked to be excluded from your version control system), as
Composer is expected to run as a part of your deploy process on any
remote servers.
• server.php is a backup server that tries to allow less-capable servers
to still pre‐ view the Laravel application.
MVC Architecture Of Laravel
Laravel also allows you to name each route, which enables you to refer to it without explicitly referencing the
URL. This is helpful because it means you can give simple nicknames to complex routes.
Route Groups
• Route groups allow you to group several routes together, and apply
any shared con‐ figuration settings once to the entire group, to reduce
this duplication. Additionally, route groups are visual cues to future
developers (and to your own brain) that these routes are grouped
together
Middleware
• Laravel uses for authenticating users and restricting guest users from using
certain parts of a site.
• Middleware acts as a bridge between a request and a
response. It is a type of filtering mechanism.
• Laravel includes a middleware that verifies whether the user
of the application is authenticated or not. If the user is
authenticated, it redirects to the home page otherwise, if
not, it redirects to the login page.
• Most common use for route groups is to apply middleware to a group of
routes
• Middleware can inspect a request and decorate it, or reject it, based
on what it finds. That means middleware is great for something like
rate limiting: it can inspect the IP address, check how many times it’s
accessed this resource in the last minute, and send back a 429 (Too
Many Requests) status if a threshold is passed.
• Because middleware also gets access to the response on its way out
of the application, it’s great for decorating responses
Creating Custom Middleware
• handle is the method that handles the Request in the given
middleware
• To pass the request deeper into the application (allowing the
middleware to "pass"), simply call the $next callback with the
$request.
Binding/Registering Middleware
• We need to register each and every middleware before using it. There are
two types of Middleware in Laravel.
• Global Middleware
• Route Middleware
• The Global Middleware will run on every HTTP request of the
application, whereas the Route Middleware will be assigned to a
specific route.
• The middleware can be registered at app/Http/Kernel.php.
• This file contains two properties $middleware and $routeMiddleware.
• $middleware property is used to register Global Middleware
and $routeMiddleware property is used to register route specific
middleware.
Binding route middleware
• Middleware intended for specific routes can be added as a route
middleware or as part of a middleware group
• Route middleware are added to the $routeMiddleware array in
app/Http/Kernel.php
• we have to give each a key that will be used when applying this
middleware to a particular route
• We can now use this middleware in our route definitions
Middleware Parameters