【问题标题】:Laravel Class not found + Abstract Class未找到 Laravel 类 + 抽象类
【发布时间】:2014-12-19 04:41:20
【问题描述】:

我在 laravel 中使用抽象类时遇到问题。我只是按照本教程http://culttt.com/2014/03/17/eloquent-tricks-better-repositories 进行操作,但是当我执行代码时出现错误“找不到类”。

我已经在服务提供者中绑定了类,这是我的代码

RepositoriesServiceProvider.php

<?php namespace Repositories;

use Illuminate\Support\ServiceProvider;

class RepositoriesServiceProvider extends ServiceProvider {

  public function register()
   {
    $this->app->bind(
   'Repositories\User\UserRepository',
   'Repositories\User\EloquentUserRepository',
   'Repositories\User\AbstractEloquentRepository'

   );
  }

}

?>

EloquentUserRepository.php

namespace Repositories\User;

use User;

class EloquentUserRepository extends AbstractEloquentRepository implements UserRepository{

    protected $model;

    public function __construct(User $model){

        $this->model = $model;

    }


    public function find($id){

        return User::find($id);

    }


    public function create($input){

        return User::create($input);

    }

}

?>

AbstractEloquentRepository.php

<?php 

  abstract class AbstractEloquentRepository{

    public function all(){

        return $this->model->all();

    }


}

?>

UserRepository.php

<?php

 namespace Repositories\User;

 interface UserRepository{

    public function all();

    public function find($id);

    public function create($input);
}

?>

用户控制器.php

<?php
  use Repositories\User\UserRepository as User;

  class UserController extends BaseController {

    /*
    |--------------------------------------------------------------------------
    | Default Home Controller
    |--------------------------------------------------------------------------
    |
    | You may wish to use controllers instead of, or in addition to, Closure
    | based routes. That's great! Here is an example controller method to
    | get you started. To route to this controller, just add the route:
    |
    |   Route::get('/', 'HomeController@showWelcome');
    |
    */


    public function __construct(User $user){

        $this->user = $user;

    }


    /**
     * Display a listing of the resource.
     *
     * @return Response
     */

    public function index()
    {
      return $this->user->all();
    }

    // public function showWelcome()
    // {
    //  return View::make('hello');
    // }






}

【问题讨论】:

标签: php laravel-4


【解决方案1】:

AbstractEloquentRepository.php 文件中没有命名空间。

【讨论】:

    【解决方案2】:

    在终端中使用php artisan dump-autoloadcomposer dump-autoload 重新生成所有类文件。愿这能解决你的问题。

    【讨论】:

    • 我收到了这个错误。无法实例化接口 Repositories\User\UserRepository
    【解决方案3】:

    尝试一次绑定一个...

    $this->app->bind('Repositories\User\UserRepository', function(){
       return new \Repositories\User\UserRepository();
    }
    

    【讨论】:

    • 我试过了,没有运气。我收到此错误消息“无法实例化接口 Repositories\User\UserRepository”
    • 您需要向我们展示您的控制器和类代码
    猜你喜欢
    • 2018-05-03
    • 2015-07-14
    • 1970-01-01
    • 2015-07-07
    • 2017-01-01
    • 2014-07-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多