ELEC1 Lesson 3
ELEC1 Lesson 3
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
5
RIDERECT ROUTES
▰ 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
▰ 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
11
Named Routes
▰ //Generating URLs
▰ $url= route('profile);
▰ //Generating Redirects...
▰ return redirect() -> route('profile');
▰ Using Controller
12
Named Routes
15
Routing Parameters
16
Required Parameters
1. Route::get('/post/{id}', function($id)
2. {
3. return "id number is : ". $id;
4. });
17
Required Parameters
1. Route::get(‘/user/{id}/{name}', function($id,$name)
2. {
3. return "id number is : ". $id ." ".$name;
4. });
18
Optional Parameters
19
POST 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
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