Laravel - 控制器调用应用程序中的存储库

时间:2017-10-19 08:18:07

标签: php laravel laravel-5 repository

我正在使用laravel Modules和Repository。我在App和Controller模块中创建了Repository。但是当控制器调用Repository时,它会报告" ReflectionException 类Modules \ Product \ Http \ Controllers \ ProductEntryRepository不存在"。

但App \ Reppsitory中的ProductEntryRepository但在Controlller中出错。 enter image description here     

namespace Modules\Product\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Http\Response;
use Illuminate\Routing\Controller;

use \App\Repositories\ProductCategoryRepository;
use \App\Repositories\ProductCategoryEntryRepository;

class ProductCategoryController extends Controller
{
    /**
     * @var PostRepository
     */
    protected $request;
    protected $product_category;
    protected $product_category_entry;

    public function __construct(Request $request, ProductCategoryRepository $product_category, ProductEntryRepository $product_category_entry){
        $this->request = $request;
        $this->product_category = $product_category;
        $this->product_category_entry = $product_category_entry;
    }

    /**
     * Display a listing of the resource.
     * @return Response
     */
    public function index()
    {
        return view('product::index');
    }

1 个答案:

答案 0 :(得分:1)

您只需在控制器顶部添加use \App\Repositories\ProductEntryRepository;即可:)

namespace Modules\Product\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Http\Response;
use Illuminate\Routing\Controller;

use \App\Repositories\ProductCategoryRepository;
use \App\Repositories\ProductCategoryEntryRepository;
use \App\Repositories\ProductEntryRepository;

class ProductCategoryController extends Controller
{
  //......
}

或者我认为你的意思是ProductCategoryEntryRepository而不是ProductEntryRepository在构造!!

public function __construct(Request $request, ProductCategoryRepository $product_category, ProductCategoryEntryRepository $product_category_entry){
    $this->request = $request;
    $this->product_category = $product_category;
    $this->product_category_entry = $product_category_entry;
}