2017-04-15 134 views
0

我希望我的用戶通過URL訪問其配置文件編輯頁面:/profile/slug/edit,其中slug表示$user->slug。 我web.php contans:用戶路由Laravel 5.4

Route::group(['middleware' => 'auth'], function() { 
Route::get('/profile/{slug}', [ 
'uses' => '[email protected]', 
'as' => 'profile' 
]); 
Route::get('/profile/{slug}/edit', [ 
'uses' => '[email protected]', 
'as' => 'profile.edit' 
]); 

如何調用從視圖[email protected],如何正確傳遞參數?嘗試:

<a href="{{route('profile', ['slug'=> Auth::user()->slug],'edit')}}"> 
Edit your profile</a> 
+1

你的路線應該有profile.edit而不是隻有配置文件。並將配置文件作爲路由功能的第三個參數移除。 – Timo002

+0

是否可以簡化我的代碼?並縮短我的代碼?我前綴',但如何處理'slu'',我必須通過它在我的每一個'路線()'? – pvaitonis

回答

1

這裏是我會怎麼做呢..

Route::group(['middleware' => 'auth'], function() { 
    Route::get('/profile/{slug}', '[email protected]')->name('profile'); 
    Route::get('/profile/{slug}/edit', '[email protected]')->name('profile.edit'); 
}); 

,然後在視圖中,可以使用..

<a href="{{ route('profile.edit', Auth::user()->slug) }}">Edit your profile</a> 

正如你所看到的,首先我們必須給我們感興趣的路線名稱route(),在你的情況下它是目標路線profile.edit,並且我們從我們的路線文件知道它缺少slug值,所以我們提供它作爲第二個參數slug值(如果有更多的缺失值,第二個參數應該是一個數組)。

它需要一些練習和時間,但嘗試不同的方法來看看是什麼讓你的代碼更具可讀性。線的數量對計算機無關緊要,編寫代碼以便您可以輕鬆閱讀並理解它,如果您想從現在開始一年或兩年更改某些內容。

1

您可以使用下面的代碼行

<a href="{{ route('profile.edit', ['slug' => Auth::user()->slug]) }}"> Edit your profile</a> 

你的路由定義似乎是罰款。

另外,如果你想添加一些獲得參數,可以可以直接作爲第二個參數

<a href="{{ route('profile.edit', ['slug' => Auth::user()->slug, 'otherparam' => 'value']) }}"> Edit your profile</a> 

希望這有助於通過陣列中添加。 :)