2017-04-15 115 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> 

希望这有助于通过阵列中添加。 :)