简体   繁体   中英

How to do simple redirection in Laravel?

I am working on building my portfolio using Laravel. I am learning Laravel in the process of building my portfolio so I am very new to it.

I am trying to do a simple redirect using Route .

This is what I have written in web.php :

Route::patch('developer-profile',[
    'as' => 'developer-profile',
    'uses' => 'redirectionLinks@pleaseRedirect'
]);

This is how I am writing the HTML (It is bladed):

<a href="{{ route('developer-profile') }}">Developer Profile</a>

And this is how my controller is - Redirection.php :

namespace app\Http\Controllers;
use app\Http\Controllers\Controller;

public class redirectionLinks extends Controller{

    protected $guard = 'web'; //I am not using it anywhere, but I am searching for anything that I might have missed.

    public function pleaseRedirect(){
        return redirect('developer/developer-profile');
    }
};

I have tried to search this and this question, but it didnt really help.

I have gone through the following Documentation links from Laravel:

  1. HTTPControllers

  2. HTTPRedirects

and a few more in the past few days whose links I couldn't find.

Am I missing something here, because I seemed to have tried 5-10 basic approaches starting from the basic function as follows:

Route::get('/developer/developer-profile', function(){
    return view('developer/developer-profile');
})->name('developer-profile');

EDIT:

When I hit php artisan route:list in the CMD, I get the following error window:

ReflectionException

UPDATE:

I have changed the filename to redirectionLinks.php from Redirection.php . Now the above error when I hit php artisan route:list show me all the routes, but still the redirection is not happening as I get the following error:

MethodNotAllowedException

Please help me with my problem if anyone has a clue about what I am asking.

Thanks in advance.

You gotta rename your file to redirectionsLinks.php instead of Redirection.php , if this still doesn't work, try rebuilding the autoload: composer dumpauto .

Either that or doing this in the routes, and renaming your class to Redirection, should work:

Route::get('developer-profile',[
    'as' => 'developer-profile',
    'uses' => 'Redirection@pleaseRedirect'
]);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM