Framework Documentation - Application Package
Getting started
Installation
composer require joomla/applicationThe package needs a PSR-7 implementation for the response object. The framework's own
applications use laminas/laminas-diactoros:
composer require laminas/laminas-diactorosThe smallest possible application
AbstractApplication gives you configuration, a logger, an event dispatcher and the lifecycle.
You supply doExecute():
<?php
use Joomla\Application\AbstractApplication;
require __DIR__ . '/vendor/autoload.php';
final class HelloApplication extends AbstractApplication
{
protected function doExecute()
{
echo 'Hello, ' . $this->get('name', 'world') . "\n";
}
}
$app = new HelloApplication();
$app->set('name', 'Joomla');
$app->execute();Run it:
$ php hello.php
Hello, JoomlaThe smallest possible web application
For HTTP, extend AbstractWebApplication. You write into a response body instead of echoing, and
execute() sends the response for you:
<?php
use Joomla\Application\AbstractWebApplication;
require __DIR__ . '/vendor/autoload.php';
final class WebHelloApplication extends AbstractWebApplication
{
protected function doExecute()
{
$this->setBody('<h1>Hello, world</h1>');
}
}
(new WebHelloApplication())->execute();execute() will:
- dispatch
application.before_execute, - call your
doExecute(), - dispatch
application.after_execute, - optionally gzip the body (if
gzipis set in the configuration), - dispatch
application.before_respond, - send status line, headers and body,
- dispatch
application.after_respond.
Adding configuration
Configuration is a Joomla\Registry\Registry. Pass one in, or set values afterwards:
use Joomla\Registry\Registry;
$config = new Registry([
'debug' => true,
'gzip' => true,
'db' => [
'driver' => 'mysqli',
'host' => 'localhost',
],
]);
$app = new WebHelloApplication(null, $config);
$app->get('db.driver'); // 'mysqli' — dot notation works
$app->get('missing', 'fallback');See Configuration for the keys the package itself reads and writes.
Adding events
Attach a dispatcher and you can hook into the lifecycle without subclassing:
use Joomla\Application\ApplicationEvents;
use Joomla\Event\Dispatcher;
$dispatcher = new Dispatcher();
$dispatcher->addListener(
ApplicationEvents::BEFORE_RESPOND,
static function (ApplicationEvent $event) {
$event->getApplication()->setHeader('X-Powered-By', 'Joomla Framework');
}
);
$app->setDispatcher($dispatcher);Without a dispatcher the application still works — dispatchEvent() returns null when none is
set. See Lifecycle and events.
Next steps
- A routed application with controllers: Routing and controllers
- The full walkthrough, wiring in database, views, session and console:
Building a complete application