@@ -59,6 +59,122 @@ Add following code in root blade file in before close the body.
5959 }
6060 < / script>
6161```
62+ # Repository Generator
63+
64+ ## Table of Contents
65+ <ol >
66+ <li><a href="#features">Features</a></li>
67+ <li>
68+ <a href="#getting-started">Getting started</a>
69+ <ul>
70+ <li><a href="#installation">Installation</a></li>
71+ <li><a href="#publish-config-(optional)">Publish config (optional)</a></li>
72+ </ul>
73+ </li>
74+ <li>
75+ <a href="#usage">Usage</a>
76+ <ul>
77+ <li><a href="#generating-repositories">Generating repositories</a></li>
78+ <li><a href="#dependency-injection">Dependency Injection</a></li>
79+ </ul>
80+ </li>
81+ <li><a href="#manual-binding">Manual binding</a></li>
82+ <li><a href="#more-generator-packages">More generator packages</a></li>
83+ <li><a href="#contributing">Contributing</a></li>
84+ <li><a href="#license">License</a></li>
85+ </ol >
86+
87+ ## Features
88+ With this package you can generate repositories with the ``` artisan make:repository ``` command.
89+ The generator will generate the repository, repository interface and will bind them automatically (can be changed to
90+ manual binding) to the Service Container so you can inject the interface into your controllers.
91+
92+ ### Publish config (optional)
93+ ```
94+ php artisan vendor:publish --provider="RajTechnologies\Tools\ToolServiceProvider" --tag="config"
95+ ```
96+
97+ ## Usage
98+ For usage take the following steps. Generate the repository and then inject it into a controller or service.
99+
100+ ### Generating repositories
101+ Run the following command.
102+ ```
103+ php artisan make:repository UserRepository
104+ ```
105+ This example will generate the following files:
106+ ```
107+ app\Repositories\Eloquent\UserRepository
108+ app\Repositories\UserRepositoryInterface
109+ ```
110+
111+ ### Dependency Injection
112+ Next we have to inject the interface into the constructor our controller or service. For this example we will use the UserController.
113+ ``` php
114+ <?php
115+
116+ namespace App\Http\Controllers;
117+
118+ use App\Repositories\UserRepositoryInterface;
119+
120+ class UserController extends Controller
121+ {
122+ private $user;
123+
124+ public function __construct(UserRepositoryInterface $userRepository)
125+ {
126+ $this->user = $userRepository;
127+ }
128+
129+ // your controller functions
130+ }
131+ ```
132+
133+ By default you will be able to use Eloquent methods like ``` all() ``` and ``` find() ``` .
134+ You can extend this in your repository. Now you will be able to use your repository
135+ in your methods like this.
136+ ``` php
137+ public function index()
138+ {
139+ return $this->user->all();
140+ }
141+ ```
142+ ## Manual binding
143+ By default the package will automatically bind the repository interfaces for you with the repositories so you can
144+ inject the interface into your controllers. If you want to bind manually you can disable
145+ this behaviour by setting the ``` auto_bind_interfaces ``` option to ``` false ``` in ``` config\repository-generator.php ``` .
146+ If the config is not there make sure to publish it first as described in the Installation chapter.
147+
148+ You can add your bindings to your AppServiceProvider or
149+ you can a create a new provider with ``` php artisan make:provider RepositoryServiceProvider ```
150+ (don't forget to add it in ``` config\app.php ``` ) and add the bindings in the ``` register() ``` method, see the example below.
151+
152+ ``` php
153+ <?php
154+
155+ namespace App\Providers;
156+
157+ use App\Repositories\Eloquent\UserRepository;
158+ use App\Repositories\UserRepositoryInterface;
159+ use Illuminate\Support\ServiceProvider;
160+
161+ /**
162+ * Class RepositoryServiceProvider
163+ * @package App\Providers
164+ */
165+ class RepositoryServiceProvider extends ServiceProvider
166+ {
167+ /**
168+ * Register services.
169+ *
170+ * @return void
171+ */
172+ public function register()
173+ {
174+ $this->app->bind(UserRepositoryInterface::class, UserRepository::class);
175+ }
176+ }
177+ ```
62178## Contributing
63179
64180- [ Bhargav Raviya] ( https://github.com/bhargavraviya )
0 commit comments