Install Laravel
-
Download and install the latest version of Composer from https://getcomposer.org/download/
-
Open Windows Terminal in Visual Studio Code install Laravel Installer with command as below:
composer global require laravel/installer
-
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
-
Run LearnLaravelWithRealApps project with command as below:
php artisan serve
-
Open LearnLaravelWithRealApps project with url as below:
http://localhost:8000
Create Controller
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');
}
public function index2()
{
return view('demo/index2');
}
public function index3()
{
return view('demo/index3');
}
}
Create Views
Create new folder named demo in resources/views folder. In this folder, create new Blade file as below:
Index View
In resources/views/demo folder, create new Blade file named index.blade.php as below:
<html>
<head>
<title>Laravel</title>
</head>
<body>
<h3>Index</h3>
</body>
</html>
Index 2 View
In resources/views/demo folder, create new Blade file named index2.blade.php as below:
<html>
<head>
<title>Laravel</title>
</head>
<body>
<h3>Index 2</h3>
</body>
</html>
Index 3 View
In resources/views/demo folder, create new Blade file named index3.blade.php as below:
<html>
<head>
<title>Laravel</title>
</head>
<body>
<h3>Index 3</h3>
</body>
</html>
Create Middlewares
Use command line below to create new middlewares:
php artisan make:middleware YourMiddleware
Log1 Middleware
Use php artisan make:middleware Log1Middleware command line to create Log1Middleware as below:
namespace App\Http\Middleware;
use Closure;
use Illuminate\Http\Request;
class Log1Middleware
{
public function handle(Request $request, Closure $next)
{
echo '<br>date: '.date('d/m/Y H:i:s');
return $next($request);
}
}
Log2 Middleware
Use php artisan make:middleware Log2Middleware command line to create Log2Middleware as below:
namespace App\Http\Middleware;
use Closure;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\URL;
class Log2Middleware
{
public function handle(Request $request, Closure $next)
{
echo '<br>'.URL::current();
return $next($request);
}
}
Log3 Middleware
Use php artisan make:middleware Log3Middleware command line to create Log3Middleware as below:
namespace App\Http\Middleware;
use Closure;
use Illuminate\Http\Request;
class Log3Middleware
{
public function handle(Request $request, Closure $next)
{
echo '<br>ip: '.$request->ip();
return $next($request);
}
}
Log4 Middleware
Use php artisan make:middleware Log4Middleware command line to create Log4Middleware as below:
namespace App\Http\Middleware;
use Closure;
use Illuminate\Http\Request;
class Log4Middleware
{
public function handle(Request $request, Closure $next)
{
echo '<br>path: '.$request->path();
return $next($request);
}
}
Declare Middlewares to Kernel
Open App\Http\Kernel.php file and declare new middlewares to $routeMiddleware as below:
protected $routeMiddleware = [
'log1Middleware' => \App\Http\Middleware\Log1Middleware::class,
'log2Middleware' => \App\Http\Middleware\Log2Middleware::class,
'log3Middleware' => \App\Http\Middleware\Log3Middleware::class,
'log4Middleware' => \App\Http\Middleware\Log4Middleware::class,
];
Create Routes
Open web.php file in routes folder, add new routes as below:
<?php
use App\Http\Controllers\DemoController;
use Illuminate\Support\Facades\Route;
Route::group(['prefix' => 'demo', 'middleware' => ['log1Middleware', 'log2Middleware', 'log3Middleware']], function () {
Route::get('/', [DemoController::class, 'index']);
Route::get('/index', [DemoController::class, 'index'])->middleware(['log4Middleware']);
Route::get('/index2', [DemoController::class, 'index2'])->withoutMiddleware(['log2Middleware', 'log3Middleware']);
Route::group(['prefix' => 'index3', 'excluded_middleware' => ['log1Middleware', 'log2Middleware']], function () {
Route::get('/', [DemoController::class, 'index3']);
});
});
Structure of Laravel Project
Run Application
-
Access index action in Demo controller with http://localhost:8000/demo url:
-
Access index action in Demo controller with http://localhost:8000/demo/index url:
-
Access index2 action in Demo controller with http://localhost:8000/demo/index2 url:
-
Access index3 action in Demo controller with http://localhost:8000/demo/index3 url: