Laravel抽象类库

时间:2017-03-24 05:32:33

标签: php laravel abstract-class repository-pattern

您好我正在尝试使用抽象类在laravel中实现存储库模式,以获得一些基本功能,这是我的新功能。

我正在

  

解析错误:语法错误,意外' - >' (T_OBJECT_OPERATOR)

在AbstractRepository->行上创建($ request); 当我从存储库中的抽象调用函数时。这是代码

AbstractRepository.php

    <?php

    namespace App\Abstracts;

    use App\Exceptions\NoResourceFoundException;
    use App\Exceptions\ResourceNotFoundException;

    abstract class AbstractRepository
    {

        /**
         * @var Model
         */
        protected $model;

        /**
         * @var array
         */
        public $errors = [];

        /**
         * @param Model $model
         */
        public function __construct(Model $model)
        {
            $this->model = $model;
        }

        public function create(array $data)
        {
            return $this->model->create($data);
        }
   }

ReportRepositoryInterface.php

<?php

namespace App\Interfaces;

interface ReportRepositoryInterface {

  public function createReport (array $data);
}
?>

ReportRepository.php

<?php

namespace App\Repositories;

use App\Interfaces\ReportRepositoryInterface;
use App\Models\Report;
use App\Services\ApiResponse;
use Illuminate\Http\Request;
use App\Abstracts\AbstractRepository;

class ReportRepository implements ReportRepositoryInterface {

  public function createReport(array $request){
    AbstractRepository->create($request);
    return ApiResponse::responseData($request, 'Record successfully created!');
  }
}

?>

ReportsController.php

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Interfaces\ReportRepositoryInterface;
use Illuminate\Support\Facades\Log;

class ReportsController extends Controller
{

  public function __construct(ReportRepositoryInterface $report, Request $request)
  {
    $this->report = $report;
    $this->request = $request;
  }

  public function createReport()
  {
    $data = $this->request->all();
        return $this->report->createReport($data);
  }
}

任何人都可以开导我吗?谢谢

1 个答案:

答案 0 :(得分:2)

当一个类为abstract时,意味着你不能直接创建该类的实例,而不是你可以在不创建类的实例的情况下调用类的方法,那就是{{1 }} 方法。 static类意在扩展。在这种情况下,您的abstract需要修改为:

ReportRepository

请查看abstraction手册和static方法可见性手册。