Support the ongoing development of Laravel.io →
Input IOC Architecture
Last updated 1 year ago.
0

If the parameters in the base controller's constructor are extended, would not the constructor automatically have the extended parameters without declaring?

Also, I know that you could declare constants or variables in your config and it will be global.

I am not sure if this helped any but there you go.

Last updated 1 year ago.
0
<?php
use MyNs\Commander\CommandBus;
use MyNs\Some\SomeRepository;

class SomeController extends BaseController {

    protected $someRepo;

    public function __construct(CommandBus $bus, SomeRepository $someRepo)
    {
        parent::__construct($bus);

        $this->someRepo = $someRepo;
    }

   public function store()
    {
        $command = new Command(...);

        $some = $this->bus->execute($command);
    }
}
Last updated 1 year ago.
0

@damienadermann without redeclaring it. You've just repeated what is in the BaseController.

I actually don't think there is a way to do it because of the way inheritance works and constructor precedence.

The laravel.io forum code doesn't have any solution to it either, so I guess I've already taken the optimization as far as it can go:

https://github.com/LaravelIO/laravel.io/blob/nextVersion/app/c...

and

https://github.com/LaravelIO/laravel.io/blob/nextVersion/app/c...

Last updated 1 year ago.
0

This has indeed nothing to do with Laravel, but just with php itself. You have to give the CommandBus object to the parent constructor, within the constructor itself. The laravel IoC is able to inject constructor properties, yes, but not like this unfortionately.

Would a solution perhaps be to initialize the command bus through a setCommandBus method? Or would there be specific cases where you would need the command bus available in the constructor?

Another solution would be to initialize the CommandBus inside the BaseController's constructor through App:make, instead of through Dependency Injection.

Last updated 1 year ago.
0

You can do this:

<?php
use MyNs\Commander\CommandBus;

class BaseController extends \Controller {

    public $bus;

    public function __construct()
    {
        $this->bus        = App::make('MyNs\Commander\CommandBus');
    }

    ...

Then in your controller

<?php

use MyNs\Some\SomeRepository;

class SomeController extends BaseController {

    protected $someRepo;

    public function __construct(SomeRepository $someRepo)
    {
        parent::__construct();
    }

   public function store()
    {
        $command = new Command(...);

        $some = $this->bus->execute($command);
    }

Hope you still need it :) works

Last updated 1 year ago.
0

Sign in to participate in this thread!

Eventy

Your banner here too?

jhauraw jhauraw Joined 12 May 2014

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2024 Laravel.io - All rights reserved.