草庐IT

php - 警告 : Missing argument 1 when passing parameters from url to controller (laravel)

coder 2024-04-18 原文

我正在构建这个网站,我想将 url 参数传递给路由以及从路由传递给 Controller ​​,我已经搜索了文档和谷歌,但找不到解决我的问题的方法

这是一个示例 url

locations/search?q=parameter1

我现在的路线是这样的:

Route::group(array('prefix'=>'search'),function()
{
    Route::get('locations/{src?}','SearchController@locations',function($src = null){});
});

我的 Controller 看起来像这样:

    class SearchController extends BaseController {

    public function locations($src)
    {
        return json_encode($src);
    }

}

我想知道的是如何将参数传递给 Controller ​​,我现在的代码返回这个错误

{"error":{"type":"ErrorException","message":"Missing argument 1 for SearchController::locations()","file":"C:\wamp\www\localsite\app\controllers\SearchController.php","line":5}}

提前致谢

最佳答案

您混合了 get 参数和路由变量。在该路由之后您最终会得到类似 search/locations/search/ 的东西,其中第二个 search 作为参数传递并分配给 $src

选项 1
对于您当前的路线,您可以做的是 search/locations/parameter1 以便将 parameter1 传递给 $src

选项 2
或者,如果您需要更改路线以遵循 locations/search/parameter1,您的路线应如下所示:

Route::group(array('prefix'=>'locations'),function()
{
    Route::get('search/{src?}','SearchController@locations',
        function($src = null){
    });
});

然后你可以使用locations/search/parameter1

但是如果你坚持使用获取参数(locations/search?q=parameter1)..

选项 3
你的路线应该是这样的:

Route::group(array('prefix'=>'locations'),function()
{
    Route::get('search','SearchController@locations');
});

和你的 Controller :

class SearchController extends BaseController {

    public function locations()
    {
        $src = Input::get('q');
        return json_encode($src);
    }

}

注意 如果你打算在路由上使 $src 可选,请确保更改行 public function locations($src) => public function locations($src = null)

关于php - 警告 : Missing argument 1 when passing parameters from url to controller (laravel),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20336322/

有关php - 警告 : Missing argument 1 when passing parameters from url to controller (laravel)的更多相关文章

随机推荐