Laravel's intended() with vue router

Laravel's intended() with vue router

我正在处理一个项目,并且在登录后努力重定向到预期位置。 问题是 Laravel 不包含 Vues 路由(#/... 之后的任何内容)所以它总是只将我重定向到 'domain.com/'

我只为 'login' 'logout' 和“/”使用 laravel 路由,应用程序的其余部分是使用 vue 路由的单页。

应用程序的用户在需要采取行动时会收到通知电子邮件。这些电子邮件包含指向需要他们采取行动的请求的链接(例如 domain.com/#/request/3413)。当然,他们需要登录才能访问,因此他们被 laravel (domain.com/login#/request/3413)

重定向到登录页面

成功登录后,我尝试使用

重定向他们
return redirect()->intended('/');

但是它将他们重定向到 'domain.com/' 而不是 'domain.com/#/request/3413'

有什么方法可以让 laravel 在重定向中包含 vues 路由吗?

非常感谢!

因此,经过一些令人痛苦的研究后,我发现 # 之后的任何内容都由浏览器处理,这意味着服务器看不到它,因此您无法在 PHP 代码中访问它。我发现感谢这个线程:Getting FULL URL with #tag

我不确定请求的那部分是否甚至没有发送到服务器,但看起来是这样,而且对我来说甚至很合乎逻辑。

所以为了解决这个问题,我将通过电子邮件发送给用户的 link 更改为

domain.com/?request=3413

这样我就可以在我的 PHP 代码中访问它,并在成功登录后将重定向 link 放在一起。

您可以通过启用 History Mode 在 Vue Router 中删除 URL 中的 # ,如下所示:

const router = new VueRouter({
  mode: 'history',
  routes: [...]
})

您需要将所有请求定向到应用所在的位置。如果您的后端是 Laravel,您可以在 routes/web.php 文件

中定义一个 catch-all 路由
Route::get('/{any}', 'SpaController@index')->where('any', '.*');

需要注意的是,对于未找到的路径,服务器将不再 return 404,因此如果未找到路径,您将必须创建一个 NotFoundComponent 以在 Vue 应用程序上显示。

const router = new VueRouter({
  mode: 'history',
  routes: [
    { path: '*', component: NotFoundComponent }
  ]
})

阅读 Vue Router History Mode Documentation 了解更多信息