Meet YaF - Part Two

published on November 23, 2011 in technical

In the first part I showed you just a few things about YaF, mostly introductory. In this article I am going write a bit about routing, bootstrap and the view layer.

1. Bootstrap

We all know that modern PHP application need, and mostly have a boostrap. In some cases this is only a file, where important things happen, but in many other cases bootstrap is a class, just as this current framework.

YaF bootstrap looks like this:

class Bootstrap extends Yaf_Bootstrap_Abstract {

	public function _initRoute(Yaf_Dispatcher $dispatcher) {
	}

	public function _initConfig(Yaf_Dispatcher $dispatcher) {
	}

	public function _initPlugin(Yaf_Dispatcher $dispatcher) {
	}

}

It should be named Bootstrap.php and placed in the application folder, defined by the APPLICATION_PATH . "/application/". This is set up in the configuraion file passed to YaF application. If in doubt, read my previous article about YaF here.

The methods defined in the Bootstrap.php will be called in the order of their definition. You can define any methods here. I use this for setting up my routing and my configuration. I do not use the plugins yet, for a very simple reason: I haven't figured out how they work, but as soon as I do, I will write about it.

2. Routing

My public routing looks like this:

public function _initRoute(Yaf_Dispatcher $dispatcher) {

	$router = $dispatcher->getRouter();

	$route = new Yaf_Route_Regex (
		"(.*)",
		array(
			"controller" => "public",
			"action"     => "index",
		),
		array()
	);
	$router->addRoute("regex", $route);

}

It directs every request to one action. It may look stupid, but it isn't. I store all my content in database, the layout configurations and everything. This way I only need one route. I also use this way of automating logic flow in my application in the case of AJAX requests.

Technically it is very simple. I grab the initialized router object and add a route to it. That's it. YaF does the rest, namely sending every request to the indexAction method of my PublicController, last one located in the application's coltrollers folder.

The above controller looks like this:

Class PublicController extends Yaf_Controller_Abstract {

	public function indexAction() {
	}
}

This is where I handle all public request, according to the url they arrive on.

2. Views

Views are located in the application's views folder. The are simple PHP templates, which may receive parameters. A very simple example for this would be this:

Class PublicController extends Yaf_Controller_Abstract {

	public function indexAction() {
		$this->getView()->display('index.php', $data);
	}
}

I pass the $data variable to the view, which I may use in the rendering process.

Due to having all the content in the database, I have to use dynamic view rendering. But YaF expects fixed view names, according to the route. This was a bit of a bottleneck to me, but I solved it by stopping view rendering when I rendered my last view.

This is the code whiche helped me in doing this:

$application = Yaf_Application::app();
$dispatcher = $application->getDispatcher();
$dispatcher->disableView();

This looks like a bit of a hack, but does the trick.

2. Conclusion

Hopefully this helpes many in getting to know YaF better. Anyways, I will try to publish more examples.