[英]Framework PHP Route - Error 404 Not found
在 Laravel 中,我需要編輯由 Auth/RegisterController 創建的用戶,但使用我創建的控制器,例如 ProfileController。 但是當我嘗試通過 aa:href 按鈕訪問時,顯示 404 錯誤。
代碼:
網頁.php
Route::group(['middleware' => ['auth']], function(){
Route::resource('profile', 'ProfileController')->except(['edit']);
Route::get('profile/{profile}/edit', ['as' => 'profile.edit', 'uses' => 'ProfileController@edit']);
});
Auth::routes();
應用程序.blade.php
<a href="{{route('profile.edit',['profile'=>auth()->user()->id])}}" class="btn btn-primary">
{{auth()->user()->email}}
</a>
配置文件控制器
public function edit($profile)
{
$user = \App\User::findOrFail($profile);
return view('profile.edit', compact('user'));
}
列出路線
| | POST | profile | profile.store | App\Http\Controllers\ProfileController@store | web,auth,guest |
| | GET|HEAD | profile | profile.index | App\Http\Controllers\ProfileController@index | web,auth,guest |
| | GET|HEAD | profile/create | profile.create | App\Http\Controllers\ProfileController@create | web,auth,guest |
| | DELETE | profile/{profile} | profile.destroy | App\Http\Controllers\ProfileController@destroy | web,auth,guest |
| | PUT|PATCH | profile/{profile} | profile.update | App\Http\Controllers\ProfileController@update | web,auth,guest |
| | GET|HEAD | profile/{profile} | profile.show | App\Http\Controllers\ProfileController@show | web,auth,guest |
| | GET|HEAD | profile/{profile}/edit | profile.edit | App\Http\Controllers\ProfileController@edit | web,auth,guest |
有人可以幫助我嗎?
tks,
阿吉亞爾,阿德森 M.
歡迎來到 SO!
就像提到的@lagbox 一樣,您可以保留
Route::resource('profile', 'ProfileController');
沒有->except()
以自動為您生成完全相同的編輯路由。
此外,您可以將控制器操作簡化為:
public function edit(User $profile)
{
return view('profile.edit', compact('profile'));
}
這使用類型提示自動讓依賴注入為您解析 User 實例,由 {profile} 參數傳入的鍵標識。
然后你將不得不在你的個人資料/edit.blade.php 中使用$profile
,除非你想做類似return view('profile.edit', ['user' => $profile]);
事情return view('profile.edit', ['user' => $profile]);
.
像您的模型一樣命名參數可能是個好主意。 資源路由通常完全基於模型的名稱,因此配置文件不是用戶路由的好選擇。 您可以使用Route::resource('user', 'ProfileController')
(甚至 UserController),或者如果您想堅持使用配置文件,但希望$user
作為參數,則必須創建所有路由自己喜歡Route::get('profile/{user}/edit', 'ProfileController')
。
此外,編寫route('profile.edit',['profile'=>auth()->user()])
也有效,並且具有在用戶未通過身份驗證時不會中斷的優點。
綜上所述,您的代碼應該仍然有效。 嘗試使用find()
而不是findOrFail()
和dd($user);
在以下行中以確保您的路線有效。 如果你看到轉儲,它可能是空的,這意味着傳遞的 id 是無效的,這真的很奇怪。
如果您可以發布錨標記的完整生成鏈接和dd
的結果,我將很樂意提供幫助。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.