Laravel类在第一次运行时没有编译


Laravel class not being compiled on first run

我使用了发布在NetTuts+上的教程(http://hub.tutsplus.com/tutorials/combining-laravel-4-and-backbone--net-31745)并按照指示执行错误处理。

在composer autoload部分,我有一个正在加载的error.php文件。它包含一个名为NotFoundException的类,用于为REST请求创建异常。

当我使用artisan转储自动加载时,它构建得很好,在第一次运行时,我会得到错误"ReflectionException:Class NotFoundException不存在"。这只发生在转储后的第一次运行时。后续运行很好,当我运行到触发异常的实例时,异常类就可以工作了。不过,每次运行单元测试时都会出现此错误,导致测试失败,但它只出现在调用异常的第一个文件中。我在两个单独的文件中有两个测试,它们都在运行,并且都将异常用作断言,第一个得到并出错,而第二个通过。

我对拉拉维尔还很陌生,所以这可能是一个简单的解决方案,但我是不是错过了什么?我忽略了另一个参考文献吗?

以下步骤的输出:

root@ubuntu:/var/www/api# composer dump-autoload
Generating autoload files
root@ubuntu:/var/www/api# php artisan clear-compiled
root@ubuntu:/var/www/api# php artisan optimize
Generating optimized class loader
Compiling common classes
root@ubuntu:/var/www/api# phpunit
PHPUnit 3.7.22 by Sebastian Bergmann.
Configuration read from /var/www/api/phpunit.xml
E........................................
Time: 2 seconds, Memory: 46.00Mb
There was 1 error:
1) AuthControllerTest::testProcess
ReflectionException: Class NotFoundException does not exist
/var/www/api/bootstrap/compiled.php:7539
/var/www/api/bootstrap/compiled.php:7533
/var/www/api/bootstrap/compiled.php:7508
/var/www/api/bootstrap/compiled.php:7477
/var/www/api/bootstrap/compiled.php:7472
/var/www/api/vendor/composer/ClassLoader.php:185
/var/www/api/vendor/composer/ClassLoader.php:185
/var/www/api/bootstrap/compiled.php:165
/var/www/api/bootstrap/compiled.php:142
/var/www/api/bootstrap/compiled.php:444
/var/www/api/bootstrap/compiled.php:83
/var/www/api/bootstrap/compiled.php:163
/var/www/api/bootstrap/compiled.php:142
/var/www/api/bootstrap/compiled.php:444
/var/www/api/bootstrap/compiled.php:186
/var/www/api/bootstrap/compiled.php:175
/var/www/api/bootstrap/compiled.php:142
/var/www/api/bootstrap/compiled.php:444
/var/www/api/bootstrap/compiled.php:4763
/var/www/api/bootstrap/compiled.php:7834
/var/www/api/bootstrap/compiled.php:7821
/var/www/api/bootstrap/compiled.php:4775
/var/www/api/bootstrap/compiled.php:483
/var/www/api/bootstrap/compiled.php:490
/var/www/api/vendor/symfony/http-kernel/Symfony/Component/HttpKernel/Client.php:82
/var/www/api/vendor/symfony/browser-kit/Symfony/Component/BrowserKit/Client.php:324
/var/www/api/vendor/laravel/framework/src/Illuminate/Foundation/Testing/TestCase.php:58
/var/www/api/app/tests/controllers/AuthControllerTest.php:22
/usr/share/php/PHPUnit/TextUI/Command.php:192
/usr/share/php/PHPUnit/TextUI/Command.php:130
FAILURES!                                                                                              
Tests: 41, Assertions: 38, Errors: 1.    

这是errors.php文件:

<?php
class PermissionException extends Exception {
    public function __construct($message = null, $code = 403)
    {
        parent::__construct($message ? : 'Action not allowed', $code);
    }
}
class ValidationException extends Exception {
    protected $messages;
    public function __construct($validator)
    {
        $this->messages = $validator->messages();
        parent::__construct($this->messages, 400);
    }
    public function getMessages()
    {
        return $this->messages;
    }
}
class NotFoundException extends Exception {
    public function __construct($message = null, $code = 404)
    {
        parent::__construct($message ? : 'Resource Not Found', $code);
    }
}

最后,我的composer.json文件:

{
        "name": "laravel/laravel",
        "description": "The Laravel Framework.",
        "keywords": ["framework", "laravel"],
        "require": {
            "laravel/framework": "4.0.*",
            "way/generators": "dev-master",
            "twitter/bootstrap": "dev-master",
            "conarwelsh/mustache-l4": "dev-master",
            "hybridauth/hybridauth": "*"
        },
        "require-dev": {
            "phpunit/phpunit": "3.7.*",
            "mockery/mockery": "0.7.*"
        },
        "autoload": {
                "classmap": [
                        "app/commands",
                        "app/controllers",
                        "app/models",
                        "app/database/migrations",
                        "app/database/seeds",
                        "app/tests/TestCase.php",
                        "app/ifr/interfaces",
                        "app/ifr/repositories",
                        "app/errors.php"
                ]
        },
        "scripts": {
            "post-install-cmd": [
                "php artisan optimize"
            ],
            "pre-update-cmd": [
                "php artisan clear-compiled"
            ],
            "post-update-cmd": [
                "php artisan optimize"
            ],
            "post-create-project-cmd": [
                "php artisan key:generate"
            ]
        },
        "config": {
                "preferred-install": "dist"
        },
        "minimum-stability": "dev"
}

尝试以下操作:

  • 创建errors.php文件并对异常进行编码
  • 将文件放在根composer.json的autload部分
  • 在你的laravel根中运行:

    php composer.phar dump-autoload
    php artisan clear-compiled
    php artisan optimize
    

请报告:)

它很旧,但可能有人需要解决问题。请删除compiled.php,然后运行composer更新。应该没问题。