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
Controllers
Create new PHP file named DemoController.php in app/Http/Controllers folder as below:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class DemoController extends Controller
{
public function index()
{
return view('demo/index');
}
public function welcome(Request $request)
{
$data = array(
'username' => $request->session()->get('username')
);
return view('demo/welcome')->with($data);
}
public function processLogin(Request $request)
{
$username = $request->input('username');
$password = $request->input('password');
if ($username == 'abc' && $password == '123') {
$request->session()->put('username', $username);
return redirect('/demo/welcome');
} else {
$data = array(
'msg' => 'Invalid'
);
return view('demo/index')->with($data);
}
}
public function logout(Request $request)
{
$request->session()->forget('username');
return redirect('/demo/index');
}
}
Views
Demo Views
Create new folder named demo in resources/views folder. In demo folder, create new views as below:
Index View
Create new PHP file named index.blade.php in resources/views/demo as below:
<html>
<head>
<title>Laravel</title>
</head>
<body>
<h3>Login</h3>
{{ isset($msg) ? $msg : '' }}
<form method="post" action="{{url('/demo/processlogin')}}">
@csrf
<table cellpadding="2" cellspacing="2">
<tr>
<td>Username</td>
<td>
<input type="text" name="username" required="required">
</td>
</tr>
<tr>
<td>Password</td>
<td>
<input type="password" name="password" required="required">
</td>
</tr>
<tr>
<td> </td>
<td>
<input type="submit" value="Login">
</td>
</tr>
</table>
</form>
</body>
</html>
Welcome View
Create new PHP file named welcome.blade.php in resources/views/demo as below:
<html>
<head>
<title>Laravel</title>
</head>
<body>
Welcome {{$username}}
<br>
<a href="{{url('/demo/logout')}}">Logout</a>
</body>
</html>
Routes
Open web.php file in routes folder, add new routes as below:
<?php
use Illuminate\Support\Facades\Route;
use App\Http\Controllers\DemoController;
Route::group([], function () {
Route::get('/', [DemoController::class, 'index']);
Route::get('/demo', [DemoController::class, 'index']);
Route::get('/demo/index', [DemoController::class, 'index']);
Route::get('/demo/welcome', [DemoController::class, 'welcome']);
Route::get('/demo/logout', [DemoController::class, 'logout']);
Route::post('/demo/processlogin', [DemoController::class, 'processLogin']);
});
Structure of Laravel Project
Run Application
Access index action in Demo controller with following url: http://localhost:8000/demo/index
Output
Test with invalid account is username: aaa and password: 123
Output
Test with valid account is username: abc and password: 123.
Output
Click logout link from success page to remove session and open login page again
Output