我在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);
}
?>
UserController.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');
// }
}
答案 0 :(得分:2)
您的AbstractEloquentRepository.php
文件中没有名称空间。
答案 1 :(得分:1)
在终端中使用php artisan dump-autoload
和composer dump-autoload
重新生成所有类文件。愿这可以解决你的问题。
答案 2 :(得分:0)
尝试一次绑定一个......
$this->app->bind('Repositories\User\UserRepository', function(){
return new \Repositories\User\UserRepository();
}