【问题标题】:Edit parent variable编辑父变量
【发布时间】:2017-08-08 07:48:28
【问题描述】:

所以我试图从子类编辑属性category,但由于某些原因,我得到了一个错误。这我知道为什么,因为需要有 2 个参数,但在父类中已经设置了一个。

代码:

孩子

class RestaurantController extends CompanyController
{
    public function __construct(){
        parent::__construct(null, "restaurant");
        //$this->category = "restaurant";
    }
    public function getCompany($slug){
        $company = parent::index($slug);
        return view("restaurant.profile")->withInformation($company);
    }
} 

父母

class CompanyController extends Controller 
{
    protected $company;
    public $category;

    public function __construct(CompanyRepository $company, $category = '')
    {
        $this->category = $category;
        $this->company = $company;
    }

    public function index($slug)
    {
        $company = $this->company->getCompany($this->category, $slug);

        return compact('company');
    }
}

现在我需要知道如何解决它。

编辑1

我遇到的错误

类型错误:传递给 App\Http\Controllers\CompanyController::__construct() 的参数 1 必须是 App\Repositories\CompanyRepository 的实例,给定 null,在 /var/www/atify.info/dev-system 中调用/app/Http/Controllers/RestaurantController.php 第 16 行

edit2

这个孩子

class RestaurantController extends CompanyController
{
    public function getCompany($slug){
        $company = parent::index($slug);
        return view("restaurant.profile")->withInformation($company);
    }
} 

父母

use App\Repositories\CompanyRepository;
class CompanyController extends Controller 
{
    protected $company;

    public function __construct(CompanyRepository $company)
    {
        $this->company = $company;
    }

    public function index($slug)
    {
        $company = $this->company->getCompany($slug);

        return compact('company');
    }
}

所以我需要一个类别(用于额外检查。否则您可能会在另一个具有错误功能的孩子中检索公司),因为我有很多孩子,每个孩子都有特殊的功能

【问题讨论】:

  • 请包含您遇到的错误。
  • @jfadich 已编辑!
  • 您将null 作为第一个参数传递给parent::__construct(null, "restaurant");,但父方法要求您将CompanyRepository 对象作为第一个参数传递....您需要实例化一个CompanyRepository 要传递的对象而不是那个 null
  • @MarkBaker True 我知道 null 中断,但 CompanyRepository 设置在 CompanyController 中。是不是有某种东西会忽略第一个论点。有点像默认值?

标签: php laravel oop laravel-5.2


【解决方案1】:

我认为这是您想要的子类:

class RestaurantController extends CompanyController
{
    public $category = 'restuarant';

    public function __construct(CompanyRepository $company){
        parent::__construct($company, $this->category);
    }

    public function getCompany($slug){
        $company = parent::index($slug);
        return view("restaurant.profile")->withInformation($company);
    }
} 

如果这就是你在构造函数中所做的一切,你可以消除子构造函数并像这样更改父构造函数。

 public function __construct(CompanyRepository $company, $category = null)
    {
      if( $category ){
        $this->category = $category;
      }
      $this->company = $company;
    }

那么每个子类只要设置类别属性

class ChildController extends CompanyController
{
    public $category = 'Child';
}

【讨论】:

  • 那行得通!但是现在每次我调用扩展 CompanyController 我都必须在构造函数中添加它吗?
  • 不是真的,你可以只设置类别并在父构造函数中处理它(我会更新答案)
猜你喜欢
  • 2020-06-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-07-11
  • 2013-06-25
  • 2013-11-20
  • 2019-09-15
  • 2016-09-21
相关资源
最近更新 更多