The Anatomy of an Addon
- Posted February 12, 2017
- Addon Development
- Essentials
Introduction
All addon types inherently support the same addon structure. This article will go over the shared structure of addons.
Dot-Namespaces
Throughout this article you will notice addons being references by their dot-namespace. The dot namespace is comprised of the addon vendor, type, and slug.
anomaly.module.pages
Resources
All addon types support a /resources directory. The resources directory is used to store config, language files, assets, images, or anything else that is not a class.
Images
Addons register the /resources directory as an image path hint:
{% verbatim %}{{ img('pryocms.theme.accelerant::img/favicon.png') }}{% endverbatim %}
Assets
Addons register the /resources directory as an asset path hint:
{% verbatim %}{{ asset_add('styles.css', 'pryocms.theme.accelerant::scss/theme.scss') }}{% endverbatim %}
Views
Addons register the /resources/views directory as a view path hint:
return view('anomaly.module.posts::posts/index');
Configuration
Addons register the /resources/config directory with Laravel configuration:
return config('anomaly.field_type.datetime::format.date');
Translator
Addons register the /resources/lang directory with Laravel's translator:
return trans('anomaly.plugin.helper::addon.name');
Composer File
All addons support a composer.json file.
Example:
{
"name": "anomaly/xml_feed_widget-extension",
"type": "streams-addon",
"description": "A dashboard widget that displays an XML feed.",
"keywords": [
"pyrocms addon",
"dashboard widget",
],
"homepage": "http://pyrocms.com/",
"license": "MIT",
"authors": [
{
"name": "PyroCMS, Inc.",
"email": "[email protected]",
"homepage": "http://pyrocms.com/",
"role": "Owner"
}
],
"support": {
"email": "@pyrocms.com"
},
"require": {
"simplepie/simplepie": "~1.4.0"
},
"autoload": {
"psr-4": {
"Anomaly\\XmlFeedWidgetExtension\\": "src/"
}
}
}
Autoloading
The autoload section loads during runtime if not already in the autoloader. Typically this is used to simply load the addons src directory.
Dependencies
Required dependencies will be downloaded on composer install and composer update.
Addon Class
Every addon must include the addon class. Modules will a module class, themes will require a theme class, and so on.
/src/ExampleModule.php
/src/TestTheme.php
These classes must extend the matching base addon class.
Service Provider
All addons also support an addon service provider. Addon service providers work essentially the same as other Laravel service providers, but with added features. Addon service providers are named after the addon class.
/src/ExampleModule.php
/src/ExampleModuleServiceProvider.php
Addon service providers must extend the AddonServiceProvider class.
Tests
The /tests directory contains all of the addon's feature and unit tests. When running make:addon an example feature test is created for you. When running make:stream tests for every entity class are also generated for you. Feel free to delete any or all tests as desired.
To run tests on the addon simply navigate to the addon's root directory and run phpunit.
Factories
The \factories directory is automatically loaded into Laravel's eloquent factory manager.
When using the make:stream command a factory is automatically generated for you.