2. The elements of this language are entities called patterns. Each pattern describes
a problem that occurs over and over again in our environment, and then
describes the core of the solution to that problem, in such a way that you can use
this solution a million times over, without ever doing it the same way twice.
— Christopher Alexander
6. class CommonController extends
BaseController
{
public function all()
{
$contacts = Contacts:all();
return
View::make(‘contacts.all’,
compact($contacts));
}
}
So it’s the way right ?
Common Controller
1. Tied to eloquent.
2. Not easily testable.
3. Maintenance is tough.
4. Bugs.
5. bla bla bla bla
8. Repositories
1. Repository design pattern is actually simple. Its just the addition of another
layer into your system. The Service Layer.
2. By this you add another layer to communicate with your database and
other thing instead of controller.
3. No need to write multiple codes.
4. Easily changeable DB.
5. Less headache.
10. Repository Controller
class CommonController extends BaseController
{
public function all()
{
$contactsRepo = new ContactRepository();
$contacts = $contactsRepo->all();
return View::make(‘contacts.list’, compact($contacts));
}
}
11. Project Structure
- app
-- lib
--- Repository
---- Storage
----- Common
Interface Class : CommonRepository.php
Eloquent Class Implemented from interface : EloquentCommonRepository.php
12. Creating the service provider to bind two repositories together
<?php namespace RepositoryStorage;
use IlluminateSupportServiceProvider;
class StorageServiceProvider extends ServiceProvider
{
public function register() {
$this->app->bind( RepositoryStorageUserCommonRepository,
RepositoryStorageCommonEloquentCommonRepository );
}
}
Finally in the providers listing you need to add it
'providers' => array( // -- RepositoryStorageStorageServiceProvider' ),
14. What if we change DB ?
Switching to any new database or any changes are simple now.
$this->app->bind( ‘RepositoryStorageCommonCommonRepository',
‘RepositoryStorageCommonMongoCommonRepository' );
15. I don’t have more to say ;)
Ways to connect with me
@samirpdl
www.samirpdl.com.np
samir@samirpdl.com.np