Making a web application more interactive, easy to use & readable are the basic objectives of any web developer before beginning with the web development. For back-end developers, routing in laravel is one of the coolest features that makes the website more readable. With this, you would be able to see top down view of your application & the routes file is like staring down at a roadmap from the clouds.
Let’s explore this routing more closely for applying them to real world outline.
All routes are defined in route files. For the new version of Laravel 5.4, we have a separate directory for routes (in previous versions the path of routes file was app/http/routes.php). Route files are automatically loaded into the framework. 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 CSRF protection and session state. The routes in routes/api.php are stateless and are assigned to the API middleware group. You can begin with defining routes in your routes/web.php file.
Routing works in following way:
Step-1 We need to execute/hit the route url of application.
Step-2 The executed url find the appropriate match for the route url in route.php, this will execute the related function.
Step-3 Function calls the related procedure/view and produces the expected results.
Following are the available Router Methods:
Route::get($uri, $callback);
Route::post($uri, $callback);
Route::put($uri, $callback);
Route::patch($uri, $callback);
Route::delete($uri, $callback);
Route::options($uri, $callback);
Define routing for static pages:
– The routes for static pages are easiest one, as they require only their views. Also, no login and no any parameters are required to display the static page. You can also display a simple message for any route, by returning message in route.
Ex.
Route::get(‘/about’, function()
{
return View::make(‘welcome’);
});
Route::get(‘/getMessage’, function()
{
return ‘Welcome Message’;
});
We already have the different functions in the controller, so we can also write routes specifying the name of the controller and its method name.
Route::get(‘/adduser’,’user@addUser’);
Route::post(‘/adduser’,’user@addUser’);
Many times you need to write the same routes for different methods. For GET and POST method it happens that you get the result from the one function and you need to post the parameter to the same function. In such cases, you can use “match” method to register such multiple methods in routes. It is an easy way to implement the multiple methods and minimize the lines of code.
Ex.
Route::match([‘get’, ‘post’], ‘/’, function () {
return View::make(‘/user’,’user@index’);
});
If you want to give response to all HTTP verbs then you can use “any” method.
But for POST, PUT, or DELETE routes, you should include a CSRF token field. Otherwise, the request will be rejected.
Ex.
Route::any(‘user’, function () {
//
});
Routing With Parameters:
Many times we need to pass some parameters to URL and want to capture them in respective functions. So we need to modify the code in routes.php file accordingly.
There are two types by which we can capture those parameters passed in URL.
- Required parameters.
- Optional parameters.
- Required parameters:
Sometimes we need to capture the parameters from the URL.
For example, if you want to pass user id to the URL.
You can do this by defining routes as follows
Route::get(‘user/view/{id}’, function ($id) {
return ‘Information User Id :’.$id;
});
We can also pass multiple parameters in URL
Route::get(‘report/{countryId}/{intYear}/{intQuarter}’, function ($countryId,$intYear,$intQuarter) {
return “Report for Country ID : $countryId >> Year : $intYear >> Quarter : $intQuarter”;
});
- Optional Parameters:
Sometimes, there are some parameters which may or may not be present in the URL.
In such cases, we can use the optional parameters as those parameters are not necessary for URL.
Route::get(‘/user/{status?}’,function($status = ‘Active’){
echo “Name: “.$name;
});
So, guys, we have discussed the routing, how routing works, routing for static pages, routing with parameters and types of parameter routing. I hope this would help all back end developers to implement it for making web application more effective.We will cover some more routing features in upcoming blogs.