Tutorials: The MVC pattern in Common Utilities
Posted by: bitceroOn 2014/11/18 21:20:00 7938 readsProbably very few of you know it, but Common Utilities have included since version 2.2, a basic implementation of the pattern MVC (Model - View - Controller). In this article I will give you a basic explanation of its operation in Common Utilities and integrated modules. If you still do not know what MVC is, please read this article on Wikipedia to learn more about it. How MVC works in Common Utilities When a module uses the MVC features of Common Utilities, all requests are received through the URL and it's the job of RMCommon to receive, process and direct them to the appropriate module. To achieve that, RMCommon includes an appropriate option to specify where to receive requests for each module. This is done through the configuration, indicating the URL you use each module. For example, if the module is that we look for is located in the "inventory" directory, and RMCommon configuration has established as their path to the folder "inventory" when RMCommon receives a request to the URL http://sitio.com/inventorios automatically redirect the request to http://sitio.com/modules/inventory . This means that the module will respond to all requests made with misitio.com/inventarios . URL Parameters Once RMCommon knows where to locate each module, you can tell that we get the module by specifying the parameters of the URL. Parameters provided must be written in the form module / controller / action / other-parameters . This simple format allows all requests to the module, which are handled by RMCommon follows: Common Utilities finds the appropriate driver folder controllers within the module's directory. Take for example the URL http://sitio.com/ library / books / list / category / bestsellers / The process is as follows: The corresponding module is located library. Depending on the routes that have been configured, this directory could match the directory of the module or be a different one. In this sample library is the directory of the module. Common Utilities driver looking books in the directory controllers of the module library , and loads the PHP class. Now find the corresponding method to the action list and processes the request by passing the parameters category = bestsellers . These parameters must always be in pairs. After processing the data, Common Utilities get the template (view) and returns the corresponding result. Some conventions in this How to locate the controllers? To begin, the drivers should be located as files within the directory controllers of each module. In addition, there are certain rules for naming files that contain drivers. In our example (yes, the library) the driver file should be called books-controller.php . In addition, this file should contain a class, the controller itself, named as follows: Library_Books_Controller and must inherit from the main class RMController . Finally, the class must contain a method called list , which will be invoked by RMCommon to present a result. Until here everything is clear? So these are the rules: - The driver files must be located in the directory controllers of the module. - The file name must follow the rule
class Mymodule_Nombrecontrolador_Controller extends RMController
{
use RMModuleAjax , RMProperties , RMModels ;
public function __construct () {
parent :: __construct ();
$ this -> default = 'index' ; // default action
$ this -> controller = 'categories' ;
}
public function index () {
// Logic index action
$ This -> tpl -> header ()
requires $ this -> parent -> view ;
$ this -> tpl -> footer ();
}
}
class Mymodule_Nombremodelo_Model extends RMActiveRecord
{
use RMModels ;
public function __construct () {
parent :: __construct ( 'model' , 'module' );
/ **
* Titles table fields
* /
$ this -> titles = array (
'column' => __ ( 'Column Title' , 'module' ),
'column2' => __ ( 'Title column2' , 'module' ),
...
);
}
}