Routing in Laravel Framework
- Basic Routing
- Routing allows user to route to a specific file or url which is mentioned in app/Http/routes.php
which is loaded by the App\Providers\RouteServiceProvider class.
Larvel Provides basic routing as follows :
Route::get('/'.function()
{
return "Hello User";
}
);
Now ,
- Routing with Parameters
- We can capture any input or example :"username " from the URL,by defining routes as follows:
Routes::get('/foo/{username}',functtion($username)
{
return "username :".$username;
});
Note: Routes parameters are always encased (enclosed) in curly braces like this
"{}".Since we get value of route parameter from above code we can do anything with that variable like search for record or save it in database. - Regular Expression in Routing
- You can add regular expression to your routing parameters using where method on routes instance .The where method accepts the name of parameters and a regular expression defining how the parameters should be constrained or conditioned :
Route::get('/user/{name}',function($name)
{
//code
})->where('name','[A-Za-z]');
- We will discuss advanced Routing in Next Post.