INT221
INT221
• All of the service providers for the application are configured in the
config/app.php configuration file's providers array.
• Laravel will iterate through this list of providers and instantiate each
of them.
• Service providers are responsible for bootstrapping all of the
framework's various components, such as the database, queue,
validation, and routing components.
• Open this file and route with Laravel, write to the end of
this file.
Syntax
// Syntax of a route
Route::request_type('/url', 'function()’);
For example
Route::get('/sayhello', function() {
return 'Hey ! Hello';
})
The Default Route Files
• All Laravel routes are defined in your route files, which are located in
the routes directory.
• Route::get($uri, $callback);
• Route::post($uri, $callback);
• Route::put($uri, $callback);
• Route::patch($uri, $callback);
• Route::delete($uri, $callback);
• Route::options($uri, $callback);
Redirect Routes
• If you are defining a route that redirects to another URI, you may use
the Route::redirect method.
Route::get('ID/{id}',function($id) {
echo 'ID: '.$id;
});
Optional Parameters
Route::get('/test2', function() {
return view('test2');
});
• Step 2 − Create two view files
— test.php and test2.php with the same code. These
are the two files which will share data.
Route::get('/', function () {
Route::get('/', function () {
});
Attaching Headers
• The response can be attached to headers using the
header() method.
• We can also attach the series of headers as shown in
the below sample code.
• In Laravel, the header() method is used to attach HTTP
headers to a response. HTTP headers provide essential
information about the request or response, and they
play a crucial role in web communication.
•
• Here are the main reasons why you might use the
header() method in Laravel responses:
1. Customizing Response Behavior:HTTP headers can control how the
client (e.g., a web browser) or intermediate servers (e.g., proxies)
handle the response.
2. Control caching:his prevents the client from caching the response.
3. Adding Custom Metadata:You can use headers to add custom
metadata to your response that might be useful for clients or other
services
4. Security Enhancements
5. Handling Authentication and Authorization
• Syntax
return response($content,$status)
->header('Content-Type', $type)
->header('X-Header-One', 'Header Value')
->header('X-Header-Two', 'Header Value');
Attaching Cookies
• The withcookie() helper method is used to attach
cookies.
return response()->json([
'status' => 'success',
'message' => 'This is a JSON response'
]);
Key Features of JSON Responses:
• Structured Data: JSON responses allow you to return complex, nested
data structures.