php - 捕获 FatalErrorException PHP

标签 php laravel

大家好! 在我的 Laravel 应用程序中,我有一个 Excel 文件的上传功能。我从网上获得了这段代码并将其调整到我的应用程序中。问题是,它没有捕捉到用户提交文件但尚未选择文件时产生的 fatal error 异常。我不明白为什么它没有被捕获。我将添加我的 Controller 的一部分。

 public function upload() {
        $file = array('thefile' => Input::file('thefile'));
        $rules = array('excel' => 'excel');
        $validator = Validator::make($file, $rules);
        if ($validator->fails()) {
            return Redirect::to('UploadExcelFile')->withInput()->withErrors($validator);
        }
        else {
            try{
            // FatalErrorException happens in this line!
            if (Input::file('thefile')->isValid()) {
                $destinationPath = 'uploads';
                $fileName = Input::file('thefile')->getClientOriginalName();
                Input::file('thefile')->move($destinationPath, $fileName);
                Session::flash('success', 'Upload successfully');
                $fileNameJSON = exec("python /path/to/script/ExcelToJSON3.py $fileName"); // This returns a full path name of the JSON file that is made...
                if ($fileNameJSON == null){
                    return Redirect::to('/dashboard/input');
                }
                else {

                //      Getting the ID from the file that is uploaded
                try {
                    $jsonDecode = json_decode(file_get_contents($fileNameJSON));
                } catch (ErrorException $e){
                    return Redirect::to('/errorpage')->with(array('status'=> 'ErrorException'));
                }


A lot of code for handling the data entered.... 

                }catch (FatalErrorException $e){
                        return Redirect::to('/errorpage')->with(array('status'=> 'FatalErrorException'));

            }    
        }
        return true;
    }

给出的错误:

UploadExcelFileController.php 第 35 行中的 FatalErrorException: 在非对象上调用成员函数 isValid()

所以,我不明白为什么这段代码不处理错误异常以及我该如何解决这个问题!

最佳答案

除非您已导入使用“use”声明 FatalErrorException 的命名空间,否则您将需要确定异常的范围,如下所示:

catch (\Symfony\Component\Debug\Exception\FatalErrorException $e) {

否则,您正在使用您的类所在的任何 namespace ,并试图捕获在该 namespace 中声明的异常。看起来您的 ErrorException 设置类似。

我不确定上面的 namespace 是否是您的类派生的 namespace ,我只是猜测这是因为您使用的是 Laravel。

关于php - 捕获 FatalErrorException PHP,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40605363/

相关文章:

php - DOMPDF loadView() 错误 - undefined variable : data

php - Laravel 在翻译字符串中使用翻译器

php - 尝试计算并返回表中的行数

php - WordPress admin-ajax.php 错误请求 400

php - CSS Sprites 和 PHP 使用动态菜单突出显示不使用循环功能

php - Laravel 5.2 Eloquent 地将日期时间与当前数据和时间进行比较

Laravel 和完整的 Paypal/支付解决方案

php - 如何在 Laravel 中获取大于 x 的记录数(使用 MySQL 查询)

php - 如何在 SSL 页面上使用不安全的 Web 脚本

javascript - AJAX POST 请求未在 post 路由中调用 Controller 方法