Love Fuel?    Donate

FuelPHP Forums

Ask your question about FuelPHP in the appropriate forum, or help others by answering their questions.
Packages, Namespaces, and Re-usable Code
  • I'm moving over from Kohana and I'm really interested in FuelPHP. With Kohana I had a "module" folder that contained all my custom libraries and helper files and views, and with that setup I could drop it into any project and immediately begin using the stuff I was used too -- custom CRUD classes, form builders, useful helper files, and my normal views like 404 pages and such. With Fuel I'm thinking this same directory should become a "package", but I'm stuck getting it setup properly. With Kohana simply dropping the directory in as a module and adding it to my config file made everything available. With Fuel I'm having to add a bootstrap file (where I list every file by name and location) and I have to include a namespace reference to each class. Beyond classes, I'm not even sure how I would begin adding views. I guess my question largely is how to get this package setup properly with classes and views. More specific questions are "what is a namespace?" and "why doesn't Fuel just auto-detect and make usable all of the files in the package like Kohana does?"
  • You have to create a bootstrap file for package.You can autoload the package from config file of application
    http://fuelphp.com/docs/general/packages.html
  • It's correct that the class within a package are not available directly when loading a package. But you don't necessarily need to reference all the classes from your package to use them. The bootstrap file can be as simple as :
    Autoloader::add_namespace('PackageName', __DIR__.DS.'classes'.DS);
    

    This way, your classes can follow the same naming convention as modules. You can also follow the PSR-0 convention by adding "true" as 3rd parameter to the add_namespace() method.
  • Scott Travis wrote on Wednesday 31st of August 2011:
    More specific questions are "what is a namespace?"
    A native PHP 5.3 concept: http://nl3.php.net/namespaces
    and "why doesn't Fuel just auto-detect and make usable all of the files in the package like Kohana does?"
    Because everything is explicit pretty much like Kohana, but the classes no longer cascade as they're namespaced. Everything in a package should be namespaced, though you can use any classname when you register classes in a bootstrap file. If your classes are namespaced with your package namespace you can just register the namespace like Julian said and use them like "PackageName\ClassName" after that. And will be auto-detected, but with the namespace attached to tell the autoloader you're request a class from the package and not from app or core. You can also add the following instruction in the bootstrap to make your classes useable without the namespace prefix (they'll be aliased to global when requested), though aliasing only works when the classes are registered in the bootstrap:
    Autoloader::add_core_namespace('PackageName');
    

    But in general you'll need to read up on namespaces first as you won't understand half of what you're doing otherwise.
  • Thanks guys. Just to circle back, here's what I've found. There are two ways to do this... METHOD #1 -- ADD NAMESPACE TO CORE.
    In this case I add the package to the core, and I just have to make sure I declare each class in the bootstrap. Note, that in this method I do not include the namespace in the add_classes array, which the documentation does not show. Autoloader::add_core_namespace('Mypackage');
    Autoloader::add_classes(array(
    'Test' => __DIR__.'/classes/test.php',
    )); METHOD #2 -- ADD NAMESPACE BY ITSELF
    In this case I add the package and it will autodetect all the classes, but I have to make namespace declarations in each class file. Autoloader::add_namespace('Mypackage');
  • You can not namespace them in which case you don't need to add the core namespace, or you can namespace them and you have to include the namespace while registering them. The advantage of namespacing them is that it's better practice (all non-namespaced classes are in app) and it allows you to create packages that are generic and can be extended in the core to overwrite the package.
  • Thanks. Last questions -- how do I setup my route.php config file to point to a view that lives in the package? Routes.php contains: return array(
    '_root_' => 'site/action_index', // The default route
    '_404_' => 'errors/action_404', // The main 404 route
    ); In my package I have a controller "errors.php" which points to a view also contained within the package. I'm currently getting this error: Fatal error: Exception thrown without a stack frame in Unknown on line 0 If I delete the line in my routes for _404_ it somehow finds the view in the package, but throws errors for missing variables because it's not going thru the controller which gives those variables.
  • Packages are considered core extensions. As such, you can not route to controllers in packages, altough you could probably work around that by adding your package classes to the global namespace. If you want modularity in your applications frontend, I suggest you use modules instead. They follow the same rules are packages (separate namespace), but are an extension of app, and you can route to them: "/module/controller/method". That fatal error is caused by an error occuring in the error handler, which PHP can't deal with...

Howdy, Stranger!

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

In this Discussion