Repository navigation
2. Extending the Service Class
In MODX Extras, the service class is a fairly standard PHP class that is used to tie things together. The minimum service class takes a $modx variable and loads some configuration into a local array, but they can also contain utilities and what not.
The service class typically lives in core/components/packagename/model/packagename/packagename.class.php.
With Alpacka, you get a whole bunch of utilities available in your service class, while minimising the boilerplate required. Here's a very simple example of a service class using Alpacka:
<?php // Require the composer-provided autoloader require_once dirname(dirname(__FILE__)) . ‘/vendor/autoload.php’; // Import the Alpacka service class use modmore\Alpacka\Alpacka; // Create your service class "PackageName" by extending the Alpacka class class PackageName extends Alpacka { // Set the namespace for automatically grabbing the right configuration protected $namespace = 'packagename'; // Extend the construct function to do your own initialisation logic public function __construct($instance, array $config = array()) { // Always start by calling the parent constructor parent::__construct($instance, $config); // Set the current version (major, minor, patch, release) $this->setVersion(1, 2, 3, 'pl'); } }
To use the service class, you can use the same $modx->getService method you would use with a standard service class. For example like so:
<?php $corePath = $modx->getOption('packagename.core_path', null, $modx->getOption('core_path').'components/packagename/'); $service = $modx->getService('packagename', 'PackageName', $corePath . 'model/packagename/');
When you have an instance of the service class, you can start using the Alpacka features.