nerdoza
nerdoza

Reputation: 924

Laravel class not being compiled on first run

I used the tutorial posted on Net Tuts+ (http://hub.tutsplus.com/tutorials/combining-laravel-4-and-backbone--net-31745) and implemented the Error handling as instructed.

In the composer autoload section, I have the error.php file being loaded. It contains a class called NotFoundException which is used to create exceptions for REST requests.

When I dump the autoload using artisan, it builds fine, and on the first run, I get the error "ReflectionException: Class NotFoundException does not exist". This ONLY happens on the first run after the dump. Subsequent runs are fine and when I run into an instance that triggers the exception, the exception class works. This error does appear each time I run my unit testing though, causing my tests to fail, but it only appears in the first file which calls the exception. I have 2 tests in 2 separate files which run and both use the exception as an assertion, and the first one gets and error, while the second one passes.

I am still very new to Laravel, so it might be a simple fix, but am I missing something? Is there supposed to be another reference that I'm overlooking?

The output from the steps below:

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.    

Here is the errors.php file:

<?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);
    }

}

And finally, my composer.json file:

{
        "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"
}

Upvotes: 1

Views: 9190

Answers (2)

Mahfuz-ur Rahman
Mahfuz-ur Rahman

Reputation: 68

it is old but somebody may need the solution of the problem. Please remove compiled.php then run composer update. It should be OK.

Upvotes: 0

Jan P.
Jan P.

Reputation: 3297

Try the following:

  • Create the errors.php file and code your exceptions
  • Put your file to the autload section of your root composer.json
  • Run in your laravel root:

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

Please report :)

Upvotes: 7

Related Questions