【问题标题】:Laravel - Route not definedLaravel - 未定义路线
【发布时间】:2018-10-15 04:23:30
【问题描述】:

我有一个使用 Laravel 表单包的表单,该表单的头部如下所示:

{{ Form::model($user, array('route' => array('users.update', $user->username), 'method' => 'PUT')) }}

此表单位于编辑刀片内views/users/edit.blade.php

据我所知,'route' => array('users.update', $user->$username) 执行以下操作: 找到名称为users.update 的路由并添加用户的用户名作为参数。

routes/web.php里面我有这个:

Route::post('/users/{user}', 'UsersController@update')->name('users.update');

鉴于此,我假设以下内容:

表单转到命名路由并将其转换为www.example.com/users/username

但是,当我转到其中包含更新表单的编辑页面时,我得到:

"Route [users.update] not defined. (View: C:\xampp\htdocs\my-newable\resources\views\users\edit.blade.php)"

更奇怪的是,当我运行 php artisan route:list 时,所讨论的路由甚至没有列为您可以在应用程序中使用的路由。

我也试过以下命令:php artisan route:cache

但还是没有出现?

最后,这是UsersController的更新方法

public function update(Request $request, User $user)
{  
    $user = User::findOrFail($user)->first(); 

    //Validate name, email and password fields  
    $this->validate($request, [

    ]);

    $roles = $request['roles']; //Retreive all roles

    if (isset($roles)) 
    {        
        $user->roles()->sync($roles);  //If one or more role is selected associate user to roles          
    }        
    else 
    {
        $user->roles()->detach(); //If no role is selected remove exisiting role associated to a user
    }

    return redirect()->route('users.index')->with('flash_message', 'User successfully edited.');
}

我只是没有看到users.update 方法是如何没有定义的?

【问题讨论】:

  • 我在用户模型中使用$username作为主键。
  • 附带说明,在数据库中使用字符串/varchars 作为主键通常是个坏主意。您应该使用整数。使用字符串作为主键会显着降低数据库速度。
  • 更多是因为我希望能够通过用户名搜索用户,因此 URL slug 包含更多人类可读的元素。如果有一种方法可以使用友好的 URL 并且仍然使用我的 ID,我很想知道。
  • 是的,有!您应该观看此视频:laracasts.com/series/laravel-from-scratch-2017/episodes/31 当您观看视频时,请观看整个系列。这是个好习惯。

标签: php laravel


【解决方案1】:

试试php artisan route:clear。 根据文档php artisan route:cache 生成缓存的路由文件,每次添加新文件时都需要刷新。 https://laravel.com/docs/5.6/controllers#route-caching

我不知道 route:cache 是否也会重置缓存的路由。我希望 route:clear 有帮助:)

【讨论】:

  • route:cache 确实会重置您的缓存。
猜你喜欢
  • 2023-03-29
  • 1970-01-01
  • 2016-01-24
  • 2020-05-03
  • 2016-04-02
  • 2014-09-20
  • 2020-07-26
  • 2020-10-28
  • 2016-04-04
相关资源
最近更新 更多