【问题标题】:How get lot of params from url but without (&)?如何从 url 获取大量参数但没有 (&)?
【发布时间】:2020-01-06 06:46:48
【问题描述】:

现在在 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 第二个参数中有一个错误原因应该是行动。

我可以创建像

这样的 url
example.com/game/?location=1&screen=main

和控制器 $request->屏幕和位置工作正常。但是有什么方法可以不使用 & 吗?并这样做:

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

【问题讨论】:

    标签: php laravel laravel-5 routing route-model-binding


    【解决方案1】:

    这条路线

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

    在 GameController 的索引方法中,您可以将这些参数获取为

    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());
    }
    

    假设您要检索属于特定屏幕、属于特定操作且属于特定位置的所有游戏,您的网址在您的问题中似乎是什么,在这种情况下,网址将是

    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
    }
    

    【讨论】:

    • 好的,我该如何使用该网址 example.com/game/locations/1/screens/2 我有一个错误
    • @Wraith 首先告诉我,如果您使用的是路由模型绑定,或者您只是在创建一个 url?
    • 使用路由模型在视图中创建url
    • 在视图中,你可以使用路由名称,让我在1分钟内编辑我的答案。
    • 在您的示例中,我仍然将操作作为参数。我想在路由中转到方法(索引)并从请求中获取参数,而不在路由中绑定参数。也许是不可能的:(
    【解决方案2】:

    你的错误是有道理的

    URL第二个参数应该是action

    因为您的路线带有通配符位置、动作、屏幕

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

    要访问此路由,您必须生成一个带有通配符的 URL,例如

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

    并且example.com/game/?location=1&amp;screen=main 无法工作,因为您的路由 URL 并且您无法像$request-&gt;screen 那样访问。

    所以你的控制器必须像

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

    您可以直接访问$location, $action, $screen,如果您要求类似

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

    这些可以通过请求访问,例如 $request-&gt;param1$request-&gt;param2

    有时您可能需要指定一个路由参数,但该路由参数的存在是可选的。您可以通过放置一个 ?在参数名称后标记。确保给路由对应的变量一个默认值:

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

    您可以使用基于模式的过滤器 您还可以指定一个过滤器根据其 URI 应用于整个路由集。

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

    【讨论】:

    • 这就是重点。如何设置路由以仅接受所有请求而不绑定任何参数
    猜你喜欢
    • 2015-04-18
    • 2019-05-21
    • 2011-05-20
    • 2016-04-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-06-01
    • 2017-03-27
    相关资源
    最近更新 更多