In my controller function I am using a require
statement to include a file, like,
require app_path().'/plivo/plivo.php';
After this statement, when I try to redirect from this controller using the statement,
return Redirect::back()->with('success', 'Note added successfully');
It gives me an error Call to undefined method Redirect::back()
How can I redirect from this function.
This is my full code,
public function sendSMS(){
require app_path().'/plivo/plivo.php';
$auth_id = "XXXXXXXXXXXX";
$auth_token = "XXXXXXXXXXXXXXXXXXXXX";
$p = new \RestAPI($auth_id, $auth_token);
$params = array(
'src' => '1XX7XX0',
'dst' => '91XXXXXXXXX7',
'text' => 'Test SMS',
'method' => 'POST'
);
$response = $p->send_message($params);
return Redirect::back()->with('success', 'Note added successfully');
}
Best How To :
This answer assumes that plivio.php
is from this git repo.
The issue is that the plivo.php
library defines a Redirect
class in the global namespace. Because of this, Laravel does not register the global Redirect
alias to point to the Illuminate\Support\Facades\Redirect
facade.
So, in your final line return Redirect::back()->with(...);
, the Redirect
class being used is the class defined in the plivio.php
library, not Laravel's Illuminate\Support\Facades\Redirect
class.
The quickest fix would be to change your line to:
return Illuminate\Support\Facades\Redirect::back()->with('success', 'Note added successfully');
Another option would be to inject Laravel's redirector into your controller, and use that instead of using the facade:
class MyController extends BaseController {
public function __construct(\Illuminate\Routing\Redirector $redirector) {
$this->redirector = $redirector;
}
public function sendSMS() {
require app_path().'/plivo/plivo.php';
//
return $this->redirector->back()->with('success', 'Note added successfully');
}
}
A third option would be to update your code to use the plivio composer package, which has a namespace. The updates have been done in the dev
branch of the repo, which you can find here. If you did this, you would get rid of your require
statement and use the namespaced plivio classes.