在同一个控制器中调用其他函数? [英] Calling other function in the same controller?

查看:63
本文介绍了在同一个控制器中调用其他函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个控制器,并且function read($q)返回错误Call to undefined function sendRequest()

I've this controller, and the function read($q) return error Call to undefined function sendRequest()

<?php

class InstagramController extends BaseController {

/*
|--------------------------------------------------------------------------
| Default Home Controller
|--------------------------------------------------------------------------
|
| You may wish to use controllers instead of, or in addition to, Closure
| based routes. That's great! Here is an example controller method to
| get you started. To route to this controller, just add the route:
|
|   Route::get('/', 'HomeController@showWelcome');
|
*/

public function read($q)
{
    $client_id = 'ea7bee895ef34ed08eacad639f515897';

    $uri = 'https://api.instagram.com/v1/tags/'.$q.'/media/recent?client_id='.$client_id;
    return sendRequest($uri);
}

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);
    $response = curl_exec($curl);
    curl_close($curl);
    return $response;
}

}

我认为这是因为我以错误的方式引用了该函数,但找不到有关如何执行此操作的任何解释.

I'm assuming it's because I'm referencing the function in the wrong manner, but I can't find any explanations for how to do it.

推荐答案

尝试:

return $this->sendRequest($uri);

由于PHP并非纯粹的对象继承语言,因此它将sendRequest()解释为尝试调用全局定义的函数(例如,像nl2br()一样),但是由于您的函数是类的一部分('InstagramController' '),则需要使用$this将解释器指向正确的方向.

Since PHP is not a pure Object-Orieneted language, it interprets sendRequest() as an attempt to invoke a globally defined function (just like nl2br() for example), but since your function is part of a class ('InstagramController'), you need to use $this to point the interpreter in the right direction.

这篇关于在同一个控制器中调用其他函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆