表单操作中的Laravel Route / {username}

时间:2016-10-22 08:22:38

标签: php forms laravel laravel-5 laravel-5.3

我目前正在处理Laravel-5.3中的图片上传。 我的"个人资料页面"路线定义为:

/* 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',
]);

因此,个人资料网址几乎看起来像example.com/john(让" john"是用户名)

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

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

如何在表单操作中定义路由,以便将用户重定向到各自的路由;像&#34; example.com/john&#34;。

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

2 个答案:

答案 0 :(得分:0)

如果您需要为当前用户提供路线,您可以执行以下操作:

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

UPD:对于命名路线,您可以使用route助手

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

答案 1 :(得分:0)

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

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