Packages can be used to add, extend or override functionality. Packages can contain classes, config files, controllers, language files, migrations, tasks and views.
Packages are installed in the
app/packages directory. Here's the directory structure of a basic package containing only one class:
All packages must include a
_init.php file. This is where you define constants, global functions and set up autoloading of the package classes. You can use the Mako autoloader or write your own.
<?php // Map classes mako\ClassLoader::mapClasses(array ( 'Foo' => __DIR__ . '/classes/Foo.php', ));
Here's the directory structure of a more complex package containing config files, controllers, language files, migrations, tasks and views.
You can route requests to package controllers by adding your package to the
packages list in the
app/config/routes.php config file. The array key is the base route you want the package to respond to and the value is the package name.
'packages' => array ( // The foobar package will now handle all requests starting with "my/base/route" 'my/base/route' => 'foobar' );
The package will automatically be initialized when a request matches the base route you have defined and the
<package_name>\controllers namespace will be registered by the autoloader allowing your controllers to be autoloaded.
Routable packages also require their own
routes.php config file where you define the default route and any custom routes you might need.
Loading configuration files in a package is done using the
Config::get method like in the application. The only difference is that you have to prefix the name of the config file with the name of your package followed by two colons (
::). The same convention is used when running tasks, generating urls or creating views.
// Loads the "app/packages/foobar/config/config.php" file $config = Config::get('foobar::config'); // Execute the default task action php reactor foobar::task // Generates a URL for the foobar package where "foobar::" is replaced by the base route echo URL::('foobar::users/profile/1'); // Loads the "app/packages/foobar/views/view.php" file $view = new View('foobar::view');
Installing packages is extremely easy. Just drop the package in the
app/packages directory and you're ready to go!
You can also install packages using composer.
composer require mako/image:*
You need to initialize a package before you can use it in your application. This can be done using the
Package::init method or by registering the package(s) you want to auto-initialize in the
app/config/application.php config file.