缺少路由所需的参数 (Laravel8)

Missing required parameters for route (Laravel8)

我正在尝试编辑某人的预制脚本,因为原所有者已经几年没有更新了,我无法联系到他们。我认为脚本是用 Laravel 编写的,但我不知道他们使用的是什么版本。目前它似乎不适用于最新的 laravel 版本 8

现在我的问题是当我尝试登录时会出现错误:

Illuminate \ Routing \ Exceptions \ UrlGenerationException Missing required parameters for [Route: dashboard.profiles.show] [URI: dashboard/{profile}].

有谁知道如何解决这个问题?这里有一些我认为与错误相关的文件

web.php路线

use Illuminate\Support\Facades\Route;

Auth::routes(['verify' => true]);
Route::get('social/{provider}', 'Auth\SocialiteController@redirectToProvider')->name('auth.socialite');
Route::get('social/{provider}/callback', 'Auth\SocialiteController@handleProviderCallback');

Route::get('/', 'HomeController@index')->name('home');
Route::get('about', 'PageController@about')->name('pages.about');
Route::get('privacy', 'PageController@privacy')->name('pages.privacy');
Route::get('terms', 'PageController@terms')->name('pages.terms');
Route::get('press', 'PageController@press')->name('pages.press');

Route::get('l/{uid}', 'LinkController@show')->name('links.show');

Route::middleware('auth')->group(function () {
    Route::view('subscribe', 'subscribe');

    Route::get('account', 'UserController@edit')->name('users.edit');
    Route::patch('account', 'UserController@update');
    Route::patch('account/password', 'UserController@changePassword');

    Route::post('referral/invites', 'ReferralController@invites')->name('referral.invites');
});

Route::get('sitemap.xml', 'SitemapController@sitemap')->name('sitemap');

Route::get('{profile}', 'ProfileController@show')->name('profiles.show');

dashboard.php路线

use Illuminate\Support\Facades\Route;

Route::group(['middleware' => 'verified'], function () {
    Route::get('profile/create', 'ProfileController@create')->name('profiles.create');
});

// api
Route::group(['prefix' => 'api/{profile}'], function () {
    Route::get('/edit', 'ProfileController@edit');
    Route::put('/update', 'ProfileController@update');
    Route::post('/create', 'ProfileController@store');

    Route::put('theme/update', 'ProfileController@update');

    Route::put('/links/create', 'LinkController@store');
    Route::delete('/links/{link}', 'LinkController@destroy');
    Route::put('/links/{link}', 'LinkController@update');
    Route::put('/links/{link}/resort', 'LinkController@resort');
});

Route::get('{profile}', 'ProfileController@show')->name('profiles.show');

ProfileController.php

<?php

namespace App\Http\Controllers;

use App\Models\Profile;
use Illuminate\Http\Request;

class ProfileController extends Controller
{
    /**
     * Display the specified resource.
     *
     * @param Profile $profile
     *
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function show(Profile $profile)
    {
        $profile->viewed();

        return view('profiles.show', compact('profile'));
    }

}

没有路线 dashboard.profiles.show 可用,只有 profiles.show ...
这可能是 Blade 模板中的错字,它通过命名路由生成 link。
错误信息的意思是:正在访问一个没有被注册的命名路由。
UrlGenerationException 引用已知路线时不应抛出。

通常传递 int $id 比传递 Profile $profile 更容易。那么,您将如何将 object 转换为 string URL ? {profile} 似乎不是一个可行的方法。


我会完全不同地写(注意单数形式):

Route::get('/dashboard/profile', [
      'as' => 'profile.show',
    'uses' => 'ProfileController@show'
]);

Route::get('/profiles/{id}', [
      'as' => 'profile.show',
    'uses' => 'ProfileController@show'
]);

使用方法签名,例如。像这样(也可以是两种方法):

/**
 * @param int $id
 * @return View
 */
public function show(int $id): View {
    if (isset($id)) {
        /* process parameter ID */
    } else {
        /* the own ID: Auth::user()->id */
    }
}

然后在ProfileController中查找用户ID(如果需要)。传递 {id} 参数仅在查看当前用户配置文件以外的其他配置文件时才有意义 - 没有对错之分,但控制器应该处理这个问题;路由参数不是必需的。 int $id方便,因为Eloquent也用int $id;因此它几乎和模型实例 Profile $profile 本身一样好。路由名称的一致语法使生活更轻松......因为 'profiles.show' 听起来更像是 N 项目的列表,而不是 1.

的详细视图

注册的路由也可以列出:

php artisan route:list