Love Fuel?    Donate

FuelPHP Forums

Ask your question about FuelPHP in the appropriate forum, or help others by answering their questions.
Bootstap a module
  • I've been working with Fuel for a personal/business project and have utilised modules throughout the app. I'm at the stage now where I'm adding quite a few custom classes to the project per module, as they are very module specific. What I would like to achieve is to autoload the classes, for the module. This way I can utilize the class throughout the module, even if I had, say 5+ controllers. There would be no need to autoload each class per controller. I do know of the bootstrap.php file for the app, and that I can autoload classes from there. I do know about the config.php file 'always_load' config setting, but classes are autoloaded and initiated from there. My main question would be, how useful would this be to the framework and to others? If its proven useful, I'd be happy to do a pull request with an implementation, or if someone is quicker, that could provide a solution.
  • I'm not sure I understand what you exactly want. You don't load anything in FuelPHP. Ever. You just use it, and FuelPHP will make sure it's at your disposal. So in any class in your application you can call \Module\Class::method and it would work (in case of a module, if the module was added previously). The only reason for manually autoloading a class would be if you have code in the classes _init method that needs to be executed before other classes are used. But I consider that poor design, it should not be needed.
  • I've built out my own ORM package, rather than using the ORM package available on the Fuel repo. In the structure I'm putting my models in sub folders, say; users model, go into model/users/users.php and so does the user model, model/users/user.php. I can't get Fuel to load these classes without defining them myself.
  • FuelPHP's autoloader uses a cascading filesystem approach. You model must therefore be called Users_User to be autoloaded. If you want non-standard naming conventions, the easiest route will probably be to extend the autoloader, and update it so it can find and load your files.
  • If you want non-standard naming conventions, the easiest route will probably be to extend the autoloader, and update it so it can find and load your files.

    Best way in those cases is to append your own autoloader using spl_autoload_register() - modifying the Fuel autoloader should only be done when there's no other way, otherwise wiser to avoid.
  • If you take that route, you'll have to be very careful that that autoloader doesn't pick up any other classes then the intended ones...
  • By default it's the second autoloader and Fuel's is efficient enough, unless you forceably prefix it of course.
  • Overriding Autoloader::class_to_path would seem the easiest thing to do, in my opinion anyway. I'll have a further look into it, but for now the app bootstrap.php file will have to do, until I find a better method to load classes within modules (no, loading it in the controller is not something I want to do).
  • As I said I would strongly advice against that. You may break the entirety of Fuel by a small mistake and we don't consider the internal workings of a class part of BC to be maintained (only the public interface is). Thus adding your own autoloader on top of Fuel's is a far safer way as you won't ever get in trouble when updating Fuel.

Howdy, Stranger!

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

In this Discussion