【问题标题】:Missing required parameters for route (Laravel8)缺少路由所需的参数(Laravel 8)
【发布时间】:2022-01-21 05:42:29
【问题描述】:

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

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

Illuminate \ Routing \ Exceptions \ UrlGenerationException Missing [Route: dashboard.profiles.show] [URI: 仪表板/{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'));
    }

}

【问题讨论】:

    标签: php laravel laravel-8


    【解决方案1】:

    没有可用的路线dashboard.profiles.show,但只有profiles.show ...
    这可能是 Blade 模板中的拼写错误,它通过命名路由生成链接。
    错误信息的意思是:访问一个未注册的命名路由。
    引用已知路由时不应抛出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
    

    【讨论】:

      猜你喜欢
      • 2019-01-09
      • 2020-08-07
      • 2020-12-25
      • 1970-01-01
      • 2020-11-01
      • 1970-01-01
      • 2022-01-05
      • 2016-05-17
      相关资源
      最近更新 更多