【问题标题】:Redirect to route in laravel not working重定向到laravel中的路由不起作用
【发布时间】:2018-12-27 12:18:09
【问题描述】:

我正在开发 Laravel 5.4.36 应用程序,其中我的路由 label 在 web.php 中定义为,

Route::get('/label/', 'LabelController@index');

但是当我试图从一个控制函数重定向到一个路由 label 使用时,

return redirect()->route('label');

错误:InvalidArgumentException Route [label] 未定义。

return Redirect::to('label');

错误:FatalErrorException 类 'App\Http\Controllers\Redirect' 不 找到了。

两者都不起作用,谁能帮助我如何重定向到 Laravel 中的路线?

【问题讨论】:

  • 像这样改变你的路线Route::get('/label', 'LabelController@index')->name('label');
  • @rkj 有错误(1/1) FatalErrorException Class 'App\Http\Controllers\Route' not found in LabelController.php (line 83)
  • 现在使用return redirect()->route('label');
  • 你必须在 LabelController 周围的某个地方有 Route line 83。检查那里或者你可以添加LabelController有问题

标签: php laravel laravel-5.4


【解决方案1】:

route() 重定向到命名路由,因此您需要在 routes/web.php 中命名您的路由:

Route::name('label')->get('/label/', 'LabelController@index');

https://laravel.com/docs/5.4/routing

【讨论】:

  • 有errpr,(1/1) FatalErrorException Class 'App\Http\Controllers\Route' not found in LabelController.php (line 84)
  • 您将其写在您定义路线的routes/web.php 中。不在您的控制器中。在您的操作中,您应该使用return \Redirect::route('label'); 或其他东西
【解决方案2】:

你可以重写路由到Route::get('label', 'LabelController@index');,然后调用return redirect()->route('label');

重写重定向调用return redirect()->route('/label/');

请试试这个

【讨论】:

    【解决方案3】:

    在您的路线/web.php 中:

    Route::get('/label/', 'LabelController@index')->name('label');
    

    在您的 LabelController 中,在函数索引的末尾:

    return redirect()->route('label');
    

    【讨论】:

      【解决方案4】:

      所以有很多方法可以做到,但我更喜欢以下两种重定向方式。

      1) 没有定义路由名称:

      Route::get('/label', 'LabelController@index');
      return redirect('/label');
      

      2) 通过定义路由名称:

       Route::get('/label', 'LabelController@index')->name('label');
       return redirect()->route('label');
      

      【讨论】:

        【解决方案5】:

        使用

        Route::get('/label', 'LabelController@index')->name('label');

        而不是

        Route::get('/label/', 'LabelController@index');

        删除标签后的 /。如果您不传递任何参数,则不需要 / 在路由的末尾

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2015-08-10
          • 2021-07-22
          • 2020-11-13
          • 2013-01-15
          • 2020-07-09
          • 2016-04-21
          • 2016-05-25
          • 1970-01-01
          相关资源
          最近更新 更多