Extending PHaiku

PHaiku is essentialy one abstract class that is meant to be extended. By default it's extended by the Haiku class, but you can extend it by any of your own classes. To make sure, the right class gets called in the bootstrap process, you define it in the ./config/config.php file:

"" => "\PHaiku\MyHaiku",

You may alter functionality, add file parsers (markdown support) and be quite creative. Don't forget the $this->app variable contains an instance of Slim framework, so don't forget to consult their documentation too.

Adding controllers

The most interesting way to extend PHaiku is to add new controllers. Haiku implements only one controller method PHaiku::setPage(). You can use your imagination or follow your needs to add controllers. One idea is for example a contact form.

To add a controller you must create a new method

public function myController($params);

Then add a route for the new controller in the ./config/routes.php file

//define basic routes
"routes" => [
		"name" => "index",
		"route" => "/",
		"method" => "get",
		"handler" => "setPage",
		"name" => "page",
		"route" => "/p/:page+",
		"method" => "get",
		"handler" => "setPage",
//custom routes
		"name" => "contact",
		"route" => "/contact",
		"method" => "get",
		"handler" => "contactForm",
//routes for myController
		"name" => "mycontroller",
		"route" => "/my/:params+",
		"method" => "get",
		"handler" => "myController",
// define default route conditions
"route.conditions" => [
	'page+' => '[a-zA-Z0-9_-]+',
	'lang' => '[\w]{2}',
//define default route conditions for params used in mycontroller
	'params+' => '[\d]+'

Adding dependencies

Sooner or later, you will need some libraries to assist you in your creativity. PHaiku uses Pimple as dependency injection container.

Add your custom services to the $di variable in the ./config/services.php file.

$di['myservice'] = function($c) {
	return new ...

For your service to be available, put it in a global variable in the MyClass::init() method.

* This is the implementation of the init abstract method. You can use it to
* add your own initialisations.
* @param object \Pimple\Pimple $di Description
public function init(\Pimple\Pimple $di) {
	$this->myservice = $di['myservice'];

Preparing for the output

The $this->data property that is by default passed to the Slim\View should contain an array of objects of type \PHaiku\Data, a class that extends the stdClass and adds a magic __get() method that returns an empty string for non existing properties.

By default, the objects are:

  • $site - defines site wide properties.
  • $page - defines properties for the current url
  • $widgets - defines widgets

Your handler may define other such objects. To achieve this, use the \PHaiku::newData() method.

$myobject = $this->newData();
$this->data['myobject'] = $myobject;
// in your views you may now access myobject properties like this
<?php echo $myobject->myproperty; ?>	

If by any chance, $myobject->myproperty does not exist, an empty string will be returned by the \PHaiku\Data::__get() magic method.

That's probably all you need to know to extend PHaiku. You can also check the reference for details.


Feel free to comment or ask questions! Thanks!

comments powered by Disqus

Execution time: 0.0057s Memory usage: 0.675112MB