【问题标题】:How to pass extra parameters to controller from Laravel route如何从 Laravel 路由向控制器传递额外的参数
【发布时间】:2015-10-27 12:49:07
【问题描述】:

我正在尝试在 Laravel 的路由中处理我的 API 调用的基本验证。这是我想要实现的目标:

Route::group(['prefix' => 'api/v1/properties/'], function () {
     Route::get('purchased', 'PropertiesController@getPropertyByProgressStatus', function () {
       //pass variable x = 1 to the controller
     });

     Route::get('waiting', 'PropertiesController@getPropertyByProgressStatus', function () {
       //pass variable x = 2 to the controller
});

});

长话短说,取决于 api/v1/properties/ 之后的 URI 段,我想将不同的参数传递给控制器​​。有没有办法做到这一点?

【问题讨论】:

    标签: php laravel-5 laravel-routing


    【解决方案1】:

    我能够让它与以下 route.php 文件一起工作:

    Route::group(['prefix' => 'api/v1/properties/'], function () {
        Route::get('purchased', [
            'uses' => 'PropertiesController@getPropertyByProgressStatus', 'progressStatusId' => 1
        ]);
        Route::get('remodeled', [
            'uses' => 'PropertiesController@getPropertyByProgressStatus', 'progressStatusId' => 1
        ]);
        Route::get('pending', [
            'uses' => 'PropertiesController@getPropertyByProgressStatus', 'progressStatusId' => 3
        ]);
        Route::get('available', [
            'uses' => 'PropertiesController@getPropertyByProgressStatus', 'progressStatusId' => 4
        ]);
        Route::get('unavailable', [
            'uses' => 'PropertiesController@getPropertyByProgressStatus', 'progressStatusId' => 5
        ]);
    });
    

    以及控制器中的以下代码:

    公共函数 getPropertyByProgressStatus(\Illuminate\Http\Request $request) {

    $action = $request->route()->getAction();
    print_r($action);
    

    $action 变量几乎可以让我访问从路由传递的额外参数。

    【讨论】:

      【解决方案2】:

      我认为您可以直接在控制器中执行此操作并接收该值作为路由的参数:

      首先你需要在控制器中指定参数的名称。

      Route::group(['prefix' => 'api/v1/properties/'], function ()
      {
          Route::get('{parameter}', PropertiesController@getPropertyByProgressStatus');
      

      这样getPropertyByProgressStatus方法就会接收到这个值,所以在控制器中:

      class PropertiesController{
      ....
      public function getPropertyByProgressStatus($parameter)
      {
          if($parameter === 'purchased')
          {
              //do what you need
          }
          elseif($parameter === 'waiting')
          {
              //Do another stuff
          }
      ....
      }
      

      我希望它有助于解决您的问题。

      查看此课程:Learn LaravelCreate a RESTful API with Laravel

      最好的祝愿。

      ----------- 已编辑 --------------- 您可以重定向到您想要的路线:

      Route::group(['prefix' => 'api/v1/properties/'], function () {
           Route::get('purchased', function () {
             return redirect('/api/v1/properties/purchased/valueToSend');
           });
      
           Route::get('waiting', function () {
             return redirect('/api/v1/properties/waiting/valueToSend');
           });
      
          Route::get('purchased/{valueToSend}', PropertiesController@getPropertyByProgressStatus);
           });
      
          Route::get('waiting/{valueToSend}', PropertiesController@getPropertyByProgressStatus);
           });
      });
      

      最后两条路由响应重定向并将该值作为参数发送到控制器,是我认为最接近直接从路由执行此操作的方法。

      【讨论】:

      • 是的,问题是我如何在路线中做到这一点?正如我在问题中提到的:“我正在尝试在 Laravel 的路由中处理我的 API 调用的基本验证”。谢谢
      • 好吧,基本上你是在路由中做的,检查你是否接收到一个参数并将它发送到控制器。对你没用?请提供更多详细信息以了解它为什么不起作用。最好的祝愿。 PS:您可以将重定向返回到另一条路线。检查响应(已编辑)
      • 仍然不是我想要的。看看这段代码:Route::get('api/v1/properties/purchased', 'PropertiesController@getPropertyById', function ($variableX) {});例如,如何修改 $variableX 以传递 1?
      猜你喜欢
      • 1970-01-01
      • 2013-08-15
      • 2015-09-25
      • 1970-01-01
      • 1970-01-01
      • 2021-01-28
      • 2019-07-10
      • 2012-09-20
      • 2017-05-14
      相关资源
      最近更新 更多