php - 如何将参数传递给 Laravel 包中的 Controller 操作?

标签 php laravel package laravel-5.1 laravel-routing

在我制作的 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/

相关文章:

php - 扩展 MySQL 语句

php - 刷新 SQL 查询以显示更改

php - 带有 FFMPEG 的 Laravel Process() 对任何甚至很大的视频都会超时

oracle - oracle中加密解密PL SQL包

Java 文件未编译

php - 通过添加后缀重命名文件

php - 在 PHP 中接收 base64 编码的图像字符串并写入文件

Javascript:图像预览无法正常工作

javascript - 使用ajax在html中加载音频

android - 将 Activity 从一个包切换到另一个包?