Love Fuel?    Donate

FuelPHP Forums

Ask your question about FuelPHP in the appropriate forum, or help others by answering their questions.
Extending error
  • Truing extend the Controller class 
    i put the class  Extpage into "/var/www/fuel.loc/fuel/app/classes/controller/extpage.php"
    and on config set : 
    'classes'  => array(
                  'Extpage' => APPPATH.'classes/controller/extpage.php',
            ),

    But i got Error 
    Fuel\Core\FuelException [ Error ]: Always load class does not exist. Unable to load: /var/www/fuel.loc/fuel/app/classes/controller/extpage.php

    Whats wrong ? 
    Thankyou
  • You get that error when the file loaded does not contain the class expected. Note that the autoloader is case sensitive, so 'Extpage' !== 'ExtPage'.

    But in general there is no need to always_load classes, FuelPHP will autoload any class required.
    It is only there is case you need the class' _init() to be called in the framework bootstrap process.
  • >>>You get that error when the file loaded does not contain the class expected. Note that the autoloader is case sensitive, so 'Extpage' !== 'ExtPage'.

    -- as you can see i write case-correctly. in this file there is a class named Extpage, but it didnt help at all  

    >>>But in general there is no need to always_load classes, FuelPHP will autoload any class required. 
    It is only there is case you need the class' _init() to be called in the framework bootstrap process.
    -- it was not loaded automatically , thats why i load it with autoloader
  • The code does:
                foreach ($array['classes'] as $class)
                {
                    if ( ! class_exists($class = ucfirst($class)))
                    {
                        throw new \FuelException('Always load class does not exist. Unable to load: '.$class);
                    }
                }
    So nothing fancy. It converts the defined class to ucfirst(), then attempts to load it.

    Ah, want a second... You placed the file in classes/controller. But this is not a controller?

    So, either call it 'Controller_Extpage', or move the file to the classes folder.
  • Actually it is an Controller, simply its extension for my controllers .  
    I tried to Put it in classes ( same error ) also tried to put it in custom folder lib  ( something like for my extensions ) - same fail. 

    So how correctly Extend the controller 
    F.Ex - I have 4 Controllers , which have same methods - so i want to combine them in 1 , and extend them from this extended . 
    Thank you
  • HarroHarro
    Accepted Answer
    The cascading file system works very simple.

    Take the namespace, and the class name, and stick them together with an underscore. Then replace all underscores and backslashes by slashes. Append APPPATH, and add ".php" to it.

    So, a class called 'Test_Class" in the namespace \Something\Special will be loaded from APPPATH.'classes/something/special/test/class.php'.

    This also means you can be very flexible with your naming. \Controller_Test and \Controller\Test point to the same filename.

    If you deviate from this rule, either the file can not be found if you reference the class, or the class can not be detected in the file loaded, which is what you have.

    If you have a class called Extpage in APPPATH.'classes/controller', it MUST be named either 'Controller_Extpage', or '\Controller\Extpage'. If you want your class to be called 'Extpage', it MUST be in APPPATH.'classes/extpage.php'.

    If you think that is all well and you STILL want to deviate from this rule, you will have to tell the autoloader where to find the class. You can do that in your app bootstrap.php:

    Autoloader::add_classes(array(
        'Extpage' => APPPATH.'classes/some/special/folder/othername.php',
    ));

  • thank-you !

Howdy, Stranger!

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

In this Discussion