I'm trying to write a shopping platform using FuelPHP, the way PyroCMS uses CodeIgniter. However, I want the controllers I write to be extendable, the same way the core controllers are with an app at the moment.
I've figured it out with Views and Configs, by changing
static::$instance = static::forge(array(APPPATH, COREPATH));
to
static::$instance = static::forge(array(MYSHOPPATH, APPPATH, COREPATH));
It'll search /fuel/myshop/views/ before searching /fuel/app/views/, then /fuel/core/views/.
How do I achieve the same thing with Controllers and Models? I want to distribute this so that people can easily extend it without overwriting anything. In theory, they should be able to update the app I write and FuelPHP so it's always up-to-date, without loosing any of their code.
For extending an app, modules are the way to go. Without modifying the autoloader, you can not have Controllers in other locations.
There are some tricks you can use when you use namespaces for your controllers, but that will become complex quickly.
In short, you can replace Controller_This_That by \Controller\This\That, so a class called That in the namespace \Controller\This. And you can convice the autoloader to load classes in this namespace from elsewhere.
Amazing.
Is it then possible for the developer to extend the modules I write? If I list more than one location, will it filter through them until it finds the module?
Everything in FuelPHP (except a view) is a class, so it can extend any other class.
To extend something in a package or a module, is must be loaded first. If your goal is to provide some sort of higher order framework for people to build on, perhaps the best thing to is is to build your framework core in a package, in it's own namespace.
That way it's out of the way of everything in app, yet both app and module classes can easily extend it:
namespace Module;
class Controller_This extends \Framework\Controller_That {}
Thanks WanWizard. I've got two issues so far:
1. Using the Modules method, I want to be able to just copy one file from a module into a different folder, but it appears that I have to copy the entire module if I want it to run for a different folder (not app/modules). Is there a way for modules to be split in two?
2. Using the extending idea, is it possible to overwrite a function previously defined? I've realised that most of the time when people are "extending" this framework, they'll actually just be replacing the way something works rather than adding a new function.
1. No. A module is a self-contained entity, a mini-app if you will. Like you can't split app, you can't also split a module.
2. No. Once a PHP function is defined, you can not overload it.
Extending is difficult if your goal is to keep the interface the same. Because you can't have two classes with the same name. So if code is calling \Module\Class\Method, you can not overload it unless you change either the namespace of the class.
This is why the FuelPHP core using aliasing. You access the alias, which either points to the original core class, or to a user provided class with the same name (which then can extend or completely replace).