programing

같은 컨트롤러에서 다른 기능을 호출하고 있습니까?

randomtip 2023. 1. 5. 20:33
반응형

같은 컨트롤러에서 다른 기능을 호출하고 있습니까?

이 컨트롤러가 있고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;
}

}

제가 잘못된 방법으로 기능을 참조하고 있기 때문이라고 생각합니다만, 그 방법에 대한 설명을 찾을 수 없습니다.

시험:

return $this->sendRequest($uri);

PHP는 순수한 객체 지향 언어가 아니기 때문에 다음과 같이 해석합니다.sendRequest()글로벌하게 정의된 함수를 호출하기 위한 시도로서nl2br()예를 들어, 함수가 클래스의 일부이기 때문에(InstagramController)를 사용해야 합니다.$this통역을 올바른 방향으로 인도할 수 있습니다.

네. 문제는 잘못된 표기법입니다.용도:

$this->sendRequest($uri)

대신.또는

self::staticMethod()

를 참조해 주세요.OOP에 대한 아이디어를 얻으려면 이 페이지를 읽어보십시오 - http://www.php.net/manual/en/language.oop5.basic.php

메서드는 다음과 같이 호출할 수 있습니다.$this->methodNameYouWantToCall($thing_you_want_to_pass).

당신의 경우 이런 식으로 할 수 있습니다...

return $this->sendRequest($uri);

라라벨 버전에서 동일한 컨트롤러 내부의 함수를 호출하려면 다음과 같이 하십시오.

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

언급URL : https://stackoverflow.com/questions/17861412/calling-other-function-in-the-same-controller

반응형