在 Laravel 4 中调用控制器

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15205239/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 02:23:50  来源:igfitidea点击:

Call a controller in Laravel 4

laravellaravel-4

提问by Thomas Clackston

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

在 Laravel 3 中,您可以使用 Controller::call 方法调用控制器,如下所示:

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

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?

我查看了 L4 中的 Controller 类,发现这个方法似乎取代了旧方法:callAction()。虽然它不是静态方法,但我无法让它工作。可能不是正确的方法吗?

How can I do this in Laravel 4?

我怎样才能在 Laravel 4 中做到这一点?

回答by Neto

You may use IoC.

您可以使用 IoC。

Try this:

尝试这个:

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

Eg:

例如:

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

and you may also give params:

你也可以给参数:

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

回答by Laurent Van Winckel

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?

如果我理解正确,您正在尝试构建一个以 API 为中心的应用程序,并希望在您的 Web 应用程序内部访问 API,以避免发出额外的 HTTP 请求(例如使用 cURL)。那是对的吗?

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.

请注意,您需要使用通常用于从外部访问 API 的 uri 路由,而不是指定 controller@method 目标。

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

更好的是,您现在可以指定请求应该响应的 HTTP 动词。

回答by dani24

Like Neto said you can user:

就像 Neto 说的那样,您可以使用:

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

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

但是要发送例如带有额外数据的 POST,您可以在之前使用“合并”方法:

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

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

It works for me!

这个对我有用!

bye

再见

回答by Gabriel Koerich

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));
});

回答by Jeto

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

Laurent 的解决方案有效(尽管您需要一个前导/,并且$params您传递的Request::create是 GET 参数,而不是 Laravel 处理的那些(必须将它们api/items/放在示例中)。

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...

我不敢相信没有更简单的方法可以做到这一点(不是说它很难,但对我来说看起来有点黑客)。基本上,Laravel 4 没有提供一种使用回调函数将路由映射到控制器的简单方法?严重地?这是世界上最常见的事情......

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.

希望我错过了一些明显的东西。

回答by PHP Connect

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

For laravel 5.1

对于 Laravel 5.1

回答by rebduvid

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

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

And then in the Routes file or in the View:

然后在路由文件或视图中:

ControllerClassName::functionNAME(params);