So I have been messing with this as well. This isn't specifically how to use the MVC, more how I set up the application around it -- that is what I first stuggled with.
It is definitely a work in progress. Since you mentioned you have written Joomla components before, this basically lets you pass a task in the format controllerclass.controllermethod like a Joomla CMS component.
One thing I did notice is that the joomla/framework (I think that's the 11.4 version) package is no longer maintained. You have to add each Joomla package individually (eg joomla/model) to get the newest version. Here is
index.php
configuration.php
composer.json
composer.lock
src
- Company
- Package
- App.php
- Factory.php
- Controllers
- Interfaces
- Models
- Views
vendor
- composer
....
...
- joomla
- application
....
- utilities
index.php
<?php
require __DIR__ . '/configuration.php';
require __DIR__ . '/vendor/autoload.php';
use Company\Package\Factory;
try {
$app = Factory::getApplication();
$app->execute();
} catch (\Exception $e) {
echo $e->getMessage();
}
configuration.php
class configuration {
/*
App Info
*/
public $version = '0.0.1';
// other configuration variables
// ...
}
src/Company/Package/App.php
<?php
namespace Company\Package;
use Joomla\Application\AbstractWebApplication;
use Joomla\Uri\Uri;
use Joomla\Registry\Registry;
class App extends AbstractWebApplication {
protected function doExecute(){
$task = $this->input->get('task');
$output = '';
try {
if (empty($task)) {
throw new \Exception('No task submitted', 400);
}
if (preg_match('/^[a-z0-9]+\.[a-z0-9]+$/i',$task)) {
list($class,$method) = explode('.',$task);
$controllerClassName = "Company\\Package\\Controllers\\" . ucfirst($class);
if (!class_exists($controllerClassName)) {
throw new \Exception('Controller does not exist', 501);
}
$controller = new $controllerClassName($this->input, $this);
} else {
throw new \Exception("Task format not recognized", 501);
}
$response = $controller->$method();
$body = $response;
$status = 200;
} catch (\Exception $e) {
// probably need and error view instead of just dumping the error message
$body = $e->getMessage();
$status = $e->getCode();
}
$this->setHeader('Status', $status, true);
$this->setBody($body);
}
protected function initialise() {
// initialize some stuff here
}
}
src/Company/Package/Factory.php
<?php
namespace Company\Package;
use Joomla\Registry\Registry;
use Joomla\Input;
use Joomla\Database\DatabaseFactory;
abstract class Factory {
protected static $app;
public static function getApplication() {
if (isset(static::$app)) return static::$app;
$config = static::getConfig();
$type = 'web'; // someday setup for cli
if ($type=='web') {
$input = new Input\Input;
static::$app = new App($input, $config);
} else {
throw new \Exception("App type not supported");
}
return static::$app;
}
// only get configuration from application
protected static $config;
protected static function getConfig() {
if (isset(static::$config)) return static::$config;
$configuration = new \configuration;
static::$config = new Registry($configuration);
return static::$config;
}
protected static $db;
public static function getDb() {
if (isset(static::$db)) return static::$db;
$app = static::getApplication();
static::$db = DatabaseFactory::getDriver($app->get('drive'),[
'host' => $app->get('host'),
'user' => $app->get('user'),
'password' => $app->get('pass'),
'database' => $app->get('db'),
'port' => $app->get('port'),
]);
return static::$db;
}
}