Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/268.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 如何检测丢失的参数路由并显示相关消息_Php_Laravel_Laravel 4 - Fatal编程技术网

Php 如何检测丢失的参数路由并显示相关消息

Php 如何检测丢失的参数路由并显示相关消息,php,laravel,laravel-4,Php,Laravel,Laravel 4,在使用Laravel 4时,我创建了以下路由并能够获取ID: Route::get('details/{ID}', 'Exchange@details'); 问题是当我在路由中丢失ID时,它会显示:NotFoundHttpException错误。我想避免这种情况。我该如何进行?即使在global.php中,我也添加了以下内容,但不起作用: App::missing(function($exception) { print "Not Found"; // return Response

在使用Laravel 4时,我创建了以下路由并能够获取ID:

Route::get('details/{ID}', 'Exchange@details');
问题是当我在路由中丢失ID时,它会显示:NotFoundHttpException错误。我想避免这种情况。我该如何进行?即使在global.php中,我也添加了以下内容,但不起作用:

App::missing(function($exception)
{
    print "Not Found";
//  return Response::make(
//      View::make('errors/404')
//  , 404);
});
控制器方法

更新:重定向到404的建议有效,但我更感兴趣的是检测丢失的ID,并用正确的消息信息加载我自己的视图,而不是显示普通的404页面。

尝试如下:

App::missing(function($exception)
{
    return View::make('errors.404');
});
该视图位于views/errors/404.blade.php中,请尝试以下操作:

App::missing(function($exception)
{
    return View::make('errors.404');
});

视图位于views/errors/404.blade.php中

我会尝试这样的方法

# file routes.php
Route::get('{ID?}', 'SomeController@getDetails')



# file SomeController.php
function getDetails($ID=null) {
    if empty($ID) {
        return Redirect::to('PREVIOUS PAGE')
            ->withInput()
            ->with('error', 'invalid value');
    }
}

我想试试这样的

# file routes.php
Route::get('{ID?}', 'SomeController@getDetails')



# file SomeController.php
function getDetails($ID=null) {
    if empty($ID) {
        return Redirect::to('PREVIOUS PAGE')
            ->withInput()
            ->with('error', 'invalid value');
    }
}

虽然它确实解决了这个问题,但我更感兴趣的是检测丢失的id并采取适当的措施。怎么做?我接受你的回答。虽然它确实解决了问题,但我更感兴趣的是检测丢失的id并采取适当的措施。怎么做?不过我接受你的回答。在采用这种方法之前,它会出错。我也做了类似的事情。选中更新的问题。然后向参数添加问号,使其成为可选路由::get'details/{ID?}','Exchange@details';伟大的这就是我要找的。我通过不添加?我已将其作为一个强制参数。在使用此方法之前,它会抛出错误。我也做了类似的事情。选中更新的问题。然后向参数添加问号,使其成为可选路由::get'details/{ID?}','Exchange@details';伟大的这就是我要找的。我通过不添加?我把它作为一个强制性参数。