1

For now in Laravel i am testing url and in route i have

Route::group(['prefix' => 'game'], function (){ 
 Route::get('/location/{location?}/action/{action?}/screen/{screen?}','GameController@index')->name('game.index');
});

In controller when i wanna pass params i have to type

example.com/game/location/1/action/update/screen/main

if i wanna pass only location and screen i have an error cause in url second param should be action.

I can create url like

example.com/game/?location=1&screen=main

and controller $request->screen and location works fine. But is any way to not using & ? and do this like:

example.com/game/location/1/screen/main
Prafulla Kumar Sahu
  • 9,321
  • 11
  • 68
  • 105
Wraith
  • 504
  • 6
  • 28

2 Answers2

1

For this route

Route::get('/locations/{location?}/actions/{action?}/screens/{screen?}','GameController@index')->name('locations.actions.screens.show');

In GameController index method, you can get these parameter as

public function index(Location $location, Action $action, Screen $screen) {
    // here you can use those models
}

if you are using route-model binding,

if not using

public function index($location, $action, $screen) {
    // here you can use these variables
}

If the route name is locations.actions.screens.show then in view, it will be

<a href="{{ route('locations.actions.screens.show', ['location' => $location, 'action' => $action, 'screen' => $screen ]) }}">Test</a>

Now, if you have some query parameter

then it will be like $http://example.com/?test="some test data"&another_test="another test"

you can access these parameter like

public function myfunction(Request $request) {
    dd($request->all());
}

Let's consider you want to retrieve all games, that belongs to a particular screen which belongs to a particular action and that belongs to a particular location, what your urls seems to be in your question, in that case, the url will be

Route::group(['prefix' => 'game'], function (){
 Route::get('locations/{location?}/actions/{action?}/screens/{screen?}','GameController@index')->name('game.index');
});

url seems to be game/locations/1/actions/1/screens/1 where action and screen parameter can be optional

now in your controller index() method

public function index(Location $location, Action $action=null, Screen $screen=null) {
    //using the model instance you received, you can retrieve your games here
}
Prafulla Kumar Sahu
  • 9,321
  • 11
  • 68
  • 105
1

Your error makes sense

URL second param should be action

because your route is with wild card location, action, screen

Route::group(['prefix' => 'game'], function (){ 
 Route::get('/location/{location?}/action/{action?}/screen/{screen?}','GameController@index')->name('game.index');
});

To access this route you have to generate a URL with a wild card like

example.com/game/location/1/screen/main

and example.com/game/?location=1&screen=main not going to work because of your route URL and you can't access like $request->screen.

so your controller must be like

public function index($reuest, $location, $action, $screen){

}

You can directly access $location, $action, $screen and if you request something like

example.com/game/location/1/screen/main?param1=1&param2=2

Those are accessible through request like $request->param1 and $request->param2

Occasionally you may need to specify a route parameter, but make the presence of that route parameter optional. You may do so by placing a ? mark after the parameter name. Make sure to give the route's corresponding variable a default value:

Route::get('user/{name?}', function ($name = null) {
    return $name;
});

you can use Pattern Based Filters You may also specify that a filter applies to an entire set of routes based on their URI.

Route::filter('admin', function()
{
    //
});

Route::when('admin/*', 'admin');
ANIK ISLAM SHOJIB
  • 3,002
  • 1
  • 27
  • 36