0% found this document useful (0 votes)
23 views10 pages

Namespaces and Request in Laravel

LPU CAP 777

Uploaded by

hemilab667
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
23 views10 pages

Namespaces and Request in Laravel

LPU CAP 777

Uploaded by

hemilab667
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 10

Namespaces &

Request Class
Gagandeep Sethi
What is Namespace?
• Class of elements in which each element has a unique name to that
associated class.
• It may be shared with elements in other classes.
• The use keyword allows the developers to shorten the namespace.
• use <namespace-name>;
• The default namespace used in Laravel is App, however a user can change
the namespace to match with web application
• Creating user defined namespace with artisan command is mentioned as
follows −
• php artisan app:name SocialNet
• The namespace once created can include various functionalities which can
be used in controllers and various classes.
Example Creating Namespace
(Steps)

• Let's create a custom class


inside the app/Helpers
directory.
1.Create the directory
app/Helpers (if it doesn't exist).
2.Inside it, create a file called
MathHelper.php:
Using Namespace In Controller
(HomeController)
• <?php namespace App\Http\Controllers;
use App\Helpers\MathHelper;
class HomeController extends Controller {
public function index() {
$sum = MathHelper::add(5, 3); // Call the add function
$difference = MathHelper::subtract(5, 3);
return response()->json(
[ 'sum' => $sum,
'difference' => $difference,
]);
}
}
Request
• The Request class is part of the Illuminate\Http\
Request namespace and is used to handle and manage
HTTP requests.
• It provides an easy way to access user input, query
parameters, headers, cookies, files, and more.
• Laravel automatically creates a Request instance for
every incoming HTTP request and passes it to your
controllers or routes.
Usage of Request Class
• Import : use Illuminate\Http\Request;
• Retrieve Request Data:
• public function store(Request $request)
{
$name = $request->input('name’);
return response()->json(
['name' =>
$name]
);
}
Retrieve Data
• $data = $request->all(): Retrieves all data
• Retrieves all input data except the specified fields.
• $data = $request->except(['password’]);
• Checks if a specific key exists in the request input.
if ($request->has('email’))
{
// Do something with the email
}
• Checks if the input field has a non-empty value.
if ($request->filled('name’))
{
// Name field is not empty
}
• Retrieves query string parameters from the request
URL.
• $page = $request->query('page', 1); // Default to 1 if not
present
• Retrieves an uploaded file from the request.
• $file = $request->file(image’);
• Checks if a file was uploaded with the given key.
• if ($request->hasFile(image’))
{
// Process the file
}
• Returns the HTTP method of the request (e.g., GET, POST, PUT).
• $method = $request->method();
• Returns the path of the current request URL.
• $path = $request->path(); // E.g., 'user/profile'
• Returns the full URL without query parameters.
• $url = $request->url(); // E.g., 'https://example.com/user/profile'
• Returns the full URL including query parameters.
• $fullUrl = $request->fullUrl(); // E.g., 'https://example.com/user/profile?
page=2'
• Retrieves a header value from the request.
• $userAgent = $request->header('User-Agent');
Example
Controller
Method Using the
Request Class

You might also like