我正在嘗試編輯某人的預制腳本,因為原始所有者已經幾年沒有更新它并且我無法與他們聯系。我認為腳本是用 Laravel 撰寫的,但我不知道他們使用的是什么版本。目前它似乎不適用于最新的 Laravel 版本 8
現在我的問題是當我嘗試登錄時會出現錯誤:
Illuminate \ Routing \ Exceptions \ UrlGenerationException 缺少 [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');
儀表板.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');
組態檔控制器.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'));
}
}
uj5u.com熱心網友回復:
沒有dashboard.profiles.show可用的路由,但只有profiles.show...
這可能是 Blade 模板中的一個錯字,它通過命名路由生成鏈接。
錯誤資訊的意思是:訪問一個命名的路由,它沒有被注冊。
UrlGenerationException參考已知路線時不應拋出。
通過int $id而不是Profile $profile. 那么你將如何將其轉換object為stringURL ?{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
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/386360.html
