Home > database >  Redirect not working within a Laravel method
Redirect not working within a Laravel method

Time:10-01

I want to redirect some of my urls as follows:

/myprefix/fashion => /fashion

So I added a method to the controller:

protected function myMethod($mySlug)
{
    if (preg_match('/myprefix\/(\w )/', $mySlug, $myMatches)) {
        dump(myUrlHelper($matches[1])); // Outputs URL properly.
        redirect(myUrlHelper($matches[1]), 301); // ...but doesn't redirect.
        // I also tried:
        // return redirect(url($matches[1]), 301);
    }

    return false;
}

What am I missing?

CodePudding user response:

try adding return before redirect function, and remove dump function

protected function myMethod($mySlug)
{
    if (preg_match('/myprefix\/(\w )/', $mySlug, $myMatches)) {
        return redirect(myUrlHelper($matches[1]), 301);
    }

    return false;
}

CodePudding user response:

As I am understanding your codes

web.php

Route::get('/myprefix/{slug}', 'YourController@myMethod');

function

public function myMethod($mySlug)
{
    return redirect(myUrlHelper($mySlug), 301);
}
  • Related