在Laravel 5 Controller中使用自定义功能

时间:2015-08-25 13:37:40

标签: function laravel controller laravel-5

我正在使用Laravel 5,我只想知道如何在我的控制器中使用或声明自定义功能。我有

class UploadController extends Controller
{
  function on_request_done($content, $url, $ch, $search) {

    $httpcode = curl_getinfo($ch, CURLINFO_HTTP_CODE);    
    if ($httpcode !== 200) {
        print "Fetch error $httpcode for '$url'\n";
        return;
    }
    $responseobject = json_decode($content, true);
    if (empty($responseobject['responseData']['results'])) {
        print "No results found for '$search'\n";
        return;
    }
    print "********\n";
    print "$search:\n";
    print "********\n";
    $allresponseresults = $responseobject['responseData']['results'];
    foreach ($allresponseresults as $responseresult) {
        $title = $responseresult['title'];
        print "$title\n";
    }
  }
}

每当我在其中一个函数中调用on_request_done时,请说,

public function getParallelApi()
{
    define ('SEARCH_URL_PREFIX', 'http://ajax.googleapis.com/ajax/services/search/web?v=1.0&rsz=large&filter=0');
    $terms_list = array(
        "John", "Mary",
        "William", "Anna",
        "James", "Emma",
        "George", "Elizabeth",
        "Charles", "Margaret",
        "Frank", "Minnie",
        "Joseph", "Ida",
        "Henry", "Bertha",
        "Robert", "Clara",
        "Thomas", "Alice",
        "Edward", "Annie",
        "Harry", "Florence",
        "Walter", "Bessie",
        "Arthur", "Grace",
        "Fred", "Ethel",
        "Albert", "Sarah",
        "Samuel", "Ella",
        "Clarence", "Martha",
        "Louis", "Nellie",
        "David", "Mabel",
        "Joe", "Laura",
        "Charlie", "Carrie",
        "Richard", "Cora",
        "Ernest", "Helen",
        "Roy", "Maude",
        "Will", "Lillian",
        "Andrew", "Gertrude",
        "Jesse", "Rose",
        "Oscar", "Edna",
        "Willie", "Pearl",
        "Daniel", "Edith",
        "Benjamin", "Jennie",
        "Carl", "Hattie",
        "Sam", "Mattie",
        "Alfred", "Eva",
        "Earl", "Julia",
        "Peter", "Myrtle",
        "Elmer", "Louise",
        "Frederick", "Lillie",
        "Howard", "Jessie",
        "Lewis", "Frances",
        "Ralph", "Catherine",
        "Herbert", "Lula",
        "Paul", "Lena",
        "Lee", "Marie",
        "Tom", "Ada",
        "Herman", "Josephine",
        "Martin", "Fanny",
        "Jacob", "Lucy",
        "Michael", "Dora",
    );

    if (isset($argv[1])) {
    $max_requests = $argv[1];
    } else {
        $max_requests = 10;
    }

    $curl_options = array(
        CURLOPT_SSL_VERIFYPEER => FALSE,
        CURLOPT_SSL_VERIFYHOST => FALSE,
        CURLOPT_USERAGENT, 'Parallel Curl test script',
    );

    $parallel_curl = new ParallelCurl($max_requests, $curl_options);

    foreach ($terms_list as $terms) {
        $search = '"'.$terms.' is a"';
        $search_url = SEARCH_URL_PREFIX.'&q='.urlencode($terms);
        $parallel_curl->startRequest($search_url, 'on_request_done', $search);
    }


    $parallel_curl->finishAllRequests();

    //return "Helloo";
}

我得到了

call_user_func() expects parameter 1 to be a valid callback, function 'on_request_done' not found or invalid function name

我的路线

Route::get('/parallelapi', 'UploadController@getParallelApi');

请注意,getParallelApi()也在UploadController之下。

1 个答案:

答案 0 :(得分:0)

问题是你传递了函数的名称,但它不知道该函数存在于哪个类中。你应该能够通过传递一个数组作为你的回调来做到这一点,第一个项是你的一个实例控制器和第二项是函数的名称。

$parallel_curl->startRequest($search_url, [$this, 'on_request_done'], $search);