In this video we are going to learn about the routing.
Routing is one of the essential concepts in Laravel.
Routing allows us, to route all our application requests, to its appropriate controller.
All Laravel routes are defined in route files, which are located in the routes directory.
These files are automatically loaded by the framework.
Lets see route file, go to the laravel project.
In Project root directory you see the routes folder and inside the routes folder.
You can see web.php and api.php.
These are the routes file.
web.php file defines routes, that are for web interface.
These routes are assigned the web middleware group, which provides features like session state and CSRF protection.
The routes defined in web.php may be accessed by entering the defined route's URL in your browser.
The routes in api.php are stateless and are assigned the api middleware group.
ok Now lets create a route.
open api.php file and just write here.

Route::get('/user', function (){
return \"Hi user\";
});

Here /user is uri and after comma is callback method whenever this route will be called then this callback function will be executed.
It will return this. Now lets check this api route.
Here I am just going to use postman.
Postman is one of the most popular tools used in RestfulAPI testing.
In postman url just type localhost:8000/api/user.
Here api is prefix for the api routes and it is default for api route in laravel.
Ok so type api here and then type route uri.
Now press enter here see the text Hi User we can also pass a parameter with the route.
Laravel provides two ways of capturing the passed parameter:.
Required parameter and Optional Parameter.
So lets see required parameter first.
so passing the parameter,
Goto the route and modify the route just type here /{} and inside the the curley bracket just type parameter name lets say parameter name is name.
and inside the callback method type here $name.
and here type \"Hi \". $name.

Route::get('/user/{$name}', function ($name){
return \"Hi \".$name;
});

In laravel dot Is used for the concatenation alright now test this route.
Switch to postman and add any name here.
just type any name.
Let say name is jenifer.
Now press enter.
ok see here is the result.
When I remove the parameter then its throwing some error.
It Means required parameter must be passed here otherwise it will gives error.
alright now see optional parameter.
For that we have to add simply a question mark here{name?} and add $name=null here.

Route::get('/user/{$name?}', function ($name=null){
return \"Hi \".$name;
});

Now this parameter has become optional.
Now check this.
Let's switch to postman and click on send.
Here you can see the response and.
Now remove the parameter.
and you can see its still working.
Now lets see how can we constrain the routes.
For that just go to the route file and just add ->where('name','[a-zA-Z]+');
Parameter name and add the regular expression.
Now this route only accept alphabet not numeric.

Route::get('/user/{$name}', function ($name){
return \"Hi \".$name;
})->where('name','[a-zA-Z]+');

Now lets check this.
If I enter any numeric value.
You can see it throwing error.
If I enter any name here you can see the response.
Now lets create another route.

Route::get('/user-ids/{id}', function ($id){
return \"Hi, Your id is \". $id;
})->where('id','[0-9]+');

Now its only accept numeric value as a prameter.
Lets check it.
So switch to the postman.
and just enter any alphabet.
You can see its not working.
If enter the numeric value its working.
Now lets see how can we add the global constraints.
Just remove this constrainsts from here.
and now go to the app/providers/RouteServiceProvider.php.
Just open this file.
and inside the boot function.
We can add the global constraints.
For that just write here.

Route::pattern('id', '[0-9]+');


Route::pattern('name', '[a-zA-Z]+');

Now this constraints are applied on each routes.
Now lets check this.
Switch to postman.
and now enter the url and and numeric value here.
Its not woring if I enter the alphabet its working now alright.
Sometimes we need to register a route.
that responds to multiple HTTP verbs.
We can do this using the match method.
for that go to the api.php file.
and create a route with match http verb.
like get, post, put, delete etc.
so i am just typing here get and post verbs

Route::match(['get', 'post'], '/', function (Request $req)
{
return \"hi there, the requested method\" . $req->method();
});


It shows which http verb is requested now test this route using postman.
Ok now we send a http get request and click on send.
here you can see.
hi there, the requested method is get.
ok now change get with post.
and now click on send button.
now here you can see this is post request.
so with match we can register a route that responds to multiple HTTP verbs. alright we can also register a route that responds to all HTTP verbs using the any method:.
lets see how can we write any http verbs so open api.php.
and here type.

Route::any('/post', function (Request $req) {
retun \"Requested method is \". $req->method();
});

Now lets check this route.
Just switch to postman.
and first we select put and now click on send button.
Ok now you can the result.
Alright now change method to post or any other.
and click one send button.
Here is result.
So in this way you can you can create any types of route so that's all about laravel 8 Routing.