Routing in Laravel

20. April 2016 Beginner, Laravel 3
Routing in Laravel

Hello everyone, welcome to justlaravel.com. Here we ‘ll see about routing in laravel. All laravel routes are defined in routes.php file (located at app/Http/routes.php).
Routing is very simple, clear and one of the coolest features in laravel, we can see all the pages, where they are linked in a single place.

Working Demo      Project on Github

We will see 4 different types of routing,

  • Route returning a view
  • Route returning some static text
  • Routing to a controller which returns a view
  • Routing to  a controller which returns data from database and renders in a view

 

Route returning a view:

When we open routes.php file, we can see a default route which returns a view, it looks like the snippet below,

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

when we open the browser and type localhost:8000/, it returns the default view named ‘welcome’

justLaravel - Routing in Laravel
justLaravel – Routing in Laravel

 

Route returning some static text:

We ‘ll define another simple route to display some sample text when that particular route is called,

Route::get ( '/hello', function () {
    return 'Hello World!';
} );

When we open the browser and type localhost:8000/hello, it will return the text ‘Hello World!’ we just gave,

justLaravel - Routing in Laravel
justLaravel – Routing in Laravel

 

Routing to a controller which returns a view:

We ‘ll define one more route, this time it calls the controller, and that controller simply returns a view,

Route::get ( '/callController', 'IndexController@index' );

The controller logic for index function would be,

public function index() {
    return view ( 'welcome' );
}

 

When we open the browser, and type localhost:8000/callController, it returns the default view named ‘Welcome’

justLaravel - Routing in Laravel
justLaravel – Routing in Laravel

 

 

Routing to a controller which returns data from the database and renders in a view:

We ‘ll define another route, it calls the controller, and this controller interacts with model and collects some data from the database, now we ‘ll render that obtained data in the view,

Route::get ( '/callControllerDb', 'IndexController@getFromDb');

The controller logic for getFromDb function would be,

public function getFromDb() {
    $users = User::all();
    return view('sample')->withDetails($users);  
}

the view file, sample.blade.php would be,

<!DOCTYPE html>
<html lang="en">
<head>
<title>justLaravel - Routing in Laravel</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet"
    href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css">
<script
    src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.0/jquery.min.js"></script>
<script
    src="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>
</head>
<body>
    <div class="fluid-container">
        <h2>Sample User details</h2>
        <table class="table table-striped">
            <thead>
                <tr>
                    <th>Name</th>
                    <th>Email</th>
                </tr>
            </thead>
            <tbody>
                @foreach($details as $user)
                <tr>
                    <td>{{$user->name}}</td>
                    <td>{{$user->email}}</td>
                </tr>
                @endforeach
            </tbody>
        </table>
    </div>
</body>
</html>

When we open the browser and type, localhost:8000/callControllerDb, it calls to index controller, which gets data from the database and renders it in the view,

justLaravel - Routing in Laravel
justLaravel – Routing in Laravel

Working Demo      Project on Github

NOTE: This post covers about routing only, other details about models, views and controllers are not covered here



3 thoughts on “Routing in Laravel”

  • 1
    Michelle Prather on December 14, 2016 Reply

    Thank you for such an easy implementation of the search function! My function displays results fine, however, when the search returns no results, the redirect to a different page works, but the message does not show. Any suggestions?

    • 2
      avinash on December 14, 2016 Reply

      Did you redirect it with a message.

  • 3
    Sam on May 21, 2018 Reply

    thanks for the amazing explanation, the foreach duplicate the value.
    when i run this code in my code, not for user for a search result
    @foreach($details as $user)

    {{$user->name}}
    {{$user->email}}

    @endforeach

    it appears as three duplicated rows.

Leave a Reply