Scroll to top

Basic Routing in Laravel

In this tutorial i am going to explain how routing works in Laravel in detail. I assume you have a new laravel application folder for testing purpose. Let’s take a look at a request being made to the Laravel framework.

http://domain.com/blog/page

In this example, we are using the HTTP  protocol used by most web browsers to access your laravel application hosted on domain.com. The blog/page portion of the URL which we use to route web requests to appropriate logic.

Routes are defined in the app/routes.php file in laravel application. Let’s go ahead and create a route that will listen for the request which we mentioned above.

<?php
 
// app/routes.php
 
Route::get('blog/page', function() {
 
  return 'Hello World!';
 
});

Now enter http://domain.com/blog/page  in your web browser you will see the output on your screen Hello World! by replacing the domain.com with your server hostname.

Routes are always defined using the Route class. The get part is a method on the route class that is used to catch requests made by using the HTTP  verb GET  to a certain URL.

There are other HTTP verbs available as well. Here are some of the methods listed below.

<?php
 
// app/routes.php
 
Route::get();
 
Route::post();
 
Route::put();
 
Route::delete();
 
Route::any();

All of the methods accept same parameters, so feel free to use whatever HTTP based on the situation. This is known as RESTful routing. For now, you need to know GET verb is used to request a web page, whereas POST verb is used to when you need to send an additional data with the request.

Whereas Route:any() can be used for all kinds of the request but it’s better to use the respect verb based on the situation and functionality.

let’s look at the parameters of get verb closely. The first parameter is blog/page defines to URI to which URL you want to match. The final parameter is used in this instance to provide the application logic to handle the request. Here we are using Closure, which is also known as an anonymous  function.

We can rewrite the above route in the below way also by saving the Closure function to a variable.

<?php
 
// app/routes.php
 
$logic = function() {
 
  return 'Hello World!';
 
}
 
Route::get('blog/page', $logic);

You can define many routes as you like, for example:

<?php
 
// app/routes.php
 
Route::get('blog/home', function() {
 
  return 'Home Page';
 
});
 
Route::get('blog/list', function() {
 
  return 'Blogs List';
 
});
 
Route::get('blog/contact', function() {
 
  return 'Contact Page';
 
});
 
Route::get('blog/about', function() {
 
  return 'About Us Page';
 
});

You can register a route for multiple verbs also something like below.

<?php
 
// app/routes.php
 
Route::match(array('GET', 'POST'), '/', function()
{
 
    return 'Hello World';
 
});

Route Parameters can be used to insert placeholders into your route definition. Let’s get a category of blogs using Route::get method.

<?php
 
// app/routes.php
 
Route::get('blog/list', function() {
 
  return 'Blogs list';
 
});
 
Route::get('blog/{category}', function($category){
 
  return 'Blogs in this {$category} category!';
 
});

In this example, we are trying to get the only certain category of blogs from all using a {category} placeholder. That {category} will map in the Closure logic part and it will give only blogs which are under the respective category.

We can make the {Category} as an optional or we can set it to default also using the below way.

<?php
 
// app/routes.php
 
// making category optional 
 
Route::get('blog/{category?}', function($category = null){
 
  if($category == null) return 'Blogs List!';
 
  return 'Blogs in this {$category} category!';
 
});
 
// setting up a default category
 
Route::get('blog/{category?}', function($category = 'Technology'){
 
  return 'Blogs in this {$category} category!';
 
});

You can restrict the placeholders using regular expressions also. Like for example, you want to have category name restrictions something like below.

<?php
 
// app/routes.php
 
Route::get('blog/{category}', function($category)
{
    //
})
->where('category', '[A-Za-z]+');

You can declare the pattern globally for all routes at a single place for category name instead of declaring for every route like below way.

<?php
 
// app/routes.php
 
Route::pattern('category', '[a-zA-Z]+');
 
Route::get('blog/{category}', function($category)
{
    // Only called if {category} is string.
});

This is all about basic routing, I will explain in my next tutorial about advanced Routing like how to use filters, groups, prefixing, securing routes and domain-based routing etc in detail.

Thanks

KodeInfo

Author avatar

admin

http://www.kodeinfo.com
Imran is a web developer and consultant from India. He is the founder of KodeInfo, the PHP and Laravel Community . In the meantime he follows other projects, works as a freelance backend consultant for PHP applications and studies IT Engineering . He loves to learn new things, not only about PHP or development but everything.

Post a Comment

Your email address will not be published. Required fields are marked *