0

How can I call a controller method manually specifying some input parameters yet still have method injection work for the parameters not specified (example below).

routes.php

$myController->index($id);

controllers/MyControllerOne.php

class MyControllerOne
{

    public function index($id, MyRequest $request)
    {

    }

}

extra information The reason I need this is because I have special information in my routes that determines which controller should be executed such as /myroute/{data}/{id}. It's a bit unorthodox but it's a necessary evil given the scope of our system. Once I resolve within my routes which controller needs to be called I then want to call the method on that controller. $controllerInstance->index($id).

user391986
  • 29,536
  • 39
  • 126
  • 205
  • 2
    If it's only for Request, I think you could manually pass this `$this->app->make('Request')`, like so `$controllerIntance->index($id, $this->app->make('Request'))` – martinczerwi May 19 '15 at 23:24
  • I've made it an answer. If you don't mind, I'd be happy if you accept it. – martinczerwi May 20 '15 at 08:06

1 Answers1

2

If it's only for Request, I think you could manually pass this $this->app->make('Request'), like so

$controllerIntance->index($id, $this->app->make('Request'))

Note that you actually don't have to inject Request, since you might as well use App::make inside of your controller. But I'm not sure how good this decision is in case of testability and coupling.

For more info:

This function resolves 'Request' out of the container, that is instantiates or returns an existing instance (depending of the type of service provider).

Using make is described here http://laravel.com/docs/5.0/container (see "Resolving"). I also found this answer helpful, to understanding how the container works https://stackoverflow.com/a/25798288/1627227

Community
  • 1
  • 1
martinczerwi
  • 2,837
  • 23
  • 23