Call a controller in Laravel 4

If I understand right, you are trying to build an API-centric application and want to access the API internally in your web application to avoid making an additional HTTP request (e.g. with cURL). Is that correct?

You could do the following:

$request = Request::create('api/items', 'GET', $params);
return Route::dispatch($request)->getContent();

Notice that, instead of specifying the controller@method destination, you'll need to use the uri route that you'd normally use to access the API externally.

Even better, you can now specify the HTTP verb the request should respond to.


Like Neto said you can user:

App::make('HomeController')->getIndex($params);

But to send for instance a POST with extra data you could use "merge" method before:

$input = array('extra_field1' => 'value1', 'extra_field2' => 'value2');
Input::merge($input);

return App:make('HomeController')->someMethodInController();

It works for me!

bye


You may use IoC.

Try this:

App::make($controller)->{$action}();

Eg:

App::make('HomeController')->getIndex();

and you may also give params:

App::make('HomeController')->getIndex($params);