在同一个控制器中调用其他功能?

时间:2022-12-01 23:25:12

I'm using Laravel. Here is a class I'm working on:

我正在使用Laravel。这是我正在研究的课程:

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

}

The line:

这条线:

return sendRequest($uri);

Renders the error: Call to undefined function sendRequest()

呈现错误:调用未定义的函数sendRequest()

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.

我假设它是因为我以错误的方式引用该函数,但我找不到任何解释如何做到这一点。

2 个解决方案

#1


86  

Try:

尝试:

return $this->sendRequest($uri);

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.

由于PHP不是纯粹的Object-Orieneted语言,它将sendRequest()解释为尝试调用全局定义的函数(例如,就像nl2br()),但由于你的函数是类的一部分('InstagramController'),你需要使用$ this将解释器指向正确的方向。

#2


3  

Yes. Problem is in wrong notation. Use:

是。问题是错误的表示法。使用:

$this->sendRequest($uri)

Instead. Or

代替。要么

self::staticMethod()

for static methods. Also read this for getting idea of OOP - http://www.php.net/manual/en/language.oop5.basic.php

对于静态方法。另请阅读本文以了解OOP - http://www.php.net/manual/en/language.oop5.basic.php

#1


86  

Try:

尝试:

return $this->sendRequest($uri);

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.

由于PHP不是纯粹的Object-Orieneted语言,它将sendRequest()解释为尝试调用全局定义的函数(例如,就像nl2br()),但由于你的函数是类的一部分('InstagramController'),你需要使用$ this将解释器指向正确的方向。

#2


3  

Yes. Problem is in wrong notation. Use:

是。问题是错误的表示法。使用:

$this->sendRequest($uri)

Instead. Or

代替。要么

self::staticMethod()

for static methods. Also read this for getting idea of OOP - http://www.php.net/manual/en/language.oop5.basic.php

对于静态方法。另请阅读本文以了解OOP - http://www.php.net/manual/en/language.oop5.basic.php