Laravel 5 route not working

This is my route: /api/v1/user

Route::group(['prefix' => 'api/{version}'], function($version){

    if ($version == 'v1') {
        Route::get('user', function(){
            return 123 ;
        });

    } else if ($version == 'v2') {

        Route::get('user', function(){
            return 456 ;
        });

    }
});

      

And this is the error I am getting:

NotFoundHttpException in RouteCollection.php line 145

      

Why is $ version not working?

+3


source to share


2 answers


I think you need to use a different structure for this.

Route::group(['prefix' => 'api/{version}'], function($version) {
    Route::get('user', function($version){
        return $version;
    });
});

      



When trying to group a route, you must use a variable in internal routing. You can filter the version here.

http://laravel.com/docs/master/routing#route-groups

+2


source


As I understand it, the variables of a group route must be passed in the Route :: get function, so you call the grouping with a prefixed variable, each of your Routes in that group has access to this variable as a parameter to their function.

(which makes sense if you are considering the idea of ​​grouping)



So...

Route::group(['prefix' => 'api/{version}'], function(){

        Route::get('user', function($version){
            if($version === 'V1'){
               return 123;
            }
            elseif($version === 'V2'){
               return 456;
            }
        });
    }
});

      

0


source







All Articles