响应

时间:2016-02-28 22:06:33

标签: php laravel

我使用的是Laravel 5.1,由于某种原因,我不时会收到此错误:

  

Response.php第397行中的UnexpectedValueException:   Response内容必须是实现__toString()的字符串或对象," boolean"给出。

令人非常沮丧的是,这是非常不可预测的,并且它只是偶尔发生 - 所以它甚至很难调试,因为它不一致。我有一条简单的路径来转储翻译文件,如下所示:

Route::get('test', function() {
    return trans('restaurant');
});

如果我不断刷新页面,它会在大多数时间显示输出正常。但是,每20到30个电话,我就会收到错误!有人可以在这里说清楚吗?

这是堆栈跟踪:

UnexpectedValueException in Response.php line 397:
The Response content must be a string or object implementing __toString(), "boolean" given.
in Response.php line 397
at Response->setContent(false) in Response.php line 54
at Response->setContent(array(...)) in Response.php line 200
at Response->__construct(array(...)) in Router.php line 1229
at Router->prepareResponse(object(Request), array(...)) in Router.php line 709
at Router->Illuminate\Routing\{closure}(object(Request))
at call_user_func(object(Closure), object(Request)) in Pipeline.php line 139
at Pipeline->Illuminate\Pipeline\{closure}(object(Request)) in LocaleSettings.php line 30
at LocaleSettings->handle(object(Request), object(Closure))
at call_user_func_array(array(object(LocaleSettings), 'handle'), array(object(Request), object(Closure))) in Pipeline.php line 124
at Pipeline->Illuminate\Pipeline\{closure}(object(Request)) in LaravelLocalizationRedirectFilter.php line 53
at LaravelLocalizationRedirectFilter->handle(object(Request), object(Closure))
at call_user_func_array(array(object(LaravelLocalizationRedirectFilter), 'handle'), array(object(Request), object(Closure))) in Pipeline.php line 124
at Pipeline->Illuminate\Pipeline\{closure}(object(Request)) in LocaleSessionRedirect.php line 25
at LocaleSessionRedirect->handle(object(Request), object(Closure))
at call_user_func_array(array(object(LocaleSessionRedirect), 'handle'), array(object(Request), object(Closure))) in Pipeline.php line 124
at Pipeline->Illuminate\Pipeline\{closure}(object(Request))
at call_user_func(object(Closure), object(Request)) in Pipeline.php line 103
at Pipeline->then(object(Closure)) in Router.php line 710
at Router->runRouteWithinStack(object(Route), object(Request)) in Router.php line 675
at Router->dispatchToRoute(object(Request)) in Router.php line 635
at Router->dispatch(object(Request)) in Kernel.php line 236
at Kernel->Illuminate\Foundation\Http\{closure}(object(Request))
at call_user_func(object(Closure), object(Request)) in Pipeline.php line 139
at Pipeline->Illuminate\Pipeline\{closure}(object(Request)) in Debugbar.php line 49
at Debugbar->handle(object(Request), object(Closure))
at call_user_func_array(array(object(Debugbar), 'handle'), array(object(Request), object(Closure))) in Pipeline.php line 124
at Pipeline->Illuminate\Pipeline\{closure}(object(Request)) in VerifyCsrfToken.php line 50
at VerifyCsrfToken->handle(object(Request), object(Closure))
at call_user_func_array(array(object(VerifyCsrfToken), 'handle'), array(object(Request), object(Closure))) in Pipeline.php line 124
at Pipeline->Illuminate\Pipeline\{closure}(object(Request)) in ShareErrorsFromSession.php line 49
at ShareErrorsFromSession->handle(object(Request), object(Closure))
at call_user_func_array(array(object(ShareErrorsFromSession), 'handle'), array(object(Request), object(Closure))) in Pipeline.php line 124
at Pipeline->Illuminate\Pipeline\{closure}(object(Request)) in StartSession.php line 62
at StartSession->handle(object(Request), object(Closure))
at call_user_func_array(array(object(StartSession), 'handle'), array(object(Request), object(Closure))) in Pipeline.php line 124
at Pipeline->Illuminate\Pipeline\{closure}(object(Request)) in AddQueuedCookiesToResponse.php line 37
at AddQueuedCookiesToResponse->handle(object(Request), object(Closure))
at call_user_func_array(array(object(AddQueuedCookiesToResponse), 'handle'), array(object(Request), object(Closure))) in Pipeline.php line 124
at Pipeline->Illuminate\Pipeline\{closure}(object(Request)) in CheckForMaintenanceMode.php line 44
at CheckForMaintenanceMode->handle(object(Request), object(Closure))
at call_user_func_array(array(object(CheckForMaintenanceMode), 'handle'), array(object(Request), object(Closure))) in Pipeline.php line 124
at Pipeline->Illuminate\Pipeline\{closure}(object(Request))
at call_user_func(object(Closure), object(Request)) in Pipeline.php line 103
at Pipeline->then(object(Closure)) in Kernel.php line 122
at Kernel->sendRequestThroughRouter(object(Request)) in Kernel.php line 87
at Kernel->handle(object(Request)) in index.php line 54

1 个答案:

答案 0 :(得分:0)

添加异常处理程序。这样您就可以处理发生错误时要显示的数据。

Route::get('test', function() {
        $response = trans('restaurant')
        try{
          return $response;
        } 
        catch(Exception $e) {
           return [];
        }
    });