【发布时间】:2018-04-23 23:48:55
【问题描述】:
例如,我的 web.php 中有这条路线:-
Route::get('products/{product}/owners', 'ProductController@showOwners');
当我尝试向产品添加新的“所有者”时,我必须在父 URL 中进行,如下所示:-
Route::post('products/storeOwner', 'ProductController@storeOwner');
然后我在表单的隐藏字段中传递产品 ID,因为发布请求不接受 URL 参数。那么有没有像下面那样做呢?
Route::post('products/{product}/storeOwner', 'ProductController@storeOwner');
所以 POST 请求将在特定的“产品”URL 中发送?
更新
/* ProductController Class */
public function storeOwner (AddProductOwner $request)
{
$product= Product::find($request->product);
$user = Auth::user();
if ( $user->ownerOf($product)) {
// Check if the current user is already one of the owners).
// If the current user is the owner then return to the product
// This line is not executed because in (products/show.blade.php) we have set a condition.
return redirect('products/' . $request->product);
}
$join = new Join;
$join->role = $request->join_role;
$join->product()->associate($request->product);
$join->user()->associate(Auth::user());
$join->message = $request->message;
$join->save();
// TODO: we have to make this with ajax instead of normal form
return redirect('products/'. $request->product);
}
我希望我的问题足够清楚..
【问题讨论】:
-
当然。如果路由和 URL 定义正确,这将起作用。
-
POST 请求不接受 URL 中的参数
-
我一直在使用它,就像下面的两个答案一样。 Laravel 就是这样聪明的。从技术上讲,它不是 POST,而是 URL 解析。
标签: php laravel post get routes