Repository Pattern 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

Create new folder named Repositories. In Repositories folder, create new folder named Demo. Create new PHP file named DemoRepository.php in app/Repositories/Demo folder as below:

<?php

namespace App\Repositories\Demo;

interface DemoRepository
{
	public function hello();

	public function hi($name);
}

Create new PHP file named DemoRepositoryImpl.php in app/Repositories/Demo folder as below:

<?php

namespace App\Repositories\Demo;

class DemoRepositoryImpl implements DemoRepository
{
	public function hello()
	{
		return 'Hello World';
	}

	public function hi($name)
	{
		return 'Hi '.$name;
	}
}

Open AppServiceProvider.php file in app/Providers folder and add declare new repository to register method as below:

<?php

namespace App\Providers;

use App\Repositories\Demo\DemoRepository;
use App\Repositories\Demo\DemoRepositoryImpl;
use Illuminate\Support\ServiceProvider;

class AppServiceProvider extends ServiceProvider
{
	public function register()
	{
		$this->app->bind(DemoRepository::class, DemoRepositoryImpl::class);
	}

	public function boot()
	{
	}
}

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

<?php

namespace App\Http\Controllers;

use App\Repositories\Demo\DemoRepository;

class DemoController extends Controller
{
	protected $demoRepository;

	public function __construct(DemoRepository $demoRepository)
	{
		$this->demoRepository = $demoRepository;
	}

	public function index()
	{
		$data = array(
			'result1' => $this->demoRepository->hello(),
			'result2' => $this->demoRepository->hi('abc')
		);
		return view('demo/index')->with($data);
	}
}

Create new folder named demo in resources/views folder. In this folder, create new Blade file named index.blade.php as below:

<html>

	<head>
		<title>Laravel</title>
	</head>

	<body>

		<h3>Index</h3>
		Result 1: {{$result1}}
		<br>
		Result 2: {{$result2}}

	</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;

Route::get('/', [DemoController::class, 'index']);
Route::get('/demo', [DemoController::class, 'index']);
Route::get('/demo/index', [DemoController::class, 'index']);

Access index action in Demo controller with urls as below:

  • http://localhost:8000
  • http://localhost:8000/demo
  • http://localhost:8000/demo/index

Output