Laravel - 无法获取 url GET 参数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26410552/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Laravel - cannot get url GET parameters
提问by Joe Fearnley
I am having an issue trying to make a GET request to a route and process the parameters passed in via the URL. Here are two routes I created in routes.php
:
我在尝试向路由发出 GET 请求并处理通过 URL 传入的参数时遇到问题。这是我创建的两条路线routes.php
:
$router->get('/', function() {
$test = \Input::get('id');
dd($test);
});
$router->get('test', function() {
$test = \Input::get('id');
dd($test);
});
When I go to the first URL/route ('/') and pass in some data, 123 prints to the screen:
当我转到第一个 URL/路由 ('/') 并传入一些数据时,屏幕上会打印 123:
http://domain.com/dev/?id=123
When I go to the second ('test') NULL prints to the screen (I've tried '/test' in the routes.php
file as well).
当我转到第二个 ('test') 时,NULL 会打印到屏幕上(我也在文件中尝试过 '/test' routes.php
)。
http://domain.com/dev/test?id=123
A few things to note here:
这里有几点需要注意:
- This installation of Laravel is in a subfolder.
- We are using Laravel 5.
- 这个 Laravel 安装在一个子文件夹中。
- 我们正在使用 Laravel 5。
Any idea why one would work and the other would not?
知道为什么一个会起作用而另一个不起作用吗?
回答by Marcin Nabia?ek
First thing - Laravel 5 is still under active development so you cannot rely on it too much at this moment.
首先 - Laravel 5 仍在积极开发中,所以你现在不能过分依赖它。
Second thing is caching and routes. Are you sure you are running code from this routes?
第二件事是缓存和路由。您确定要从这条路线运行代码吗?
Change this code into:
将此代码更改为:
$router->get('/', function() {
$test = \Input::get('id');
var_dump($test);
echo "/ route";
});
$router->get('test', function() {
$test = \Input::get('id');
var_dump($test);
echo "test route";
});
to make sure messages also appear. This is because if you have annotations with the same verbs and urls they will be used and not the routes you showed here and you may dump something else. I've checked it in fresh Laravel 5 install and for me it works fine. In both cases I have id
value displayed
以确保消息也出现。这是因为如果您有具有相同动词和 url 的注释,它们将被使用,而不是您在此处显示的路由,您可能会转储其他内容。我已经在新的 Laravel 5 安装中检查过它,对我来说它工作正常。在这两种情况下,我都id
显示了价值
回答by Emre Do?an
You can use
您可以使用
Request::path()
to retrieve the Request URI or you can use
检索请求 URI,或者您可以使用
Request::url()
to get the current Request URL.You can check the details from Laravel 5 Documentation in here : http://laravel.com/docs/5.0/requests#other-request-informationand when you did these process you can get the GET parameters and use with this function :
获取当前请求 URL。您可以在此处查看 Laravel 5 文档中的详细信息:http://laravel.com/docs/5.0/requests#other-request-information ,当您执行这些过程时,您可以获得 GET 参数并与此功能一起使用:
function getRequestGET($url){
$parts = parse_url($url);
parse_str($parts['query'], $query);
return $query;
}
For this function thanks to @ruel with this answer : https://stackoverflow.com/a/11480852/2246294
对于此功能,感谢@ruel 提供此答案:https://stackoverflow.com/a/11480852/2246294
回答by ArtisanBay
Try this:
尝试这个:
Route::get('/{urlParameter}', function($urlParameter)
{
echo $urlParameter;
});
Go to the URL/route ('/ArtisanBay'):
转到 URL/路由 ('/ArtisanBay'):
Hope this is helpful.
希望这是有帮助的。