0

I have this route.php:

Route::group(['prefix' => 'v3/page1'], function()
{
    Route::get('page1', 'TestController@page1');
});

Route::group(['prefix' => 'v4/page1'], function()
{
    Route::get('page1', 'TestController@page1');
});

As you can see, there are 2 groups that have the same routes. The only difference is that the prefix is slightly different for each group.

I need a way to pass data from route to controller. In this case Im only interested in passing the "v3" or "v4"-string from route to controller.

I have read a little about before_filter. But Im not sure if it is the right way to go. I can imagine that a solution could be to extract the url (maybe in the constructor for the controller) and from there understand if the prefix is v3 or v4. But I wonder if there is a better way, more a best practice. Maybe something with before_filter?

1
  • 1
    The answer for this question is applicable to yours. Also you can remove the "/page1" part from your prefixes. Commented May 16, 2016 at 7:01

3 Answers 3

3

You can try something like:

Route::group(['prefix' => '{version}/page1'], function(){
    Route::get('page1', 'TestController@page1');
})->where('version', 'v[3|4]');

In your controller you can get the version by $request->version

Sign up to request clarification or add additional context in comments.

Comments

1
Route::group(['prefix' => '{v}/page1'], function()
{
    Route::get('page1', 'TestController@page1');
});

& in your method

public function page1($v) {}

Read more from https://laravel.com/docs/5.2/routing#route-parameters

Comments

1

I would write it like this

Route::group(['prefix' => '{version}'], function() 
{
    Route::get('page1', 'TestController@page1');
});

I wouldn't pass in 'page1' in prefix, as it would mean page1 will show up twice in route. In 'page1($version)' method you should be able to get the version. I haven't tested this though.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.