如何将参数传递给 Laravel 包中的控制器操作?
How do I pass a parameter to a controller action within a Laravel Package?
在我制作的 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()
函数。
它正在尝试从 App\Http\Controllers
命名空间访问您的控制器。可以看到他们已将其添加到您的错误中的控制器名称中:
App\Http\Controllers\Ariel\SpotBuy\Http\Controllers\Admin\SpotBuyController@getPart
您需要在开头使用 \
转义 Ariel
命名空间:
return redirect()->action('\Ariel\SpotBuy\Http\Controllers\Admin\SpotBuyController@getPart', [$id]);
在我制作的 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()
函数。
它正在尝试从 App\Http\Controllers
命名空间访问您的控制器。可以看到他们已将其添加到您的错误中的控制器名称中:
App\Http\Controllers\Ariel\SpotBuy\Http\Controllers\Admin\SpotBuyController@getPart
您需要在开头使用 \
转义 Ariel
命名空间:
return redirect()->action('\Ariel\SpotBuy\Http\Controllers\Admin\SpotBuyController@getPart', [$id]);