在我制作的 Laravel 包中,我想将用户重定向到需要参数的 Controller 操作(在同一包中)。
Controller :
public function postMatchItem(Request $request, $id)
{
$this->validate($request, [
'item_match' => 'required|numeric|exists:item,id',
]);
$spot_buy_item = SpotBuyItem::find($id);
$item = Item::find($request->input('item_match'));
$price = $item->getPrice();
$spot_buy_item_response = new SpotBuyItemResponse();
$spot_buy_item_response->spot_buy_item_id = $id;
$spot_buy_item_response->spot_buy_id = $spot_buy_item->spot_buy_id;
$spot_buy_item_response->item_id = $item->id;
$spot_buy_item_response->user_id = $spot_buy_item->user_id;
$spot_buy_item_response->spot_buy_price = $price;
$spot_buy_item_response->created_ts = Carbon::now();
$spot_buy_item_response->save();
return redirect()->action('Ariel\SpotBuy\Http\Controllers\Admin\SpotBuyController@getPart', [$id]);
}
重定向中的操作与我在 routes.php
文件中用于将用户定向到此 Controller 操作的路径相同
路线:
Route::get('/part/{id}', 'Ariel\SpotBuy\Http\Controllers\Admin\SpotBuyController@getPart')->where('id', '[0-9]+');
我已经尝试过此路径的变体但没有成功,包括 SpotBuyController@getPart
就像文档建议的那样 ( https://laravel.com/docs/5.1/responses#redirects )
注意:我通过在 routes.php
中命名我的路线并使用 return redirect()->route('route_name', [ $id]);
,但我仍然想知道如何将包 Controller 操作传递给 ->action()
函数。
最佳答案
它试图从 App\Http\Controllers
命名空间中访问您的 Controller 。可以看到他们已将它添加到您的错误中的 Controller 名称中:
App\Http\Controllers\Ariel\SpotBuy\Http\Controllers\Admin\SpotBuyController@getP art
您需要在开头使用 \
转义 Ariel
命名空间:
return redirect()->action('\Ariel\SpotBuy\Http\Controllers\Admin\SpotBuyController@getPart', [$id]);
关于php - 如何将参数传递给 Laravel 包中的 Controller 操作?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35071374/