【问题标题】:Laravel Route Link - Error Route not DefinedLaravel 路由链接 - 未定义错误路由
【发布时间】:2021-05-01 16:47:51
【问题描述】:

在包含我的路线的 web.php 中,我有以下路线:

Route::get('/profile', 'Profile\ProfileController@index');

哪些链接到我的配置文件控制器
我用

链接到这个控制器
Route::get('/profile', 'Profile\ProfileController@index');

但是,当我导航到设置了此路由的页面时,我收到此错误

    ErrorException (E_ERROR)
Route [profile] not defined. (View: D:\MAMP\htdocs\milestoneexp\resources\views\layouts\app.blade.php) (View: D:\MAMP\htdocs\milestoneexp\resources\views\layouts\app.blade.php)

但是,当我从路由切换到 URL 时,以下代码可以正常使用导航

<a class="dropdown-item" href="{{ url('profile') }}">{{ __('Profile') }}</a>

为什么 URL 有效但路由无效?我怎样才能让我的路线工作?
Laravel 5.8.35 版

【问题讨论】:

  • 这不是导致错误的行...某处有一个route('profile') 调用
  • route() 函数仅用于调用具有与路由关联的名称的路由。在您的路由声明中,您没有为其指定名称,这就是 route() 函数不起作用的原因。您必须在 web.php 中的路由声明中添加“->name('profile')”以使其正常工作。

标签: php laravel routes


【解决方案1】:

路由必须命名,所以在 web.php 中正确的路由代码是

Route::get('/profile', 'Profile\ProfileController@index')->name('profile');

来源:Route not defined. (laravel)

【讨论】:

    【解决方案2】:

    您也可以这样做:

    Route::get('profile', [
        'as' => 'profile',
        'uses' => 'Profile\ProfileController@index',
    ]);
    

    【讨论】:

      猜你喜欢
      • 2019-01-15
      • 2017-11-23
      • 2018-06-27
      • 2017-05-02
      • 2016-01-30
      • 1970-01-01
      • 1970-01-01
      • 2018-06-30
      • 2018-12-16
      相关资源
      最近更新 更多