【发布时间】:2016-05-06 09:53:35
【问题描述】:
在我制作的 Laravel 包中,我想将用户重定向到需要参数的控制器操作(在同一个包中)。
控制器:
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 文件中用于将用户引导到此控制器操作的路径相同
路线:
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]); 来实现这一点,但我仍然想知道如何将包控制器操作传递给->action() 函数.
【问题讨论】:
-
试试:
return redirect()->action('Ariel\SpotBuy\Http\Controllers\Admin\SpotBuyController@getPart', ['id' => $id]); -
不走运。我收到相同的错误消息:
production.ERROR: exception 'InvalidArgumentException' with message 'Action App\Http\Controllers\Ariel\SpotBuy\Http\Controllers\Admin\SpotBuyController@getPart not defined.
标签: php laravel package laravel-5.1 laravel-routing