Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Calling other function in the same controller?

Tags:

php

laravel

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.

like image 374
Himmators Avatar asked Jul 25 '13 14:07

Himmators


People also ask

Can a controller call another controller laravel?

Laravel gives you lots of other other options too i.e. event, jobs, model oberservers etc. If you have two controllers that need to perform the same functionality, then one option is to use a trait which you can then include in both controllers. But you should never need to transfer from one controller to another.

How can we call one controller function from another controller in PHP?

Show activity on this post. Create new Helper (e.g PermissionHelper. php ) then move the funtion to it and call it where you want using : PermissionHelper::permission();


2 Answers

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.

like image 67
haim770 Avatar answered Oct 13 '22 04:10

haim770


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

like image 22
QArea Avatar answered Oct 13 '22 06:10

QArea