mercredi 2 mars 2016

How to setup "Loosely Coupled" design pattern in "Laravel"

I am very new to “Laravel” world so I could be wrong in very basics.

I am trying to setup a project with “Loosely Coupled” design pattern. I have looked into following tutorials:

http://ift.tt/1d1b2iO

http://ift.tt/21CNbhe

and also looked into following threads of SO:

Laravel 5 - Interface is not instantiable

Laravel ReflectionException error : Repository doesn't exist

but couldn’t got the results.

So here is my application structure:

`

    -app

       -Providers
                 IOCProvider
       -Repositories
           -Abstracts
                     IUserRepository
           -Concrete
                     UserRepository

`

Here is "IUserRepository": `

  <?php
   namespace App\Repositories\Abstracts;

     interface IUserRepository {
           public function getUserByID($ID);

        }

`

And UserRepository:

`

    <?php
      namespace App\Repositories\Concrete;
      use App\Repositories\Abstracts;
      use app\Model;
      class UserRepository implements IUserRepository {
         public function getUserByID($id){
           $user=  User::find(1);
           return $user;
         }
     }

`

IOCProvider.php:

`

    <?php
     namespace App\Providers;
     use Illuminate\Support\ServiceProvider;

      class IocProvider extends ServiceProvider {
           public function register()
           {

             $this->app->bind('App\Repositories\Abstracts\IUserRepository', 'App\Repositories\Concrete\UserRepository');

          }
      }

`

And composer.json

`

   "autoload": {
    "classmap": [
        "database"
    ],
    "psr-4": {
        "App\\": "app/",
        "App\\Repositories\\Abstracts":"app/Repositories/Abstract",
        "App\\Repositories\\Concrete":"app/Repositories/Concrete"
    }
},

`

Now I am getting this error:

`

   ReflectionException in Container.php line 798:
     Class App\Repositories\Abstracts\IUserRepository does not exist

`

Please guide me what am I doing wrong.

Your help would really be appreciated.

Thanks.



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire