首页 > 解决方案 > 如何在 Laravel 中进行简单的重定向?

问题描述

我正在使用 Laravel 构建我的作品集。我在构建我的投资组合的过程中学习 Laravel,所以我对它很陌生。

我正在尝试使用Route.

这是我在web.php中写的:

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

这就是我编写 HTML 的方式(它是刀片式的):

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

这就是我的控制器的样子 - 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');
    }
};

我试图搜索这个这个问题,但它并没有真正帮助。

我浏览了 Laravel 的以下文档链接:

  1. HTTP控制器

  2. HTTP重定向

在过去的几天里还有一些我找不到链接。

我在这里遗漏了什么吗,因为我似乎从基本功能开始尝试了 5-10 种基本方法,如下所示:

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

编辑:

当我点击php artisan route:listCMD 时,我得到以下错误窗口:

反射异常

更新:

我已将文件名更改为redirectionLinks.phpfrom Redirection.php。现在,当我点击显示所有路线时出现上述错误php artisan route:list,但仍然没有发生重定向,因为我收到以下错误:

MethodNotAllowedException

如果有人知道我在问什么,请帮助我解决我的问题。

提前致谢。

标签: phplaravelcontrollerroutesurl-redirection

解决方案


您必须将文件重命名为,redirectionsLinks.php而不是Redirection.php,如果这仍然不起作用,请尝试重建 autoload: composer dumpauto

无论是这样做还是在路由中执行此操作,并将您的类重命名为 Redirection,都应该有效:

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

推荐阅读