SlideShare a Scribd company logo
Object-Oriented
Programming
(with Laravel)
By: Shahrzad Peyman
Session 4
May-2017
shahrzad.peymaan@gmail.com
1
2
Today’s Presentation
• CSRF Protection
• HTTP Request
• PSR-7 Request
• HTTP Response
3
CSRF Protection
Laravel makes it easy to protect your
application from cross-site request forgery
(CSRF) attacks.
Laravel automatically generates a CSRF
"token" for each active user session managed
by the application. This token is used to verify
that the authenticated user is the one actually
making the requests to the application.
CSRF Protection
4
Anytime you define a HTML form in your
application, you should include a hidden CSRF
token field in the form so that the CSRF
protection middleware can validate the request.
Excluding URIs from CSRF
Protection
5
Sometimes you may wish to
exclude a set of URIs from CSRF
protection.
Typically, you should place these
kinds of routes outside of the web
middleware group that the
RouteServiceProvider applies to
all routes in the routes/web.php
file. However, you may also
exclude the routes by adding their
URIs to the $expect property of
the verifyCSRFToken middleware:
6
X-CSRF-TOKEN
In addition to checking for the CSRF token as a
POST parameter, the verifyCsrfToken middleware
will also check for the X-CSRF-TOKEN request
header. You could, for example, store the token in
a HTML meta tag:
X-CSRF-TOKEN
Then, once you have created the meta tag, you
can instruct a library like jQuery to automatically
add the token to all request headers. This
provides simple, convenient CSRF protection for
your AJAX based applications:
7
HTTP Requests
To obtain an instance of
the current HTTP request
via dependency injection,
you should type-hint the
IlluminateHTTPRequest
class on your controller
method.
8
Request & Route Parameters
9
If your controller method is also
expecting input from a route
parameter you should list your
route parameters after your other
dependencies. For example, if
your route is defined like so:
Accessing the Request via
Route Closures
10
Request Path & Methods
The IlluminateHTTPRequest instance provides a variety of
methods for examining the HTTP request for your application.
We will discuss a few of the most important methods below.
The path method returns the request's path information. So, if
the incoming request is targeted at http://domain.com/foo/bar,
the path method will return foo/bar:
Retrieving the Request URL
To retrieve the full URL for the incoming
request you may use the url or fullUrl methods.
Retrieving the Request
Method
The method method will return the HTTP verb
for the request. You may use the isMethod
method to verify that the HTTP verb matches a
given string:
PSR-7 Request
The PSR-7 standard specifies interfaces for HTTP
messages, including requests and responses. If you
would like to obtain an instance of a PSR-7 request
instead of a Laravel request, you will first need to install
a few libraries.
Laravel uses the Symfony HTTP Message Bridge
component to convert typical Laravel requests and
responses into PSR-7 compatible implementations:
14
PSR-7 Request
15
Once you have installed these libraries, you may
obtain a PSR-7 request by type-hinting the request
interface on your route Closure or controller
method:
Retrieving Input
16
You may also retrieve all of the input data as
an array using the all method:
Retrieving the portion of
Input Data
17
Determine If an Input Value
is Present
18
You should use the has method to determine if a value
is present on the request. The has method returns true
if the value is present and is not an empty string:
19
Old Input
Laravel allows you to keep input from one request during the next
request. This feature is particularly useful for re-populating forms after
detecting validation errors. However, if you are using Laravel's included
validation features, it is unlikely you will need to manually use these
methods, as some of Laravel's built-in validation facilities will call them
automatically.
The flash method on the IlluminateHttpRequest class will flash the
current input to the session so that it is available during the user's next
request to the application:
20
Flashing Input then
Redirecting
Since you often will want to flash input to the
session and then redirect to the previous page, you
may easily chain input flashing onto a redirect
using the withInput method:
21
Retrieving Old Input
To retrieve flashed input from the previous request,
use the old method on the Request instance. The old
method will pull the previously flashed input data
from the session:
22
Files
You may access uploaded files from a IlluminateHttpRequest
instance using the file method or using dynamic properties. The file
method returns an instance of the IlluminateHttpUploaded File
class.
Validating Successful
Uploads
23
In addition to checking if the file is present, you
may verify that there were no problems
uploading the file via the isValid method:
24
File Path & Extensions
The uploadedFile class also contains methods for
accessing the file's fully-qualified path and its extension.
The extension method will attempt to guess the file's
extension based on its contents. This extension may be
different from the extension that was supplied by the client:
24
25
File Path & Extensions
To store an uploaded file, you will typically use one of your
configured filesystems. The UploadedFile class has a store
method which will move an uploaded file to one of your
disks, which may be a location on your local filesystem or
even a cloud storage location like Amazon S3.
25
26
HTTP Responses
All routes and controllers should return a response to be sent
back to the user's browser.
Laravel provides several different ways to return responses.
The most basic response is simply returning a string from a
route or controller. The framework will automatically convert
the string into a full HTTP response:
27
Response Objects
Typically, you won't just be returning simple strings
or arrays from your route actions. Instead, you will
be returning full IlluminateHttpResponse instances
or views.
28
Redirects
Redirects are instances of the IlluminateHttpRedirectResponse
class, and contain the proper headers needed to redirect the user
to another URL. There are several ways to generate a
RedirectResponse instance. The simplest method is to use the
global redirect helper:
29
Redirecting to Named
Routes
Redirecting to Controller
Actions
30
31
Other Response Types
The response helper may be used to generate other types of
response instances.
If you need control over the response's status and headers but
also need to return a view as the response's content, you should
use the view method:
32
File Downloads
The download method may be used to generate a response that
forces the user's browser to download the file at the given path.
The download method accepts a file name as the second
argument to the method, which will determine the file name that
is seen by the user downloading the file. Finally, you may pass
an array of HTTP headers as the third argument to the method:

More Related Content

PDF
Object Oriented Programming with Laravel - Session 5
PDF
Object Oriented Programming with Laravel - Session 6
PDF
Object Oriented Programming with Laravel - Session 2
PPTX
Sql Injection attacks and prevention
DOCX
Asynchronous reading and writing http r equest
PPT
SQL injection basics
PPT
Entity frameworks101
PPT
SQL Injection Attacks
Object Oriented Programming with Laravel - Session 5
Object Oriented Programming with Laravel - Session 6
Object Oriented Programming with Laravel - Session 2
Sql Injection attacks and prevention
Asynchronous reading and writing http r equest
SQL injection basics
Entity frameworks101
SQL Injection Attacks

What's hot (20)

PDF
SQL Injection Attack Detection and Prevention Techniques to Secure Web-Site
PPT
Sql injection attacks
PPTX
Web application penetration using SQLMAP.
PPTX
Mule batch processing
PDF
Oracle Concurrent Program Setup document
PPTX
ApacheCon North America 2018: Creating Spark Data Sources
PPT
D:\Technical\Ppt\Sql Injection
PPS
Manindra kishore _incident_handling_n_log_analysis - ClubHack2009
PPTX
Sql injection
PPT
Sql Injection Attacks Siddhesh
DOC
Asp.Net Database
PPT
SQL Injection
PPTX
SQL Injections - A Powerpoint Presentation
PDF
Assignment 2 - Power drill Grapevine "It's like Yik Yak, but for opinions ab...
PPT
Rail3 intro 29th_sep_surendran
PDF
Poster Declaratively Describing Responses of Hypermedia-Driven Web APIs
PPTX
SQL Injection Defense in Python
PPTX
cakephp UDUYKTHA (1)
PPTX
Using SP Metal for faster share point development
PPTX
QA Lab: тестирование ПО. Владимир Гарбуз: "Application Security 101"
SQL Injection Attack Detection and Prevention Techniques to Secure Web-Site
Sql injection attacks
Web application penetration using SQLMAP.
Mule batch processing
Oracle Concurrent Program Setup document
ApacheCon North America 2018: Creating Spark Data Sources
D:\Technical\Ppt\Sql Injection
Manindra kishore _incident_handling_n_log_analysis - ClubHack2009
Sql injection
Sql Injection Attacks Siddhesh
Asp.Net Database
SQL Injection
SQL Injections - A Powerpoint Presentation
Assignment 2 - Power drill Grapevine "It's like Yik Yak, but for opinions ab...
Rail3 intro 29th_sep_surendran
Poster Declaratively Describing Responses of Hypermedia-Driven Web APIs
SQL Injection Defense in Python
cakephp UDUYKTHA (1)
Using SP Metal for faster share point development
QA Lab: тестирование ПО. Владимир Гарбуз: "Application Security 101"
Ad

Similar to Object Oriented Programming with Laravel - Session 4 (20)

PPTX
Laravel 5
PPTX
Slim Framework
PDF
Lunacloud's Compute RESTful API - Programmer's Guide
PDF
laravel-interview-questions.pdf
PPT
An Introduction To Java Web Technology
PDF
MidwestPHP 2016 - Adventures in Laravel 5
PPT
Web Tech Java Servlet Update1
PPTX
Rest and Sling Resolution
PPTX
Java Servlets
PPTX
SW Security Lec4 Securing architecture.pptx
PPTX
Intro to flask
PPTX
Intro to flask2
PPT
Anintroductiontojavawebtechnology 090324184240-phpapp01
PPTX
Laravel development (Laravel History, Environment Setup & Laravel Installatio...
PDF
Web Component Development Using Servlet & JSP Technologies (EE6) - Chapter 4...
PPTX
SPFx Webinar Loading SharePoint data in a SPFx Webpart
PDF
Xamarin Workshop Noob to Master – Week 5
PDF
Web services with laravel
PDF
Building Restful Applications Using Php
PPTX
Study-Guide 3 let Routing-in-Laravel.pptx
Laravel 5
Slim Framework
Lunacloud's Compute RESTful API - Programmer's Guide
laravel-interview-questions.pdf
An Introduction To Java Web Technology
MidwestPHP 2016 - Adventures in Laravel 5
Web Tech Java Servlet Update1
Rest and Sling Resolution
Java Servlets
SW Security Lec4 Securing architecture.pptx
Intro to flask
Intro to flask2
Anintroductiontojavawebtechnology 090324184240-phpapp01
Laravel development (Laravel History, Environment Setup & Laravel Installatio...
Web Component Development Using Servlet & JSP Technologies (EE6) - Chapter 4...
SPFx Webinar Loading SharePoint data in a SPFx Webpart
Xamarin Workshop Noob to Master – Week 5
Web services with laravel
Building Restful Applications Using Php
Study-Guide 3 let Routing-in-Laravel.pptx
Ad

More from Shahrzad Peyman (10)

PDF
Web Design & Development - Session 9
PDF
Web Design & Development - Session 8
PDF
Web Design & Development - Session 7
PDF
Web Design & Development - Session 6
PDF
Web Design & Development - Session 4
PDF
Web Design & Development - Session 3
PDF
Web Design & Development - Session 2
PDF
Web Design & Development - Session 1
PDF
Object Oriented Programming with Laravel - Session 3
PDF
Object Oriented Programming with Laravel - Session 1
Web Design & Development - Session 9
Web Design & Development - Session 8
Web Design & Development - Session 7
Web Design & Development - Session 6
Web Design & Development - Session 4
Web Design & Development - Session 3
Web Design & Development - Session 2
Web Design & Development - Session 1
Object Oriented Programming with Laravel - Session 3
Object Oriented Programming with Laravel - Session 1

Recently uploaded (20)

PDF
The Role of Automation and AI in EHS Management for Data Centers.pdf
PPTX
Benefits of DCCM for Genesys Contact Center
PDF
AI in Product Development-omnex systems
PDF
Convert Thunderbird to Outlook into bulk
PDF
Perfecting Gamer’s Experiences with Performance Testing for Gaming Applicatio...
PDF
Become an Agentblazer Champion Challenge
PPT
JAVA ppt tutorial basics to learn java programming
PDF
Best Practices for Rolling Out Competency Management Software.pdf
PDF
How to Confidently Manage Project Budgets
PDF
Multi-factor Authentication (MFA) requirement for Microsoft 365 Admin Center_...
DOCX
The Five Best AI Cover Tools in 2025.docx
PDF
A REACT POMODORO TIMER WEB APPLICATION.pdf
PPTX
AIRLINE PRICE API | FLIGHT API COST |
PPTX
Save Business Costs with CRM Software for Insurance Agents
PPTX
Online Work Permit System for Fast Permit Processing
PPTX
Materi_Pemrograman_Komputer-Looping.pptx
PDF
top salesforce developer skills in 2025.pdf
PDF
Claude Code: Everyone is a 10x Developer - A Comprehensive AI-Powered CLI Tool
PPTX
CRUISE TICKETING SYSTEM | CRUISE RESERVATION SOFTWARE
PDF
Comprehensive Salesforce Implementation Services.pdf
The Role of Automation and AI in EHS Management for Data Centers.pdf
Benefits of DCCM for Genesys Contact Center
AI in Product Development-omnex systems
Convert Thunderbird to Outlook into bulk
Perfecting Gamer’s Experiences with Performance Testing for Gaming Applicatio...
Become an Agentblazer Champion Challenge
JAVA ppt tutorial basics to learn java programming
Best Practices for Rolling Out Competency Management Software.pdf
How to Confidently Manage Project Budgets
Multi-factor Authentication (MFA) requirement for Microsoft 365 Admin Center_...
The Five Best AI Cover Tools in 2025.docx
A REACT POMODORO TIMER WEB APPLICATION.pdf
AIRLINE PRICE API | FLIGHT API COST |
Save Business Costs with CRM Software for Insurance Agents
Online Work Permit System for Fast Permit Processing
Materi_Pemrograman_Komputer-Looping.pptx
top salesforce developer skills in 2025.pdf
Claude Code: Everyone is a 10x Developer - A Comprehensive AI-Powered CLI Tool
CRUISE TICKETING SYSTEM | CRUISE RESERVATION SOFTWARE
Comprehensive Salesforce Implementation Services.pdf

Object Oriented Programming with Laravel - Session 4

  • 2. 2 Today’s Presentation • CSRF Protection • HTTP Request • PSR-7 Request • HTTP Response
  • 3. 3 CSRF Protection Laravel makes it easy to protect your application from cross-site request forgery (CSRF) attacks. Laravel automatically generates a CSRF "token" for each active user session managed by the application. This token is used to verify that the authenticated user is the one actually making the requests to the application.
  • 4. CSRF Protection 4 Anytime you define a HTML form in your application, you should include a hidden CSRF token field in the form so that the CSRF protection middleware can validate the request.
  • 5. Excluding URIs from CSRF Protection 5 Sometimes you may wish to exclude a set of URIs from CSRF protection. Typically, you should place these kinds of routes outside of the web middleware group that the RouteServiceProvider applies to all routes in the routes/web.php file. However, you may also exclude the routes by adding their URIs to the $expect property of the verifyCSRFToken middleware:
  • 6. 6 X-CSRF-TOKEN In addition to checking for the CSRF token as a POST parameter, the verifyCsrfToken middleware will also check for the X-CSRF-TOKEN request header. You could, for example, store the token in a HTML meta tag:
  • 7. X-CSRF-TOKEN Then, once you have created the meta tag, you can instruct a library like jQuery to automatically add the token to all request headers. This provides simple, convenient CSRF protection for your AJAX based applications: 7
  • 8. HTTP Requests To obtain an instance of the current HTTP request via dependency injection, you should type-hint the IlluminateHTTPRequest class on your controller method. 8
  • 9. Request & Route Parameters 9 If your controller method is also expecting input from a route parameter you should list your route parameters after your other dependencies. For example, if your route is defined like so:
  • 10. Accessing the Request via Route Closures 10
  • 11. Request Path & Methods The IlluminateHTTPRequest instance provides a variety of methods for examining the HTTP request for your application. We will discuss a few of the most important methods below. The path method returns the request's path information. So, if the incoming request is targeted at http://domain.com/foo/bar, the path method will return foo/bar:
  • 12. Retrieving the Request URL To retrieve the full URL for the incoming request you may use the url or fullUrl methods.
  • 13. Retrieving the Request Method The method method will return the HTTP verb for the request. You may use the isMethod method to verify that the HTTP verb matches a given string:
  • 14. PSR-7 Request The PSR-7 standard specifies interfaces for HTTP messages, including requests and responses. If you would like to obtain an instance of a PSR-7 request instead of a Laravel request, you will first need to install a few libraries. Laravel uses the Symfony HTTP Message Bridge component to convert typical Laravel requests and responses into PSR-7 compatible implementations: 14
  • 15. PSR-7 Request 15 Once you have installed these libraries, you may obtain a PSR-7 request by type-hinting the request interface on your route Closure or controller method:
  • 16. Retrieving Input 16 You may also retrieve all of the input data as an array using the all method:
  • 17. Retrieving the portion of Input Data 17
  • 18. Determine If an Input Value is Present 18 You should use the has method to determine if a value is present on the request. The has method returns true if the value is present and is not an empty string:
  • 19. 19 Old Input Laravel allows you to keep input from one request during the next request. This feature is particularly useful for re-populating forms after detecting validation errors. However, if you are using Laravel's included validation features, it is unlikely you will need to manually use these methods, as some of Laravel's built-in validation facilities will call them automatically. The flash method on the IlluminateHttpRequest class will flash the current input to the session so that it is available during the user's next request to the application:
  • 20. 20 Flashing Input then Redirecting Since you often will want to flash input to the session and then redirect to the previous page, you may easily chain input flashing onto a redirect using the withInput method:
  • 21. 21 Retrieving Old Input To retrieve flashed input from the previous request, use the old method on the Request instance. The old method will pull the previously flashed input data from the session:
  • 22. 22 Files You may access uploaded files from a IlluminateHttpRequest instance using the file method or using dynamic properties. The file method returns an instance of the IlluminateHttpUploaded File class.
  • 23. Validating Successful Uploads 23 In addition to checking if the file is present, you may verify that there were no problems uploading the file via the isValid method:
  • 24. 24 File Path & Extensions The uploadedFile class also contains methods for accessing the file's fully-qualified path and its extension. The extension method will attempt to guess the file's extension based on its contents. This extension may be different from the extension that was supplied by the client: 24
  • 25. 25 File Path & Extensions To store an uploaded file, you will typically use one of your configured filesystems. The UploadedFile class has a store method which will move an uploaded file to one of your disks, which may be a location on your local filesystem or even a cloud storage location like Amazon S3. 25
  • 26. 26 HTTP Responses All routes and controllers should return a response to be sent back to the user's browser. Laravel provides several different ways to return responses. The most basic response is simply returning a string from a route or controller. The framework will automatically convert the string into a full HTTP response:
  • 27. 27 Response Objects Typically, you won't just be returning simple strings or arrays from your route actions. Instead, you will be returning full IlluminateHttpResponse instances or views.
  • 28. 28 Redirects Redirects are instances of the IlluminateHttpRedirectResponse class, and contain the proper headers needed to redirect the user to another URL. There are several ways to generate a RedirectResponse instance. The simplest method is to use the global redirect helper:
  • 31. 31 Other Response Types The response helper may be used to generate other types of response instances. If you need control over the response's status and headers but also need to return a view as the response's content, you should use the view method:
  • 32. 32 File Downloads The download method may be used to generate a response that forces the user's browser to download the file at the given path. The download method accepts a file name as the second argument to the method, which will determine the file name that is seen by the user downloading the file. Finally, you may pass an array of HTTP headers as the third argument to the method: