【问题标题】:Laravel Route /{username} in form action表单操作中的 Laravel Route /{username}
【发布时间】:2017-03-04 13:09:34
【问题描述】:

我目前正在处理 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',
]);

因此,个人资料 URL 看起来很像 example.com/john(让“john”成为用户名)

但在表单中,我必须定义“动作”。

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

如何在表单操作中定义路由,以便将用户重定向到各自的路由;类似于“example.com/john”。

我想我不能直接定义&lt;form action="/{username}"&gt;,因为我会被带到example.com/{username}

【问题讨论】:

  • 您有 2 条路线名称相同。

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


【解决方案1】:

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

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

【讨论】:

    【解决方案2】:

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

    <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">
    

    【讨论】:

      猜你喜欢
      • 2020-02-27
      • 1970-01-01
      • 2015-11-28
      • 1970-01-01
      • 2020-05-07
      • 2017-07-01
      • 1970-01-01
      • 2017-04-11
      • 1970-01-01
      相关资源
      最近更新 更多