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
In app/Http/Controllers folder create new controllers as below:
Demo 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');
}
}
News Controller
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');
}
}
Dashboard Controller
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');
}
}
Views
In resources/views folder, create new views as below:
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>
<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>
News Views
Create new folder named news in resources/views folder. In news folder, create new views as below:
Index View
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>
Details View
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>
Dashboard Views
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:
Index View
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>
Routes
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']);
});
});
Structure of Laravel Project
Run Application
-
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