如何从 Laravel 中的其他控制器访问控制器?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18231154/
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
How to access controller from other controller in Laravel?
提问by Himmators
Maybe I'm doing this wrong alltogether. But I'm doing my first MVC in Laravel. Here is my setup.
也许我完全做错了。但我正在 Laravel 中进行我的第一个 MVC。这是我的设置。
The app is gonna download data from varoius sources and save to a database for output.
该应用程序将从各种来源下载数据并保存到数据库以供输出。
I have two controllers. One that saves data to the database and one that downloads data from instagram. In the Instagram-controller. I currently just output the data. I would like to save it using my save-controller instead.
我有两个控制器。一种将数据保存到数据库,另一种从 Instagram 下载数据。在 Instagram 控制器中。我目前只是输出数据。我想用我的保存控制器来保存它。
Instagram controller:
Instagram控制器:
class InstagramController extends BaseController {
public function read($q)
{
$client_id = 'ea7bee895ef34ed08eacad639f515897';
$uri = 'https://api.instagram.com/v1/tags/'.$q.'/media/recent?client_id='.$client_id;
$response = $this->sendRequest($uri);
print_r(json_decode($response));
//return json_decode($response);
}
public function sendRequest($uri){
$curl = curl_init($uri);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 0);
$responses = curl_exec($curl);
$responses = json_decode($responses);
curl_close($curl);
echo '<pre>';
foreach ($responses->data as $data) {
//0/{postid}/{url}/{author}/{content}
$type = 0;
$postid = $data->id;
$url = $data->link;
$author = $data->user->username;
$content = $data->images->standard_resolution->url;
echo $type.' '.$postid.' '.$url.' '.$author.' '.$content.'<br />';
}
//var_dump($responses->data);
echo '</pre>';
//return $response;
}
}
Save Controller:
保存控制器:
class PostController extends BaseController {
public function save($type, $postid, $url, $author, $content)
{
$post = new Post;
$post->type = $type;
$post->postid = $postid;
$post->url = $url;
$post->author = $author;
$post->content = $content;
try
{
$post->save();
echo $post;
}catch(Exception $e){
throw new Exception( 'Already saved', 0, $e);
}
//
}
}
回答by Atrakeur
Controllers are mapped to actions (one route perform one action). You can call a controller by calling his route.
控制器映射到动作(一个路由执行一个动作)。您可以通过调用他的路由来调用控制器。
Because getting images, and saving them are part of the same action (ie: same route), I recommend you to move that code to a library or helper (or why not a model, because you are retrieving and saving data)
因为获取图像并保存它们是同一操作的一部分(即:相同的路线),我建议您将该代码移至库或助手(或者为什么不是模型,因为您正在检索和保存数据)
Anyway, I don't get the reason why you are doing that in controllers. Controllers usually perform operations between models and the views. Fetching data from a webservice belong more to models than to controllers. In fact, I really recommend you to move that code to a library to be reusable later. Your controller must only have the responsibility of calling the library's read method to read the image, then saving it locally using a model.
无论如何,我不明白你在控制器中这样做的原因。控制器通常执行模型和视图之间的操作。从网络服务中获取数据更多地属于模型而不是控制器。事实上,我真的建议您将该代码移动到一个库中以便以后可以重用。您的控制器必须只负责调用库的 read 方法来读取图像,然后使用模型将其保存在本地。