如何使用带有 url 参数和错误消息的 auth 中间件的 redirectTo 方法?

问题描述

我想要做的就是使用带有语言参数的 URL,这会导致我想向用户显示错误消息。如果我执行注释的代码,我会收到错误;

标题不能包含多个标题,检测到新行。

另一方面,如果我执行未包含在注释中的代码,则会出现错误;

在字符串上使用 () 调用成员函数。

我知道错误的原因。但是,我正在寻找一种解决方案来实现我的目标。

new Vue({
    vuetify: new Vuetify(),el: "#app",data: {
    dragged: {
      from: -1,to: -1,newIndex: -1
    },selectedSync: ['a'],itemsSync: ['a','b','c','d','e']
  },methods: {
    move: function(value) {
      this.dragged = {
        from: parseInt(value.from.id),to: parseInt(value.to.id),newIndex: value.draggedContext.futureIndex,}
    },change: function(value) {
      if (value.removed) {
        // insert
        this.selectedSync.splice(this.dragged.to+this.dragged.newIndex,this.selectedSync[this.dragged.from])
        // delete
        if (this.dragged.from < this.dragged.to) // LTR
          this.selectedSync.splice(this.dragged.from,1)
        else // RTL
          this.selectedSync.splice(this.dragged.from + 1,1)
      }
    }
  },computed: {
    dragOptionsChips() {
      return {
        animation: 200,group: "group",disabled: false,ghostClass: "ghost",sort: true,};
    }
  },})

解决方法

您不能在 with() 方法上调用 route(),因为 route() 方法只返回一个字符串,并不负责重定向。

如果您需要在调用 redirectTo() 方法后向用户显示错误消息,我认为您可以将错误消息保留在 Laravel Session

https://laravel.com/docs/8.x/session#interacting-with-the-session

protected function redirectTo($request)
{
    if (! $request->expectsJson()) {

        // This next line keeps the error message in session for you to use on your redirect and then deletes it from session immediately after it has been used
        $request->session()->flash('error','Error message!');

        return route('login',['locale' => app()->getLocale()]);
    }
}

您现在可以像往常一样查看错误消息:

在您的控制器中:

$request->session()->get('error');

或者从你的角度来看:

{{ Session::get('error) }}
,

试试你不能在 redirect() 中传递 2 个参数,它接受 url 而不是路由名称

return redirect()->route('login',['locale' => app()->getLocale()])->with('error','Error message'); 

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...