-
Notifications
You must be signed in to change notification settings - Fork 16
Controller autorun method
We learned how, by using the __construct
method of the controller, we can implement some actions to be automatically executed. However, having in some circumstances the goal of managing complex web pages, we could involve mixing, when writing the code for the constructor, of some generics actions together with more specialized ones. The autorun
method gives you the ability to separate, outside the controller constructor, the coding of those specialized actions that must be automatically executed. As result of this separation, you will obtain a smart reuse of a controller any times you will need to extend it to a more specialized behavior.
First, consider the following and simple extension of the previous controllers\EchoController
, we call it controllers\WebPageProductsList
and we use it for simulating the behavior that must be executed automatically inside a web page having the responsibility of showing a list of products for a hypothetical e-commerce application.
<?php
namespace controllers;
use controllers\EchoController;
class WebPageProductsList extends EchoController
{
/*
* @override controllers\EchoController __construct()
*/
public function __construct()
{
echo "Building page navigation bar <br>";
echo "Building page status bar <br>";
echo "Building page credits information <br>";
echo "Building products list <br>";
parent::__construct();
}
}
We are simulating that this page, together with its specialized responsibility to show products list, it must show others generics sections, like the site navigation bar, the status bar and credits information. We emulated this actions just by printing a message respectively for each action.
Now suppose we need a new web page, we call it controllers\WebPageProductDetail
, that must show the same generics section regarding navigation, credits information and status bar but it must show a product detail rather than the products list.
We can code controllers\WebPageProductDetail
in this way:
<?php
namespace controllers;
use controllers\EchoController;
class WebPageProductDetail extends EchoController
{
/*
* @override controllers\EchoController __construct()
*/
public function __construct()
{
echo "Building page navigation bar <br>";
echo "Building page status bar <br>";
echo "Building page credits information <br>";
echo "Building product detal information <br>";
parent::__construct();
}
}
Now try to type the following HTTP requests to show the results:
http://localhost/web_page_products_list
and
http://localhost/web_page_product_detail
Although this implementation is correct, it is certainly not the most efficient way because the two classes contain code repetitions necessary to perform the (shared) generic actions for showing the navigation bar, the credits information, and the status bar.
By using autorun, in conjunction with OOP extension, you can code a better version for implementing the previous controllers. See the code below:
<?php
namespace controllers;
use controllers\EchoController;
class WebPageProductsList extends EchoController
{
/*
* @override controllers\EchoController __construct()
*/
public function __construct()
{
echo "Building page navigation bar <br>";
echo "Building page status bar <br>";
echo "Building page credits information <br>";
parent::__construct();
}
/**
* @override framework\Controller autorun()
*/
protected function autorun($parameters = null)
{
echo "Building products list <br>";
}
}
<?php
namespace controllers;
use controllers\WebPageProductsList;
class WebPageProductDetail extends WebPageProductsList
{
/**
* @override controllers\WebPageProductsList autorun()
*/
protected function autorun($parameters = null)
{
echo "Building product detail <br>";
}
}
In this version, we coded a parent plus a child controller. Then we shared the generic actions inside the parent constructor leaving to the autorun method the responsibility for implementing, respectively, the different and specialized behavior of each class.
By using the controller autorun
you have the ability to automatically execute some custom code, even located outside its constructor. Then, you can potentially extend a controller just by writing or overriding the autorun code for extending a parent behavior that must be executed automatically without the need of modifying its constructor.
In others terms, you can think about autorun
like an event that is generated after a controller object creation. So you can handle this event in any child controller, to extend the main responsibility of a parent one without the need to override the constructor.
Until now we have focused on the main processing entity of WebMVC: the Controller. To clearly showing its basic features, we have introduced an extended version called EchoController, which modifies its basic behavior: that is, interaction with the View. Starting from now we will introduce the use of the View, an essential entity for the creation of web pages.