PageRenderTime 57ms CodeModel.GetById 28ms RepoModel.GetById 1ms app.codeStats 0ms

/app/readme.md

https://gitlab.com/buitenzorg812/garapic.cms
Markdown | 217 lines | 150 code | 67 blank | 0 comment | 0 complexity | c47a8b0ff4929410f926cc38503f1931 MD5 | raw file
  1. ### Support development of Modular Extensions - HMVC
  2. [![Support development](https://www.paypal.com/en_US/i/btn/btn_donateCC_LG.gif "Support Development")](https://www.paypal.com/cgi-bin/webscr?cmd=_s-xclick&hosted_button_id=FK79XNCUE9P5C)
  3. # Modular Extensions - HMVC
  4. Modular Extensions makes the CodeIgniter PHP framework modular. Modules are groups of independent components,
  5. typically model, controller and view, arranged in an application modules sub-directory that can be dropped into other CodeIgniter applications.
  6. HMVC stands for Hierarchical Model View Controller.
  7. Module Controllers can be used as normal Controllers or HMVC Controllers and they can be used as widgets to help you build view partials.
  8. ### Features:
  9. All controllers can contain an $autoload class variable, which holds an array of items to load prior to running the constructor.
  10. This can be used together with module/config/autoload.php, however using the $autoload variable only works for that specific controller.
  11. :::php
  12. <?php
  13. class Xyz extends MX_Controller
  14. {
  15. $autoload = array(
  16. 'helper' => array('url', 'form'),
  17. 'libraries' => array('email'),
  18. );
  19. }
  20. The Modules::$locations array may be set in the application/config.php file. ie:
  21. :::php
  22. <?php
  23. $config['modules_locations'] = array(
  24. APPPATH.'modules/' => '../modules/',
  25. );
  26. Modules::run() output is buffered, so any data returned or output directly from the controller is caught and
  27. returned to the caller. In particular, $this->load->view() can be used as you would in a normal controller, without the need for return.
  28. Controllers can be loaded as class variables of other controllers using $this->load->module('module/controller');
  29. or simply $this->load->module('module'); if the controller name matches the module name.
  30. Any loaded module controller can then be used like a library, ie: $this->controller->method(), but it has access to its own models and libraries independently from the caller.
  31. All module controllers are accessible from the URL via module/controller/method or simply module/method if the module and controller names match.
  32. If you add the _remap() method to your controllers you can prevent unwanted access to them from the URL and redirect or flag an error as you like.
  33. ### Notes:
  34. To use HMVC functionality, such as Modules::run(), controllers must extend the MX_Controller class.
  35. To use Modular Separation only, without HMVC, controllers will extend the CodeIgniter Controller class.
  36. You must use PHP5 style constructors in your controllers. ie:
  37. :::php
  38. <?php
  39. class Xyz extends MX_Controller
  40. {
  41. function __construct()
  42. {
  43. parent::__construct();
  44. }
  45. }
  46. Constructors are not required unless you need to load or process something when the controller is first created.
  47. All MY_ extension libraries should include (require) their equivalent MX library file and extend their equivalent MX_ class
  48. Each module may contain a config/routes.php file where routing and a default controller can be defined for that module using:
  49. :::php
  50. <?php
  51. $route['module_name'] = 'controller_name';
  52. Controllers may be loaded from application/controllers sub-directories.
  53. Controllers may also be loaded from module/controllers sub-directories.
  54. Resources may be cross loaded between modules. ie: $this->load->model('module/model');
  55. Modules::run() is designed for returning view partials, and it will return buffered output (a view) from a controller. The syntax for using modules::run is a URI style segmented string and unlimited variables.
  56. :::php
  57. <?php
  58. /** module and controller names are different, you must include the method name also, including 'index' **/
  59. modules::run('module/controller/method', $params, $...);
  60. /** module and controller names are the same but the method is not 'index' **/
  61. modules::run('module/method', $params, $...);
  62. /** module and controller names are the same and the method is 'index' **/
  63. modules::run('module', $params, $...);
  64. /** Parameters are optional, You may pass any number of parameters. **/
  65. To call a module controller from within a controller you can use $this->load->module() or Modules::load()
  66. and PHP5 method chaining is available for any object loaded by MX.
  67. ie: $this->load->library(validation)->run().
  68. To load languages for modules it is recommended to use the Loader method which will pass the active module
  69. name to the Lang instance; ie: $this->load->language('language_file');
  70. The PHP5 spl_autoload feature allows you to freely extend your controllers, models and libraries from
  71. application/core or application/libraries base classes without the need to specifically include or require
  72. them.
  73. The library loader has also been updated to accommodate some CI 1.7 features: ie Library aliases are
  74. accepted in the same fashion as model aliases, and loading config files from the module config directory
  75. as library parameters (re: form_validation.php) have beed added.
  76. $config = $this->load->config(config_file), Returns the loaded config array to your variable.
  77. Models and libraries can also be loaded from sub-directories in their respective application directories.
  78. When using form validation with MX you will need to extend the CI_Form_validation class as shown below,
  79. :::php
  80. <?php
  81. /** application/libraries/MY_Form_validation **/
  82. class MY_Form_validation extends CI_Form_validation
  83. {
  84. public $CI;
  85. }
  86. before assigning the current controller as the $CI variable to the form_validation library.
  87. This will allow your callback methods to function properly. (This has been discussed on the CI forums also).
  88. :::php
  89. <?php
  90. class Xyz extends MX_Controller
  91. {
  92. function __construct()
  93. {
  94. parent::__construct();
  95. $this->load->library('form_validation');
  96. $this->form_validation->CI =& $this;
  97. }
  98. }
  99. ### View Partials
  100. Using a Module as a view partial from within a view is as easy as writing:
  101. :::php
  102. <?php echo Modules::run('module/controller/method', $param, $...); ?>
  103. Parameters are optional, You may pass any number of parameters.
  104. ### Modular Extensions installation
  105. 1. Start with a clean CI install
  106. 2. Set $config[base_url] correctly for your installation
  107. 3. Access the URL /index.php/welcome => shows Welcome to CodeIgniter
  108. 4. Drop Modular Extensions third_party files into the CI 2.0 application/third_party directory
  109. 5. Drop Modular Extensions core files into application/core, the MY_Controller.php file is not required unless you wish to create your own controller extension
  110. 6. Access the URL /index.php/welcome => shows Welcome to CodeIgniter
  111. 7. Create module directory structure application/modules/welcome/controllers
  112. 8. Move controller application/controllers/welcome.php to application/modules/welcome/controllers/welcome.php
  113. 9. Access the URL /index.php/welcome => shows Welcome to CodeIgniter
  114. 10. Create directory application/modules/welcome/views
  115. 11. Move view application/views/welcome_message.php to application/modules/welcome/views/welcome_message.php
  116. 12. Access the URL /index.php/welcome => shows Welcome to CodeIgniter
  117. You should now have a running Modular Extensions installation.
  118. ### Installation Guide Hints:
  119. -Steps 1-3 tell you how to get a standard CI install working - if you have a clean/tested CI install, skip
  120. to step 4.
  121. -Steps 4-5 show that normal CI still works after installing MX - it shouldnt interfere with the normal CI
  122. setup.
  123. -Steps 6-8 show MX working alongside CI - controller moved to the welcome module, the view file remains
  124. in the CI application/views directory - MX can find module resources in several places, including the application directory.
  125. -Steps 9-11 show MX working with both controller and view in the welcome module - there should be no
  126. files in the application/controllers or application/views directories.
  127. ### FAQ
  128. Q. What are modules, why should I use them?
  129. A. (http://en.wikipedia.org/wiki/Module)
  130. (http://en.wikipedia.org/wiki/Modular_programming)
  131. (http://blog.fedecarg.com/2008/06/28/a-modular-approach-to-web-development)
  132. Q. What is Modular HMVC, why should I use it?
  133. A. Modular HMVC = Multiple MVC triads
  134. This is most useful when you need to load a view and its data within a view. Think about adding a shopping
  135. cart to a page. The shopping cart needs its own controller which may call a model to get cart data.
  136. Then the controller needs to load the data into a view. So instead of the main controller handling the
  137. page and the shopping cart, the shopping cart MVC can be loaded directly in the page.
  138. The main controller doesnt need to know about it, and is totally isolated from it.
  139. In CI we cant call more than 1 controller per request. Therefore, to achieve HMVC, we have to simulate
  140. controllers. It can be done with libraries, or with this Modular Extensions HMVC contribution.
  141. The differences between using a library and a Modular HMVC HMVC class is:
  142. 1. No need to get and use the CI instance within an HMVC class
  143. 2. HMVC classes are stored in a modules directory as opposed to the libraries directory.
  144. Q. Is Modular Extensions HMVC the same as Modular Separation?
  145. A. Yes and No. Like Modular Separation, Modular Extensions makes modules portable to other installations. For example, if you make a nice self-contained model-controller-view set of files you can bring that MVC into another project by copying just one folder - everything is in one place instead of spread around model, view and controller folders.
  146. Modular HMVC means modular MVC triads. Modular Separation and Modular Extensions allows related
  147. controllers, models, libraries, views, etc. to be grouped together in module directories and used
  148. like a mini application. But, Modular Extensions goes one step further and allows those modules to
  149. talk to each other. You can get controller output without having to go out through the http interface
  150. again.