Call a controller in Laravel 4

2019-01-21 02:44发布

问题:

In Laravel 3, you could call a controller using the Controller::call method, like so:

Controller::call('api.items@index', $params);

I looked through the Controller class in L4 and found this method which seems to replace the older method: callAction(). Though it isn't a static method and I couldn't get it to work. Probably not the right way to do it?

How can I do this in Laravel 4?

回答1:

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.



回答2:

Hey guys 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);


回答3:

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



回答4:

This is not the best way, but you can create a function to do that:

function call($controller, $action, $parameters = array())
{
    $app = app();
    $controller = $app->make($controller);
    return $controller->callAction($app, $app['router'], $action, $parameters);
}

Route::get('/test', function($var = null) use ($params)
{
    return call('TestController', 'index', array($params));
});


回答5:

Laurent's solution works (though you need a leading / and the $params you pass to Request::create are GET params, and not those handled by Laravel (gotta put them after api/items/ in the example).

I can't believe there isn't an easier way to do this though (not that it's hard, but it looks kinda hackish to me). Basically, Laravel 4 doesn't provide an easy way to map a route to a controller using a callback function? Seriously? This is the most common thing in the world...

I had to do this on one of my projects:

Route::controller('players', 'PlayerController');

Route::get('player/{id}{rest?}', function($id)
{
    $request = Request::create('/players/view/' . $id, 'GET');
    return Route::dispatch($request)->getContent();
})
->where('id', '\d+');

Hope I'm missing something obvious.



回答6:

$request = Request::create('common_slider', 'GET', $parameters);
return  Controller::getRouter()->dispatch($request)->getContent();

For laravel 5.1



回答7:

It's an Old question. But maybe is usefull. Is there another way.

In your controller: You can declare the function as public static

public static function functioNAME(params)
{
    ....
}

And then in the Routes file or in the View:

ControllerClassName::functionNAME(params);