Laravel路由/{username}在表单动作


Laravel Route /{username} in form action

我目前正在处理Laravel-5.3中的图像上传。我的"profile page"路由定义为:

/* GET request to show the profile page */
Route::get('/{username}', [
    'uses' => ''App'Http'Controllers'ProfileController@getProfile',
    'as'   => 'profile.index',
]);
/* POST request to update the Avatar (display pic) */
Route::post('/{username}', [
    'uses' => ''App'Http'Controllers'ProfileController@updateAvatar',
    'as'   => 'profile.index',
]);

因此,配置文件URL看起来很像example.com/john(让"john"作为用户名)

在表单中,我必须定义"action"。

<form enctype="multipart/form-data" action="" method="POST">

我如何在表单动作中定义路由,以便用户被重定向到他们各自的路由;比如" example.com/john "

我想我不能直接定义<form action="/{username}">,因为我将被带到example.com/{username}

当您命名路由时,您可以使用该名称通过route()帮助器定义URL,传递一个参数数组作为第二个参数。

<form enctype="multipart/form-data" 
      method="POST"
      action="{{ route('profile.index', [
          'username' => Auth::user()->name
      ]) }}">

如果你需要当前用户的路由,你可以这样做:

<form enctype="multipart/form-data" action="{{ url(Auth::user()->name) }}" method="POST">

UPD:对于命名路由,您可以使用route helper

<form enctype="multipart/form-data" action="{{ route('profile.index', ['username' => 'username_here']); }}" method="POST">