Route in Laravel Framework

  1. Download and install the latest version of Composer from https://getcomposer.org/download/

  2. Open Windows Terminal in Visual Studio Code install Laravel Installer with command as below:

    composer global require laravel/installer
  3. Create new folder named LearnLaravelWithRealApps. Use Visual Studio Code open to LearnLaravelWithRealApps foler. In this folder, create new project named LearnLaravelWithRealApps with command as below:

    laravel new LearnLaravelWithRealApps

  4. Run LearnLaravelWithRealApps project with command as below:

    php artisan serve
  5. Open LearnLaravelWithRealApps project with url as below:

    http://localhost:8000

In app/Http/Controllers folder create new controllers as below:

Create new PHP file named DemoController.php in app/Http/Controllers folder as below:

<?php

namespace App\Http\Controllers;

class DemoController extends Controller
{
    public function index()
    {
        return view('demo/index');
    }
}

Create new PHP file named NewsController.php in app/Http/Controllers folder as below:

<?php

namespace App\Http\Controllers;

class NewsController extends Controller
{
    public function index()
    {
        return view('news/index');
    }

    public function details($id)
    {
        echo 'id: '.$id;
        return view('news/details');
    }

    public function display($id, $username)
    {
        echo 'id: '.$id;
        echo '<br>username: '.$username;
        return view('news/details');
    }

}

In app/Http/Controllers, create new folder named Admin. In Admin folder, create new PHP file named DashboardController.php as below:

<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;

class DashboardController extends Controller
{
    public function index() {
        return view('admin/dashboard/index');
    }

    public function details($id) {
        echo 'id: '.$id;
        return view('admin/dashboard/index');
    }
}

In resources/views folder, create new views as below:

Create new folder named demo in resources/views folder. In demo folder, create new views as below:

Create new PHP file named index.blade.php in resources/views/demo as below:

<html>
    [<head>
        <title>Laravel</title>
    </head>
    <body>
        <fieldset>
            <legend>Demo Controller Routing</legend>
            <a href="{{url('/')}}">Menu 1</a> 
            <br>
            <a href="{{url('/demo')}}">Menu 2</a>
            <br>
            <a href="{{url('/demo/index')}}">Menu 3</a>
        </fieldset>
    
        <fieldset>
            <legend>News Controller Routing</legend>
            <a href="{{url('/news')}}">Menu 4</a> 
            <br>
            <a href="{{url('/news/index')}}">Menu 5</a> 
            <br>
            <a href="{{url('/news/details/123')}}">Menu 6</a> 
            <br>
            <a href="{{url('/news/display/456/p91')}}">Menu 7</a> 
        </fieldset>
    
        <fieldset>
            <legend>Dashboard Controller Routing</legend>
            <a href="{{url('/admin')}}">Menu 8</a> 
            <br>
            <a href="{{url('/admin/dashboard')}}">Menu 9</a> 
            <br>
            <a href="{{url('/admin/dashboard/index')}}">Menu 10</a> 
            <br>
            <a href="{{url('/admin/dashboard/details/123')}}">Menu 11</a> 
        </fieldset>
    
    </body>
</html>

Create new folder named news in resources/views folder. In news folder, create new views as below:

Create new PHP file named index.blade.php in resources/views/news as below:

<html>
<head>
    <title>Laravel</title>
</head>
<body>
    <h3>Index - News Controller</h3>
</body>
</html>

Create new PHP file named details.blade.php in resources/views/news as below:

<html>
<head>
    <title>Laravel</title>
</head>
<body>
    <h3>Details - News Controller</h3>
</body>
</html>

Create new folder named admin in resources/views folder. In admin folder, create new folder named dashboard. In dashboard folder, create new views as below:

Create new PHP file named index.blade.php in resources/views/admin/dashboard as below:

<html>
<head>
    <title>Laravel</title>
</head>
<body>
    <h3>Dashboard - Admin Panel</h3>
</body>
</html>

Open web.php file in routes folder, add new routes as below:

<?php
				
use Illuminate\Support\Facades\Route;

use App\Http\Controllers\DemoController;
use App\Http\Controllers\NewsController;
use App\Http\Controllers\Admin\DashboardController;

Route::group([], function () {
    Route::get('/', [DemoController::class, 'index']);
    Route::get('/demo', [DemoController::class, 'index']);
    Route::get('/demo/index', [DemoController::class, 'index']);
});

Route::group(['prefix' => 'news'], function () {
    Route::get('/', [NewsController::class, 'index']);
    Route::get('/index', [NewsController::class, 'index']);
    Route::get('/details/{id}', [NewsController::class, 'details']);
    Route::get('/display/{id}/{username}', [NewsController::class, 'display']);
});

Route::group(['namespace' => 'Admin', 'prefix' => 'admin'], function () {
    Route::group([], function () {
        Route::get('/', [DashboardController::class, 'index']);
    });
    Route::group(['prefix' => 'dashboard'], function () {
        Route::get('/', [DashboardController::class, 'index']);
        Route::get('/index', [DashboardController::class, 'index']);
        Route::get('/details/{id}', [DashboardController::class, 'details']);
    });
});

  • Access index action in Demo controller with following url: http://localhost:8000/demo/index

    Output

  • Access index action in News controller with following url: http://localhost:8000/news/index

    Output

  • Access details action in News controller with following url: http://localhost:8000/news/details/123

    Output

  • Access display action in News controller with following url: http://localhost:8000/news/display/456/p91

    Output

  • Access index action in Dashboard controller with following url: http://localhost:8000/admin/dashboard/index

    Output

  • Access details action in Dashboard controller with following url: http://localhost:8000/admin/dashboard/details/123

    Output