Skip to content
Advertisement

Laravel Redirect from Helper Class

I’m trying to redirect to an external URL from a helper class.

Here’s my controller:

<?php

namespace AppHttpControllers;

use AppLead;
use IlluminateHttpRequest;
use App;
use Helper;

class MyController extends Controller
{
    public function entities_get() {
        Helper::my_function(); // <---- Call my Helper class method to redirect.

        return view( 'template' );
    }
}

Here’s my helper class used in the controller:

<?php

namespace AppHelpers;

class Helper
{
    public static function my_function()
    {
        return redirect()->away( 'https://www.google.com' ); // <---- Not redirecting.
    }
}

Why is the redirect() not working inside my_function()? Do I need to include some Laravel classes using the PHP “use” statement?

Advertisement

Answer

You can add send method and it will work.

<?php

namespace AppHelpers;

class Helper
{
    public static function my_function()
    {
        return redirect()->away('https://www.google.com')->send();
    }
}
User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement