繁体   English   中英

Laravel-检查URL中是否存在$ id?

[英]Laravel - Check if $id is exist in the URL?

在继续进行操作之前,如何检查URL中是否存在$ id?

在路线中,我有:

Route::get('list', 'ListController@showList');
Route::get('list/detail/{id}', 'ListController@showListDetail');

在控制器文件中,我有:

   public function showListDetail($id = null)
    {

        if ($id == null)  {
            return Redirect::to("/");
        }

        echo "Test - Found ID: $id";
    }

在浏览器中,当我输入: http://project.dev/list/detail : http://project.dev/list/detail ,出现错误:

Symfony \ Component \ HttpKernel \ Exception \ NotFoundHttpException

但是,当我尝试http://project.dev/list/detail/123它工作正常。

通过这样设置路由,使id -parameter为可选:

Route::get('list/detail/{id?}', 'ListController@showListDetail');

有关更多详细信息,请参阅有关路由参数的文档

将您的路线更改为以下内容,这使ID为可选:

Route::get('list/detail/{id?}', 'ListController@showListDetail');

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM