TIP: Use Markdown or, <pre> for multi line code blocks / <code> for inline code.
These forums are read-only and for archival purposes only!
Please join our new forums at discourse.kohanaframework.org
Autoloader problem
  • Im currently deploying my kohana website with nginx, mysql... pretty usual you'll say!

    I got an error telling me that Class 'Kohana' not found. However, the autoloader is able to load many classes before it fails. Look at this log (I print every classes that worked.. its fails at Kohana_HTML) :

    I18n Kohana_I18n Debug Kohana_Debug Date Kohana_Date

    ErrorException [ Fatal Error ]: html Kohana_HTML Class 'Kohana' not found

    Can you help me out please

  • You messed up the directory/filename structure.

    Post what the class is called, and where it is (starting from root), ie. root/application/classes/kohana.php would be one possible path for the class Kohana.

  • I don't get what you say. My directory/filename structure is okay. I did not change anything in kohana's system. Its works well on my localhost...

  • In 3.0 the Kohana class is required in index.php In 3.1 the Kohana class is required in bootstrap.php

    Are those lines intact? Are those files' contents it tries to require intact?

    PS. if you do not have a APPPATH/classes/kohana.php, that is ok the system folder (should) contain(s) one, if you do, difines it a Kohana class?

  • No I don't define any else Kohana class

    I got this in my bootstrap which is ok :

    require SYSPATH.'classes/kohana/core'.EXT;
    
    if (is_file(APPPATH.'classes/kohana'.EXT))
    {
        // Application extends the core
        require APPPATH.'classes/kohana'.EXT;
    }
    else
    {
        // Load empty core extension
        require SYSPATH.'classes/kohana'.EXT;
    }
    

    And I repeat that it works well on every developer's computer... Also, it fails at Kohana_HTML Class 'Kohana' not found after a sucessfull load of : I18n Kohana_I18n Debug Kohana_Debug Date Kohana_Date

    Thank you again

  • Doesn't the stack trace give you a filename and line number that you can follow to the source?

  • Yep

    ErrorException [ Fatal Error ]: Class 'Kohana' not found
    
    SYSPATH/classes/kohana/core.php [ 492 ]
    
    488         try
    489         {
    490             // Transform the class name into a path
    491             $file = str_replace('_', '/', strtolower($class));
    492             if ($path = Kohana::find_file('classes', $file))
    493             {
    494                 // Load the class file
    495                 require $path;
    496 
    
    
  • File permissions?

  • The Kohana class should never be autoloaded. It's manually loaded in index.php.

  • Are those files' contents it tries to require intact?

    Maybe something went wrong while uploading/whatever.

    And I repeat that it works well on every developer's computer...

    Could it be that the server is case sensitive and non of the developer's computers is? Maybe you have a APPPATH/classes/Kohana.php instead of a APPPATH/classes/kohana.php Now I think about this a little more, that can not cause it.... I'll leave this in just in case it gives somebody else an idea.

    It would help if you post more of the stack trace.

    Also what have you registred as your autoloader? By default Kohana::auto_load() is the autoloader, but since your autoloader is trying to autoload the Kohana class I do not see how you could have Kohana::auto_load() as your autoloader.

    Basically... RUN No, wait... I meant to say "Basically we can be more a help with more info."

  • The line error is straight from Kohana_Core::auto_load function. The process is this: Kohana_Core is loaded then either SYSPATH/classes/kohana.php is loaded, or APPPATH/classes/kohana.php is loaded (in the eventuality that it exists). The autoload function is then set to Kohana::auto_load (yes not the core). So let's say we load a class Foo afterwards: Kohana::auto_load('Foo') is called, so how can that throw an error of Class 'Kohana' not found inside the method; obviously it was loaded. Something's fishy.

    Did you edit the bootstrap? What's the full stacktrace?

    @zombor

    It's in bootstrap.php. But you're right, it should be in index.php. *moves code to index.php*

  • Use xdebug and figure it out. Stop guessing everyone.

  • I'm running KO3.1

    @Darsstar I think also it may be something wrong with case sensitive... but everything seems ok with the name of my files.

    @Akki You described my problem perfectly.

    Here is the complete error/stacktrace

    ErrorException [ Fatal Error ]: Class 'Kohana' not found
    
    SYSPATH/classes/kohana/core.php [ 492 ]
    
    487     {
    488         try
    489         {
    490             // Transform the class name into a path
    491             $file = str_replace('_', '/', strtolower($class));
    492             if ($path = Kohana::find_file('classes', $file))
    493             {
    494                 // Load the class file
    495                 require $path;
    496 
    497                 // Class has been found
    
        {PHP internal call} » Kohana_Core::shutdown_handler()
    
    Environment
    

    And now my boostrap

    <?php defined('SYSPATH') or die('No direct script access.');
    
    // -- Environment setup --------------------------------------------------------
    
    // Load the core Kohana class
    require SYSPATH.'classes/kohana/core'.EXT;
    
    if (is_file(APPPATH.'classes/kohana'.EXT))
    {
        // Application extends the core
        require APPPATH.'classes/kohana'.EXT;
    }
    else
    {
        // Load empty core extension
        require SYSPATH.'classes/kohana'.EXT;
    }
    
    /**
     * Set the default time zone.
     *
     * @see  http://kohanaframework.org/guide/using.configuration
     * @see  http://php.net/timezones
     */
    date_default_timezone_set('America/Chicago');
    
    /**
     * Set the default locale.
     *
     * @see  http://kohanaframework.org/guide/using.configuration
     * @see  http://php.net/setlocale
     */
    setlocale(LC_ALL, 'en_US.utf-8');
    
    /**
     * Enable the Kohana auto-loader.
     *
     * @see  http://kohanaframework.org/guide/using.autoloading
     * @see  http://php.net/spl_autoload_register
     */
    spl_autoload_register(array('Kohana', 'auto_load'));
    
    /**
     * Enable the Kohana auto-loader for unserialization.
     *
     * @see  http://php.net/spl_autoload_call
     * @see  http://php.net/manual/var.configuration.php#unserialize-callback-func
     */
    ini_set('unserialize_callback_func', 'spl_autoload_call');
    
    // -- Configuration and initialization -----------------------------------------
    
    /**
     * Set the default language
     */
    I18n::lang('en-us');
    
    /**
     * Set Kohana::$environment if a 'KOHANA_ENV' environment variable has been supplied.
     *
     * Note: If you supply an invalid environment name, a PHP warning will be thrown
     * saying "Couldn't find constant Kohana::<INVALID_ENV_NAME>"
     */
    if (isset($_SERVER['KOHANA_ENV']))
    {
        Kohana::$environment = constant('Kohana::'.strtoupper($_SERVER['KOHANA_ENV']));
    }
    
    /**
     * Initialize Kohana, setting the default options.
     *
     * The following options are available:
     *
     * - string   base_url    path, and optionally domain, of your application   NULL
     * - string   index_file  name of your index file, usually "index.php"       index.php
     * - string   charset     internal character set used for input and output   utf-8
     * - string   cache_dir   set the internal cache directory                   APPPATH/cache
     * - boolean  errors      enable or disable error handling                   TRUE
     * - boolean  profile     enable or disable internal profiling               TRUE
     * - boolean  caching     enable or disable internal caching                 FALSE
     */
    include('config/config.php');
    
    /**
     * Attach the file write to logging. Multiple writers are supported.
     */
    Kohana::$log->attach(new Log_File(APPPATH.'logs'));
    
    /**
     * Attach a file reader to config. Multiple readers are supported.
     */
    Kohana::$config->attach(new Config_File);
    
    /**
     * Enable modules. Modules are referenced by a relative or absolute path.
     */
    Kohana::modules(array(
        'auth'                      => MODPATH.'auth',           // Basic authentication
        // 'cache'          => MODPATH.'cache',          // Caching with multiple backends
        // 'codebench'  => MODPATH.'codebench',  // Benchmarking tool
        'database'              => MODPATH.'database',   // Database access
        'image'                     => MODPATH.'image',          // Image manipulation
        'multitemplate'     => MODPATH.'multitemplate',
        'orm'                           => MODPATH.'orm',                // Object Relationship Mapping
        'migrations'      => MODPATH.'migrations', //Migration system
        // 'unittest'       => MODPATH.'unittest',   // Unit testing
        // 'userguide'  => MODPATH.'userguide',  // User guide and API documentation
        'mailer'                    => MODPATH.'mailer',
        'purifier'              => MODPATH.'purifier', //html purifier (http://htmlpurifier.org/)
        ));
    
    /**
     * Set the routes. Each route must have a minimum of a name, a URI and a set of
     * defaults for the URI.
     */
    
    A LOT OF ROUTES HERE
    
    //Catch all possible route before default, so let default at the end.
    Route::set('default', '(<controller>(/<action>(/<id>)))')
        ->defaults(array(
            'controller' => 'mycontroller',
            'action'     => 'index'
        ));
    

    And now the index.php file

    <?php
    
    /**
     * The directory in which your application specific resources are located.
     * The application directory must contain the bootstrap.php file.
     *
     * @see  http://kohanaframework.org/guide/about.install#application
     */
    $application = 'application';
    
    /**
     * The directory in which your modules are located.
     *
     * @see  http://kohanaframework.org/guide/about.install#modules
     */
    $modules = 'modules';
    
    /**
     * The directory in which the Kohana resources are located. The system
     * directory must contain the classes/kohana.php file.
     *
     * @see  http://kohanaframework.org/guide/about.install#system
     */
    $system = 'system';
    
    /**
     * The default extension of resource files. If you change this, all resources
     * must be renamed to use the new extension.
     *
     * @see  http://kohanaframework.org/guide/about.install#ext
     */
    define('EXT', '.php');
    
    /**
     * Set the PHP error reporting level. If you set this in php.ini, you remove this.
     * @see  http://php.net/error_reporting
     *
     * When developing your application, it is highly recommended to enable notices
     * and strict warnings. Enable them by using: E_ALL | E_STRICT
     *
     * In a production environment, it is safe to ignore notices and strict warnings.
     * Disable them by using: E_ALL ^ E_NOTICE
     *
     * When using a legacy application with PHP >= 5.3, it is recommended to disable
     * deprecated notices. Disable with: E_ALL & ~E_DEPRECATED
     */
    error_reporting(E_ALL | E_STRICT);
    
    /**
     * End of standard configuration! Changing any of the code below should only be
     * attempted by those with a working knowledge of Kohana internals.
     *
     * @see  http://kohanaframework.org/guide/using.configuration
     */
    
    // Set the full path to the docroot
    define('DOCROOT', realpath(dirname(__FILE__)).DIRECTORY_SEPARATOR);
    
    // Make the application relative to the docroot, for symlink'd index.php
    if ( ! is_dir($application) AND is_dir(DOCROOT.$application))
        $application = DOCROOT.$application;
    
    // Make the modules relative to the docroot, for symlink'd index.php
    if ( ! is_dir($modules) AND is_dir(DOCROOT.$modules))
        $modules = DOCROOT.$modules;
    
    // Make the system relative to the docroot, for symlink'd index.php
    if ( ! is_dir($system) AND is_dir(DOCROOT.$system))
        $system = DOCROOT.$system;
    
    // Define the absolute paths for configured directories
    define('APPPATH', realpath($application).DIRECTORY_SEPARATOR);
    define('MODPATH', realpath($modules).DIRECTORY_SEPARATOR);
    define('SYSPATH', realpath($system).DIRECTORY_SEPARATOR);
    
    // Clean up the configuration vars
    unset($application, $modules, $system);
    
    if (file_exists('install'.EXT))
    {
        // Load the installation check
        return include 'install'.EXT;
    }
    
    /**
     * Define the start time of the application, used for profiling.
     */
    if ( ! defined('KOHANA_START_TIME'))
    {
        define('KOHANA_START_TIME', microtime(TRUE));
    }
    
    /**
     * Define the memory usage at the start of the application, used for profiling.
     */
    if ( ! defined('KOHANA_START_MEMORY'))
    {
        define('KOHANA_START_MEMORY', memory_get_usage());
    }
    
    // Bootstrap the application
    require APPPATH.'bootstrap'.EXT;
    
    /**
     * Execute the main request. A source of the URI can be passed, eg: $_SERVER['PATH_INFO'].
     * If no source is specified, the URI will be automatically detected.
     */
    echo Request::factory()
        ->execute()
        ->send_headers()
        ->body();
    
    
  • Really, stop guessing and flailing. Use XDebug to step through the code and see what is really causing it. It's likely a "hidden" error that php is infamous for, happening inside an exception or shutdown handler.

  • @zombor

    Really sorry for this. I did not see your message before sending this. I'll try to figure this out myself. I tought someone here may have seen a similar problem in the past. I'm not guessing at all.

  • If you set 'errors' => FALSE in the kohana init function. Does it give the same error message?

  • @Akki Now I'm getting a database error. Seems good to me... but I don't think disabling errors is the best solution. Don't you think?

  • Yes that's true, but I think you don't understand the purpose.

    You didn't disable errors with that option, the idea is to (temporarily) reverted the system to standard PHP errors. Because Kohana has this stacking system where even very critical functions can be replaced there's potential for a vicious error loops occurring. Simple example: you overwrite HTML in your application, and make a typo, HTML doesn't parse, an error is thrown, the error handler is called, and it displays an error page, which just happens to use the class HTML, so you don't get the error page, instead you get a ambiguous error with little information. By setting 'errors' => FALSE kohana won't attach it's error handlers, so now the loop is broken.

    Fix your database error, and any others. Then revert back; see if you still get errors.

  • @Akki Wow!! That last post solve my problem. In fact, I was just ignoring that Kohana works that way. I turned off debug, then fixed all my errors and put back the setting to TRUE and everything is working perfect now. Thank you so much for your help!!

  • Too bad you didn't learn how to use xdebug ;)

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

In this Discussion