0% found this document useful (0 votes)
3 views26 pages

ELEC1 Lesson 3

The document provides an overview of routing in Laravel, detailing how routes are defined in route files and the differences between web and API routes. It explains various routing methods, including GET, POST, PUT, PATCH, and how to handle redirects and named routes. Additionally, it covers the use of parameters in routes and the resource method approach for defining routes with controllers.

Uploaded by

22ur0690
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)
3 views26 pages

ELEC1 Lesson 3

The document provides an overview of routing in Laravel, detailing how routes are defined in route files and the differences between web and API routes. It explains various routing methods, including GET, POST, PUT, PATCH, and how to handle redirects and named routes. Additionally, it covers the use of parameters in routes and the resource method approach for defining routes with controllers.

Uploaded by

22ur0690
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/ 26

Exception ROUTING IN LARAVEL

Handling Web Systems and Technologies 2

Leo Gabriel Villanueva


OVERVIEW

▰ Routing is one of the essential concepts in Laravel. The main


functionality of the routes is to route all your application
requests to the appropriate controller.
▰ All Laravel routes are defined in your route files, which are
located in the routes directory.
▰ The routes/web.php file defines routes that are for your web
interface. These routes are assigned the web middleware group,
which provides features like session state and CSRF protection.
▰ The routes in routes/api.php are stateless and are assigned
the api middleware group.
2
ROUTE METHO

3
GET METHOD : WEB.PHP

▰ Route::get('/', function () {
▰ return 'Hello, World!’; // Hello, World!
▰ });
▰ To check the name of the route, type in the command prompt
the code php artisan route:list

4
RIDERECT ROUTES

▰ We can use Route::redirect method if we want to redirect from


one page to another.
▰ This method provides a convenient shortcut so that you do not
have to define a full route or controller for performing a simple
redirect:

▰ By default, Route::redirect returns a 302 status code. You may


customize the status code using the optional third parameter:

5
RIDERECT ROUTES

▰ Route::permanentRedirect method to return a 301 status code:

▰ Redirect() method
Redirect() method is used to navigate from one URL to another
URL. This method provides a convenient or shortcut way to
move from one URI to another URI. With the help of this method,
you don't need to define the full route.

6
2 ways of redirect() method

▰ There are two ways of using redirect() method


▰ First:
▰ <?php
▰ Route::get('hello', function () {
▰ return redirect('/');
▰ })
▰ Second:
▰ <?php
▰ Route::redirect('hello','/');
7
2 ways of redirect() method

▰ There are two ways of using redirect() method


▰ First:
▰ <?php
▰ Route::get('hello', function () {
▰ return redirect('/');
▰ })
▰ Second:
▰ <?php
▰ Route::redirect('hello','/');
8
View Routes

▰ Route::view
▰ Method used to return view. It provides a simple shortcut so that
you do not have to define a full route or controller
▰ It accepts a URI as its first argument and a view name as its
second argument.
▰ We can also provide an array of data to pass to the view as an
optional third argument:

9
View Routes

▰ View()
▰ It is method used to return the view of another URL.

10
Named Routes

▰ Named routes allow the convenient generation of URLs or


redirects for specific routes. You may specify a name for a route
by chaining the name method onto the route definition.
▰ If when we need to access this URL, all we need to do is invoke
the function route('profile').

11
Named Routes

▰ //Generating URLs
▰ $url= route('profile);
▰ //Generating Redirects...
▰ return redirect() -> route('profile');
▰ Using Controller

12
Named Routes

▰ If the named route defines


parameters, you may pass the
parameters as the second argument
to the route function. The given
parameters will automatically be
inserted into the generated URL in
their correct positions:

▰ If you pass additional parameters in


the array, those key / value pairs will
automatically be added to the 13
generated URL's query string:
Navigating from One Routes to
Another

▰ Step 1: Define the route in


the web.php file.
▰ Step 2: Move to the resources
▰ Route::Get('/',function()
folder and then click on the views
▰ { folder.
▰ return view('student'); ▰ Step 3: Create a new file and it is
▰ }); named as student.blade.php.
▰ Route::get('student/
details',function()
▰ {
▰ $url=route('student.details');
▰ return $url; 14
Navigating from One Routes to
Another

▰ <a href="{{ route('student.details') }}">Student</a>


▰ The above code navigates from student page to
the student.details which is the named route.

15
Routing Parameters

▰ There are two types of parameters we can use:


o Required Parameters
o Optional Parameters
Required Parameters
The required parameters are the parameters that we pass in the
URL. Sometimes you want to capture some segments of the URI
then this can be done by passing the parameters to the URL. For
example, you want to capture the user id from the URL.

16
Required Parameters

1. Route::get('/post/{id}', function($id)
2. {
3. return "id number is : ". $id;
4. });

- route parameters are enclosed within {} brackets,


- and parameters must contain alphabetic characters.
- It should not contain '-' character, and instead of using this
character, you can use '_' character.
- Route parameters are available in the route callbacks.

17
Required Parameters

1. Route::get(‘/user/{id}/{name}', function($id,$name)
2. {
3. return "id number is : ". $id ." ".$name;
4. });

18
Optional Parameters

1. Route::get('user/{id?} /{name?}', function ($id=null, $name=null) {


2. return $name;
3. });
- To make the route parameter optional, you can place '?' operator after
the parameter name.
- If you want to provide the optional parameter, and then make sure that
you have also provided the default value to the variable.

19
POST Method

▰ Sends data to the server to create a new resource.


▰ We define routes that handle the creation of resources, such as form submissions
or API requests to store data.
▰ // In routes/web.php
▰ Route::post('/users', function (Request $request) {
▰ // Validate and create a new user
▰ $validated = $request->validate([
▰ 'name' => 'required|string',
▰ 'email' => 'required|email|unique:users',
▰ ]);

▰ $user = App\Models\User::create($validated);
▰ return redirect('/users');
20
▰ });
PUT Method

▰ To update an existing resource by replacing it with the new data.


▰ We define routes that allow users to update data (for example, editing a user's
profile).
▰ // In routes/web.php
▰ Route::put('/users/{id}', function ($id, Request $request) {
▰ // Find the user by ID and update their information
▰ $user = App\Models\User::findOrFail($id);
▰ $user->update($request->all());

▰ return redirect('/users');
▰ });
21
PATCH Method

▰ To partially update an existing resource (typically, updating only specific fields of


a resource).
▰ We define routes for partial updates
▰ // In routes/web.php
▰ Route::patch('/users/{id}', function ($id, Request $request) {
▰ // Find the user and update only the provided fields
▰ $user = App\Models\User::findOrFail($id);
▰ $user->update($request->only(['name', 'email']));

▰ return redirect('/users');
▰ });
22
OPTIONS Method

▰ To describe the allowed HTTP methods for a resource. This method is usually
used for pre-flight requests in CORS (Cross-Origin Resource Sharing).
▰ It is commonly used in API configurations

▰ // In routes/web.php
▰ Route::options('/users', function () {
▰ return response()->json([], 200)->header('Allow', 'GET, POST, PUT, DELETE');
▰ });

23
ANY Method : Wildcard Route

▰ To handle requests of any HTTP method (GET, POST, PUT, DELETE, etc.).
▰ It is commonly used in API configurations

▰ // In routes/web.php
▰ Route::any('/users', function () {
▰ return 'This route accepts any HTTP method';
▰ });

24
Match Method

▰ To handle specific HTTP methods for the same route.


▰ // In routes/web.php
▰ Route::match(['get', 'post'], '/users', function () {
▰ return 'This route handles both GET and POST requests';
▰ });

25
Resource Method Approach

▰ We can also use controllers to define our routes, which is a more structured
approach.
▰ // In routes/web.php
▰ Route::resource('users', UserController::class);

▰ This single line will automatically create routes for GET, POST, PUT, PATCH, and
DELETE for the UserController.

26

You might also like