简体   繁体   中英

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://vegibit.com/laravel-repository-pattern/

http://heera.it/laravel-repository-pattern#.Vtaepfl97IU

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.

In your UserRepository try changing this : use App\\Repositories\\Abstracts to this : use App\\Repositories\\Abstracts\\IUserRepository

On a side note, as you are new to Laravel - go to Laracasts.com. There is no better laravel resource for tutorials then that site. It is the defacto recommendation for all new laravel people. (For example hre is a good set of free beginning laravel video tutorials: https://laracasts.com/series/laravel-5-from-scratch )

Good luck

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM