【问题标题】:"Class App\Repositories\TodoInterface does not exist" Laravel 5.5“类 App\Repositories\TodoInterface 不存在” Laravel 5.5
【发布时间】:2019-12-05 21:41:35
【问题描述】:

我正在尝试在我的应用程序结构中实现存储库。

我在这里找不到问题。我收到以下错误消息:

反射异常 类 App\Repositories\TodoInterface 不存在

我的文件夹结构如下:

  ->app
    ->Repositories
         ->TodoInterface.php
         ->EloquentTodo.php

我的 TodoInterface.php

<?php

namespace App\Repositories;

interface TodoInterface {

public function getAll();

}

我的 EloquentTodo.php

<?php

namespace App\Repositories;
use App\Todo;
class EloquentTodo implements TodoInterface
{
/**
 * @var Todo
 */
private $model;
/**
 * EloquentTodo constructor.
 * @param Todo $model
 */
public function __construct(Todo $model)
{
    $this->model = $model;
}
public function getAll()
{
    return $this->model->all();
}   
}

我的 AppServiceProvide.php

<?php

namespace App\Providers;

use App\Repositories\EloquentTodo;
use App\Repositories\TodoInterface;
use Illuminate\Support\ServiceProvider;

class AppServiceProvider extends ServiceProvider
{
/**
 * Bootstrap any application services.
 *
 * @return void
 */
public function boot()
{
    //
}

/**
 * Register any application services.
 *
 * @return void
 */
public function register()
{
    $this->app->bind(
            TodoInterface::class,
            EloquentTodo::class
        );
}
}

我的 composer.json

"autoload": {
    "classmap": [
        "database/seeds",
        "database/factories"
    ],
    "psr-4": {
        "App\\": "app/",
        "Repositories\\": "app/Repositories/"
    }
},

我的 TodoController.php

<?php

namespace App\Http\Controllers;

use App\Http\Requests;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Repositories\TodoInterface;

class TodoController extends Controller
{
//
private $todo;

public function __construct(TodoInterface $todo) {

  $this->todo = $todo;

}

public function getAllTodos() {
  return $this->todo->getAll();
}
}

我的路线 - web.php

Route::get('my-todos','TodoController@getAllTodos');

谁能帮帮我。

【问题讨论】:

  • 我觉得你需要实现接口
  • @Priya 在“EloquentTodo.php”中实现接口。
  • 对不起,是我的错误
  • 你想要执行以下命令 php artisan config:cache , composer dumpautoload
  • @RavindraBhanderi 谢谢你现在的工作

标签: php laravel laravel-5.5


【解决方案1】:

在你的 EloquentTodo.php 添加use App\Repositories\TodoInterface;

见下文

<?php

namespace App\Repositories;
use App\Todo;
use App\Repositories\TodoInterface;
class EloquentTodo implements TodoInterface
{
/**
 * @var Todo
 */
private $model;
/**
 * EloquentTodo constructor.
 * @param Todo $model
 */
public function __construct(Todo $model)
{
    $this->model = $model;
}
public function getAll()
{
    return $this->model->all();
}   
}

【讨论】:

    【解决方案2】:

    您可以尝试以下命令:

    composer dump-autoload
    php artisan cache:clear
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-03-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-12-29
      • 1970-01-01
      • 1970-01-01
      • 2018-09-27
      相关资源
      最近更新 更多