Getting Started
From Joomla! Documentation
Revision as of 22:14, 15 January 2021 by Alexandreelise (talk | contribs) (Fix what seems to by a typo Default rather than Defult)
These are some questions and answers that may help make getting started with the Framework easier.
Why does the router keep throwing "Unable to handle request for route"[edit]
This error is generated when the \Joomla\Router\Router class is unable to find a valid route to match the URL the user has entered.
This could be due to:
- There is no route defined for the resource and the router does not have a defaultController (defined using $router->setDefaultController(fully namespaces controller name)). Check to see if a route exists, or the defaultController has been set.
- You have not created a .htaccess file to rewrite the URL To remove the index.php component of the URL. Use the example .htaccess in the framework-app. (https://github.com/dbhurley/framework-app/blob/master/www/.htaccess)
How do I create a default route when no existing route matches?[edit]
A default controller can be created for the Joomla\Router\Router class. This is used when no existing route matches the request URI.
To define a default controller call the \Joomla\Router\Router class setDefaultController function and pass the fully namespaced controller name you wish to use:
$router->setDefaultControllerPrefix('App');
$router->setDefaultController('\Controller\DefaultController');
This will attempt to load the DefaultController.php file from the App/Controller directory. The class name would be DefaultController.
How can I easily create a lot of routes?[edit]
The \Joomla\Router\Router class has a function addMaps that takes an array of routes.
$router->addMaps(my array of routes)
These can be loaded from an array defined elsewhere or from an external file. The framework-app (https://github.com/dbhurley/framework-app/) shows how to load these from an external JSON file.
$router = new Router();
$maps = json_decode(file_get_contents(JPATH_CONFIGURATION.'/routes.json'));
$router->addMaps($maps);
I find creating routes in a routes.json file easier to manage.
How do I connect to the database from a Controller?[edit]
Connecting to the database is managed through the \Joomla\Database\DatabaseFactory class. While you could create this each time in each controller this would be extra code to manage for any changes. I found the easiest way was to create this when bootstrapping the application and maintaining a reference in the AbstractWebApplication class.
Much of this code has been lifted from the Framework-app at (https://github.com/dbhurley/framework-app).
First, create a json file called config.json in the App/Config directory:
{
"database":{"driver":"mysql","host":"localhost","user":"root","password":"root","name":"mydatabase","prefix":"app_"},
"system":{"list_limit":"20","gzip":"0","offset":"UTC"},"languages":["en-GB"]
}
Then when bootstrapping your app (maybe in the index.php) do the following:
$config = json_decode(file_get_contents(JPATH_CONFIGURATION.'/config.json'),true);
$application = new \App\App(null,new \Joomla\Registry\Registry($config));
$application->execute();
Then in the doExecute() method of your app create a reference to the database:
$dbFactory = new Database\DatabaseFactory;
$this->db = $dbFactory->getDriver(
$this->get('database.driver'),
array(
'host'=>$this->get('database.host'),
'user'=>$this->get('database.user'),
'password'=>$this->get('database.password'),
'port'=>$this->get('database.port'),
'socket'=>$this->get('database.socket'),
'database'=>$this->get('database.name'),
)
);
In your controller the database can then be accessed using:
$app = $this->getApplication();
$db = $app->db;