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:
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 save(Request $request)
{
$this->validate($request, [
'username' => [
'required',
'min:3',
'max:8',
],
'password' => 'required|regex:((?=.*\\d)(?=.*[a-z])(?=.*[A-Z])(?=.*[@#$%]).{6,20})',
'confirmpassword' => 'required|same:password',
'email' => 'required|email',
'website' => 'nullable|url',
'age' => 'numeric|min:18|max:120'
]);
return view('demo/success');
}
}
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>Register</h3>
@if (count($errors) > 0)
<div class="alert alert-danger">
<ul>
@foreach ($errors->all() as $error)
<li>{{ $error }}</li>
@endforeach
</ul>
</div>
@endif
<form method="post" action="{{url('/demo/save')}}">
@csrf
<table border="0">
<tr>
<td>Username</td>
<td><input type="text" name="username" value="{{ old('username') }}"></td>
<td>
{!! $errors->first('username', '<p class="help-block">:message</p>') !!}
</td>
</tr>
<tr>
<td>Password</td>
<td><input type="password" name="password"></td>
<td>
{!! $errors->first('password', '<p class="help-block">:message</p>') !!}
</td>
</tr>
<tr>
<td>Confirm Password</td>
<td><input type="password" name="confirmpassword"></td>
<td>
{!! $errors->first('confirmpassword', '<p class="help-block">:message</p>') !!}
</td>
</tr>
<tr>
<td>Email</td>
<td><input type="text" name="email" value="{{ old('email') }}"></td>
<td>
{!! $errors->first('email', '<p class="help-block">:message</p>') !!}
</td>
</tr>
<tr>
<td>Website</td>
<td><input type="text" name="website" value="{{ old('website') }}"></td>
<td>
{!! $errors->first('website', '<p class="help-block">:message</p>') !!}
</td>
</tr>
<tr>
<td>Age</td>
<td><input type="text" name="age" value="{{ old('age') }}"></td>
<td>
{!! $errors->first('age', '<p class="help-block">:message</p>') !!}
</td>
</tr>
<tr>
<td> </td>
<td><input type="submit" value="Save"></td>
</tr>
</table>
</form>
</body>
</html>
Welcome View
Create new PHP file named success.blade.php in resources/views/demo as below:
<html>
<head>
<title>Laravel</title>
</head>
<body>
Success
</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::post('/demo/save', [DemoController::class, 'save']);
});
Structure of Laravel Project
Run Application
Access index action in Demo controller with following url: http://localhost:8000/demo/index
Output
Click Save button submit form to Save action in Demo controller with invalid data as below:
Click Save button submit form to Save action in Demo controller with valid data
Output