1

今のところLaravelで私はURLをテストしており、ルートでは私が持っています

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

コントローラーでパラメーターを渡したいときは、入力する必要があります

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

場所と画面のみを渡したい場合は、URL にエラーの原因があり、2 番目のパラメーターは action にする必要があります。

次のようなURLを作成できます

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

コントローラー $request->screen と location は正常に機能します。しかし、 & を使用しない方法はありますか? そしてこれを次のようにします:

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

2 に答える 2

1

このルートの場合

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

GameController index メソッドでは、これらのパラメーターを次のように取得できます。

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

ルートモデル バインディングを使用している場合は、

使用しない場合

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

ルート名がlocations.actions.screens.show表示されている場合は、

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

さて、クエリパラメータがある場合

$ http://example.com/?test= "some test data"&another_test="another test" のようになります

これらのパラメーターに次のようにアクセスできます

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

特定のアクションに属し、特定の場所に属している特定の画面に属しているすべてのゲームを取得したいと考えてみましょう。あなたの URL は質問にあると思われます。その場合、URL は次のようになります。

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

url はgame/locations/1/actions/1/screens/1、アクションと画面のパラメーターをオプションにできる場所のようです

コントローラーの index() メソッドで

public function index(Location $location, Action $action=null, Screen $screen=null) {
    //using the model instance you received, you can retrieve your games here
}
于 2019-09-03T11:04:40.790 に答える