Routing in Laravel 5.3

Routing, a Static Method


Routing is the concept of setting up a new URI like http://localhost:8000/hello. It will take you to a destination web page. Laravel makes it extremely simple. It’s a static method that accepts two things: an URI and an anonymous function or Closure.
In the ‘app/Http/routes.php’ it’s been defined primarily. Start your favorite text editor and open the file. What you see?
You see some code like this:

Route::get('/', function () {
return view('welcome');
});

Here ‘Route’ is a class that has a static method ‘get’ that returns a ‘view’ method which presents a web page. When a user visits the home page he is taken to the ‘welcome’ page. This ‘welcome’ is actually a PHP file – ‘welcome.blade.php’. It was stored by default in the ‘views’ folder while we had been installing laravel. When we discuss the concept of ‘view’ and ‘blade’ template engine you’ll fully understand what’s happening in the presentation layer.
Using HTTP protocol you can send another request instead of the default route.

Route::get('/', function () {
return 'welcome';
});

It will simply return a word – ‘welcome’ on your home page. Try it or anything else.
Look at the code, there is an anonymous function also. As a PHP programmer I assume you know about anonymous function or closure. It’s a very useful function we often use in building applications. Laravel also uses this concept.
Now we have not yet defined our route so if we hit the browser on the URL – http://localhost:8000/hello we will get an error page. So let us create it first. Go to the ‘routes.php’ file and add this code:

//requesting a new page
Route::get('/hello', function () {
echo '

Hello

';
});

Let us see how it looks and if we can add any new HTML code into it to change the look.

Now we’d like to add more CSS style into it. I change the previous code into this:

//requesting a new page
Route::get('/hello', function () {
    echo "". 
    "Laravel
         
            html, body {
                height: 100%;
            }
            body {
                margin: 0;
                padding: 0;
                width: 100%;
                display: table;
                font-weight: 100;
                font-size: 20px;
                font-family: 'Lato';
            }
            .container {
                text-align: center;
                display: table-cell;
                vertical-align: middle;
            }
            .content {
                text-align: center;
                display: inline-block;
            }
            .title {
                font-size: 110px;
            }
        
    
    
        
Hello, Dear Reader
How are you? I hope you would love this book!
". ""; });

This code is quite big. It’s because I have added a small CSS style into it. You need not write this code. Just try to understand the power of this small file ‘routes.php’. You can virtually add any functionality into your application through this file. But never do this. The file ‘routes.php’ is not meant for this. Laravel has made separate places to put your CSS styles and HTML pages. We’ll see to it later in detail. Just before that we have just wanted to play with it.

With a new CSS style it looks very special. Later we will give the same output using Laravel’s ‘blade’ template engine.
Now, routing has many other important functions. Let us consider them one by one.
First of all we can add route parameter and pass data through the URL. It’s something like you have a http://localhost:8000/hello page and want to say ‘hello’ to your friend. Laravel manages it nicely. Through the anonymous function we can pass this parameter such as http://localhost:8000/hello/reader and the output will be ‘Hello reader.’ Let us see how we can do that.
Add this code to your ‘routes.php’:
Route::get( 'hello/{name}', function ($name){
echo "Hello " . $name;
});

Now you can pass any data through the URL http://localhost:8000/hello/reader as route parameter. I passed ‘reader’, you can pass any other data as you like.
I hope you get a basic idea how to use Route class. You also have come to know that Route class has some static methods like ‘GET’ and other methods. Through these methods Route class help the user reach her URL destination.
Other routing methods are like this:
Route::get();
Route::post();
Route::patch();
Route::put();
Route::delete();
Route::any();

I find the ‘any()’ method very interesting. You can register any route that responds to any HTTP verbs with this method.

Route::any('any', function (){
return "Anything is possible if you try hard!";
});

Go to ‘http://localhost:8000/any’ and see this simple output.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s