2
votes

In Laravel 4.0 my routes file looks like this:

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

    // define the user resource with no system
    Route::resource('users', 'UserController'); 

    Route::get('me', function () {
        $r = URL::action('UserController@show', [Auth::user()->getAuthIdentifier()]);
        return Redirect::to($r);
    });

    Route::group(['prefix' => 'systems/{systems}'], function(){
        // define the user resource with a system system
        Route::resource('users', 'UserController'); 
    });

});

Which worked perfectly, because I only passed in one parameter to the URL::action method parameters array in the /me route it would use the system-less Route::resource('users...` and if I was to pass in two it would use the system-prefixed version.

But in Laravel 4.1 instead it is returning the route

/v1/systems/[the user id]/users/{users} (the literal placeholder {users}).

What has caused this change/how can I fix it?

Both sets of routes are being registered as the output of php artisan routes has:

 GET    v1/users                           | v1.users.index                     | UserController@index
 GET    v1/users/create                    | v1.users.create                    | UserController@create
 POST   v1/users                           | v1.users.store                     | UserController@store
 GET    v1/users/{users}                   | v1.users.show                      | UserController@show
 GET    v1/users/{users}/edit              | v1.users.edit                      | UserController@edit
 PUT    v1/users/{users}                   | v1.users.update                    | UserController@update
 PATCH  v1/users/{users}                   |                                    | UserController@update
 GET    v1/systems/{systems}/users         | v1.systems.{systems}.users.index   | UserController@index
 POST   v1/systems/{systems}/users         | v1.systems.{systems}.users.store   | UserController@store
 GET    v1/systems/{systems}/users/{users} | v1.systems.{systems}.users.show    | UserController@show
 PUT    v1/systems/{systems}/users/{users} | v1.systems.{systems}.users.update  | UserController@update
 PATCH  v1/systems/{systems}/users/{users} |                                    | UserController@update
 DELETE v1/systems/{systems}/users/{users} | v1.systems.{systems}.users.destroy | UserController@destroy
1
Aren't you supposed to use :systems instead of {systems} - Got The Fever Media

1 Answers

0
votes

You have two routes pointing to the same action:

v1.users.index

and

v1.systems.{systems}.users.index

(take a look at php artisan routes command ouput)

When URL::action function is called, Laravel tries to find the name of the route by looking up in an actionMap array and then generates a link using this name.

actionMap array maps action to name for all routes.

So, what happens is that the latter route definition is used in the actionMap array.

Use URL::route or route helper function to generate links to named routes and also pass route parametes with a key, like this :

['systems' => 'someSystem', 'users' => Auth::user()->getAuthIdentifier() ]


Laravel Named Routes

Laravel URLs