在同一控制器中调用其他函数?

我有这个控制器,以及 function read($q)返回错误 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;
}


}

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

213019 次浏览

Try:

return $this->sendRequest($uri);

Since PHP is not a pure object-oriented 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.

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

To call a function inside a same controller in any laravel version follow as bellow

$role = $this->sendRequest('parameter');
// sendRequest is a public function

You can call the method with $this->methodNameYouWantToCall($thing_you_want_to_pass).

In Your case you can something like this......

return $this->sendRequest($uri);