API请求中的Laravel可选参数未正确识别

时间:2019-01-10 08:26:29

标签: php laravel laravel-5.3

在我的api.php中,我有这些路线。

我可以请求端点:/api/v2/commodities/1-该请求被重定向到APICommodityController@read-可以!

每当我GET请求/v2/commodities/categories/时,都会发生此问题。我希望将此请求重定向到route-group:'categories',但是Laravel认为这是第一组中的可选参数(在代码注释中为〜ref。“ HERE”)。

我该如何解决?

Route::group( [ 'prefix' => 'v2' ], function() {

    /**
     * Commodity
     */
    Route::group( [ 'prefix' => 'commodities' ], function() {

        /**
         * C: CREATE
         */
        Route::post( '{id?}', 'API\v2\Commodity\APICommodityController@updateOrCreate' );

        /**
         * U: UPDATE
         */
        Route::put( '{id}', 'API\v2\Commodity\APICommodityController@updateOrCreate' );

        /**
         * R: READ (HERE)
         */
        Route::get( '{id?}', 'API\v2\Commodity\APICommodityController@read' );

        /**
         * D: DELETE
         */
        Route::delete( '{id}', 'API\v2\Commodity\APICommodityController@delete' );

        /**
         * Commodity categories
         */
        Route::group( [ 'prefix' => 'categories' ], function() {

            /**
             * C: CREATE
             */
            Route::post( '{id?}', 'API\v2\CommodityCategories\APICommodityCategoriesController@updateOrCreate' );

            /**
             * U: UPDATE
             */
            Route::put( '{id}', 'API\v2\CommodityCategories\APICommodityCategoriesController@updateOrCreate' );

            /**
             * R: READ
             */
            Route::get( '{id?}', 'API\v2\CommodityCategories\APICommodityCategoriesController@read' );

            /**
             * D: DELETE
             */
            Route::delete( '{id}', 'API\v2\CommodityCategories\APICommodityCategoriesController@delete' );

        } );

    } );

2 个答案:

答案 0 :(得分:0)

在您的id参数末尾添加此条件:

->where('id', '[0-9]+')

所以它变成了这样的东西:

Route::get( '{id?}', 'API\v2\Commodity\APICommodityController@read' )->where('id', '[0-9]+');

答案 1 :(得分:0)

只需将您的“商品类别”路线部分移至所有其他路线定义上方。这将使laravel注册此静态路由,而不会被动态路由捕获。

类似的东西:

Route::group( [ 'prefix' => 'v2' ], function() {

    /**
     * Commodity
     */
    Route::group( [ 'prefix' => 'commodities' ], function() {
        /**
         * Commodity categories
         */
        Route::group( [ 'prefix' => 'categories' ], function() {

            /**
             * C: CREATE
             */
            Route::post( '{id?}', 'API\v2\CommodityCategories\APICommodityCategoriesController@updateOrCreate' );

            /**
             * U: UPDATE
             */
            Route::put( '{id}', 'API\v2\CommodityCategories\APICommodityCategoriesController@updateOrCreate' );

            /**
             * R: READ
             */
            Route::get( '{id?}', 'API\v2\CommodityCategories\APICommodityCategoriesController@read' );

            /**
             * D: DELETE
             */
            Route::delete( '{id}', 'API\v2\CommodityCategories\APICommodityCategoriesController@delete' );

        } );

        /**
         * C: CREATE
         */
        Route::post( '{id?}', 'API\v2\Commodity\APICommodityController@updateOrCreate' );

        /**
         * U: UPDATE
         */
        Route::put( '{id}', 'API\v2\Commodity\APICommodityController@updateOrCreate' );

        /**
         * R: READ (HERE)
         */
        Route::get( '{id?}', 'API\v2\Commodity\APICommodityController@read' );

        /**
         * D: DELETE
         */
        Route::delete( '{id}', 'API\v2\Commodity\APICommodityController@delete' );
    } );