Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/271.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php Laravel 5.4未在get router中设置默认id_Php_Laravel_Model View Controller - Fatal编程技术网

Php Laravel 5.4未在get router中设置默认id

Php Laravel 5.4未在get router中设置默认id,php,laravel,model-view-controller,Php,Laravel,Model View Controller,我正在学习Laravel,并试图为$id设置一个默认值 例如: Route::get('/user/{id?}/', function ($id=1) { echo 'user '. $id; }); 但是,当我打开http://localhost/laravel/blog/public/user,我只看到用户,而不是用户1。你知道为什么会这样吗 Route::get('test/{id?}', function($id = 1) { echo 'test '.$id; });

我正在学习Laravel,并试图为$id设置一个默认值

例如:

Route::get('/user/{id?}/', function ($id=1) {
    echo 'user '. $id;
});
但是,当我打开
http://localhost/laravel/blog/public/user
,我只看到
用户
,而不是
用户1
。你知道为什么会这样吗

Route::get('test/{id?}', function($id = 1) {
    echo 'test '.$id;
});
这对我有用。如果我进入
foo.com/test/2
,它输出
test2
,如果我输入
foo.com/test
,它输出
test1


这对我有用。如果我转到
foo.com/test/2
,它将输出
test 2
,如果我键入
foo.com/test
,它将输出
test 1

,因为
$id
将始终传递给函数,即使它是空的,因此,默认值将不会被指定。好吧,我知道这可能是原因,但在教程中我看到了它的工作原理。你认为他们在以后的版本中改变了什么吗?我有一个类似的路径
/test/{id?}
,导航到
/test
显示
test:1
(就像你的函数声明中一样,
$id=1
)并导航到
/test/5
显示
test:5
;基本上,我不能重现这个。您的文件中是否有其他可能与此冲突的路由?还有@Joe,这是不正确的;像
{id?}
这样的可选URL参数不是这样工作的;如果它在URL中,它将被分配,否则它将默认为函数声明中的任何内容,因此它应该是
user 1
@timliewis OMG!是的,我有另一个
Route::get('/user',函数(){
在我之前测试的同一个文件中。删除它后,当我转到
http://localhost/laravel/blog/public/user
。非常感谢!因为即使函数为空,
$id
也将始终传递给函数,因此不会分配默认值。好的,我知道如何分配了这可能是原因,但在教程中我看到了它的工作原理。你认为他们在更高版本中改变了什么吗?我有一个类似的路线
/test/{id?}
,导航到
/test
显示
test:1
(就像你的函数声明中一样,
$id=1
)导航到
/test/5
会显示
test:5
;基本上,我无法重新创建此文件。您的文件中是否有其他可能与此冲突的路由?还有@Joe,这是不正确的;一个可选的URL参数,如
{id?}
不是这样工作的;如果它在URL中,它将被分配,否则它将默认为函数声明中的任何内容,因此它应该是
user 1
@timliewis OMG!是的,我有另一个
Route::get('/user',function(){
在我之前测试的同一个文件中。删除它后,当我转到
http://localhost/laravel/blog/public/user
。非常感谢!