用户路由Laravel 5.4

问题描述 投票:0回答:2

我希望用户通过URL /profile/slug/edit访问其个人资料编辑页面,其中slug表示$user->slug 。 我的web.php contans:

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

如何从视图调用ProfilesController@edit ,如何正确传递参数? 尝试过:

<a href="{{route('profile', ['slug'=> Auth::user()->slug],'edit')}}">
Edit your profile</a>
php laravel-routing laravel-5.4
2个回答
1
投票

这是我的方法。

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

然后您认为可以使用。

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

如您所见,首先我们必须给route()一个我们感兴趣的路由名称,在您的情况下,它是profile.edit这是目标路由,并且从路由文件中我们知道它缺少slug值,因此我们将它的值设为第二个参数(如果缺少更多值,则第二个参数应为数组)。

这需要一些实践和时间,但是尝试不同的方法来查看什么使您的代码更具可读性。 行数与计算机无关紧要,编写代码,这样一来,如果您想从现在开始更改一两年,便可以轻松阅读和理解。


1
投票

您可以使用以下代码行

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

您的路线定义似乎很好。

另外,如果您想添加一些get参数,则可以直接在作为第二个参数传递的数组中添加

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

希望这可以帮助。 :)

© www.soinside.com 2019 - 2024. All rights reserved.