首页 文章

Laravel PHP:存储库类不存在

提问于
浏览
0

我是'm still new with using Repositories in Laravel PHP. I have created a repository file and have placed it in a directory called ' Repositories ' witin my project'的根文件夹 . 在我的主页's controller, I have created a ' __construct()' function for the repository but when I try to access this page, I keep getting a ' Class Repositories\VideoRepository does not exist '错误 .

我不太确定为什么即使在我使用'php artisan dump-autoload'之后我仍然会收到此错误 . 我不是将存储库放在正确的文件夹中吗?

Controller(app\OverviewController.php):

<?php

use Controllers\VideosController;

use Models\Video;

use Models\Validators as Validators;

class OverviewController extends BaseController {

/* The Video model */
protected $video;

/* The picture model */
protected $picture;

/* The layout for the Videos and Pictures Overview */
protected $layout = 'layouts.master';

 public function __construct()
 {
    $this->video =   \App::make('Repositories\VideoRepository');
 }

/* List all the videos and stones
Included Pagination for neatness */

 public function index()
 {
    $allpicsvids = Video::paginate(10);
    $this->layout->content = \View::make('overview', array('allpicsvids' => $allpicsvids));
 }


}

Repository(app\repositories\VideoRepository.php):

编辑:将命名空间'app \ repositories'添加到此界面 .

<?php namespace app\repositories;

interface VideoRepository {

public function all();

public function find($id);

public function findOrFail($id);

public function create($input);

public function update($id, $input);

public function delete($id);

public function forceDelete($id);

public function restore($id);

}

Eloquent Repository(app\repositories\EloquentVideoRepository.php):

<?php namespace Repositories;

use Models\Video;

class EloquentVideoRepository implements VideoRepository {

public function all()
{
    return Video::all();
}

public function find($id)
{
    return Video::find($id);
}

public function findOrFail($id)
{
    return Video::findOrFail($id);
}

public function create($input)
{
    return Video::create($input);
}

public function update($id, $input)
{
    $video = Video::find($id);
    $video->video_name = $input['video_name'];
    $video->video_description = $input['video_name'];
    $video->video_edges = $input['video_edges'];
    $video->video_stores = $input['video_stores'];
    $video->video_order = $input['video_order'];
    $video->video_link = $input["video_link"];
    $video->video_height = $input['video_height'];
    $video->video_width = $input['video_width'];
    $video->category = $input['category'];
    $video->video_project = $input['video_project'];
    $video->touch();
    return $video->save();
}

public function delete($id)
{
    $video = Video::find($id);
    return $video->delete();
}

public function forceDelete($id)
{
    $video = Video::find($id);
    return $video->forceDelete();
}

public function restore($id)
{
    $video = Video::withTrashed()->find($id);
    return $album->restore();
}


}

composer.json:

{
"name": "laravel/laravel",
"description": "The Laravel Framework.",
"keywords": ["framework", "laravel"],
"license": "MIT",
"require": {
    "laravel/framework": "4.2.*"
},
"autoload": {
    "classmap": [
        "app/commands",
        "app/controllers",
        "app/models",

       /* Added this line below so that my repositories could be recognized */
        "app/repositories",

        "app/database/migrations",
        "app/database/seeds",
        "app/tests/TestCase.php"
    ]
},
"scripts": {
    "post-install-cmd": [
        "php artisan clear-compiled",
        "php artisan optimize"
    ],
    "post-update-cmd": [
        "php artisan clear-compiled",
        "php artisan optimize"
    ],
    "post-create-project-cmd": [
        "php artisan key:generate"
    ]
},
"config": {
    "preferred-install": "dist"
},
"minimum-stability": "stable"
}

编辑:为我的存储库和 php artisan dump-autoload 添加“ psr-4 ”代码块后,这就是我的' autoload_psr4.php '目前的样子:

<?php

// autoload_psr4.php @generated by Composer

$vendorDir = dirname(dirname(__FILE__));
$baseDir = dirname($vendorDir);

return array(
 'repositories\\' => array($baseDir . '/app/repositories'),
 'Monolog\\' => array($vendorDir . '/monolog/monolog/src/Monolog'),
);

3 回答

  • 2

    听起来它没有被自动加载,你需要将它添加到composer.json .

    您可以将其添加到autoload / classmap数组或查看使用PSR autoloading .

  • 2

    您的界面不在任何特定的命名空间中 . 您的EloquentVideoRepository是 - 因此无法找到该类,因为它们位于同一目录中 .

    如果将接口拉入与实现类相同的命名空间,则很容易修复 .

    另外,psr-4自动加载应该是“Repositories \”:'repositories',除非你想调整文件夹名称的大小 .

  • 1

    您可以通过psr-4加载存储库,而不是尝试将其添加到类映射中:

    在你的作曲家json中:

    "autoload": {
        "classmap": [
            "app/commands",
            "app/controllers",
            "app/models",
    
            "app/database/migrations",
            "app/database/seeds",
            "app/tests/TestCase.php"
        ],
        psr-4: {
            "Repositories\\" : "Repositories"
        }
    },
    

    您还需要在 VideoRepository 文件的顶部添加命名空间 .

    另外,我建议您将 Repositories 目录移动到您的app文件夹中 . 这样,您的自定义代码将全部驻留在框架_398503中,旨在包含您的添加内容 . composer.json psr-4看起来像这样:

    "Repositories\\" : "app\\Repositories"
    

    并且任何Repository文件中的命名空间都是:

    <?php namespace app\Repositories;
    

    我还建议查看Laracast video on PSR-4 autoloading . 它非常物有所值 .

相关问题