Create Repository Pattern Files In Laravel

Create Repository Pattern Files In Laravel

In repository pattern, there are 2 files that needs to be created. First file is the interface where you declare your methods and the second is your implementation class of interface. It is bit time consuming, in general, to create files and write the default code snippets in it. Hence, we have created a package for Laravel >= 5.1, >= 5.2 and >=5.3.

The above package when installed, will give a command that can be executed from the terminal, which then will create the files for you with the default code snippet to save you some time.

Let us install the package.

Installation Guide

From your terminal, require the package:

composer require detatech/repository-pattern

Once installed, open the config/app.php file and add the following line in $providers array

$providers = [
    // ... other serivce providers

    DetaTech\RepositoryPattern\RepositoryPatternServiceProvider::class,
];

Publish the config file in which you will be able to edit where the files should be created.

php artisan vendor:publish

This will publish repository_pattern.php in config folder with the following content:

<?php

return [
    /**
     * ------------------------------------------------------------------------
     * Root of the project
     * ------------------------------------------------------------------------
     * 
     * You can change it to whichever location you feel appropriate as per your
     * own convenience. The contract file and its corresponding implementation
     * file will be placed inside this folder.
     * 
     */
    'rootLocation' => app_path(),

    /**
     * ------------------------------------------------------------------------
     * Contract File Location
     * ------------------------------------------------------------------------
     * 
     * In this path, your contracts file will be created. You can change it to 
     * whichever location you feel appropriate as per your own convenience.
     * 
     */
    'contractFileLocation' => '/Repositories/Contracts',

    /**
     * ------------------------------------------------------------------------
     * Implemented Class File Location
     * ------------------------------------------------------------------------
     * 
     * In this path, the class that will implement contract file, will be
     * created here. You can change it to whichever location you feel
     * appropriate as per your own convenience.
     * 
     */
    'implementedClassFileLocation' => '/Repositories/Classes',
];

Now you can create the files using the following command from your terminal. 

php artisan repository:create YourRepositoryName

This command will create the interface file app/Repositories/Contracts/YourRepositoryNameContract and its implementation class file app/Repositories/Classes/YourRepositoryName.

When the files are created, you need to bind it to the IoC Container. In order to do that, open app/Providers/AppServiceProvider.php file and add the below code in register method:

$repositoryFileNames = [
    // Whatever file name that you give while creating the file from
    // the terminal that same name should come here in single quotes(')
    // In this example, it should be: 'YourRepositoryName'
];

foreach ($repositoryFileNames as $key => $fileName) {
    // Notice the namespace..
    // Keep it default if you have not changed it,
    // Else, update the word _App_ with your application's namespace.

    $this->app->bind(
        "App\\Repositories\\Contracts\\{$fileName}Contract",
        "App\\Repositories\\Classes\\{$fileName}"
    );
}

And done. You can now start coding in repository pattern.

Leave a Reply

Your email address will not be published. Required fields are marked *