Difference between revisions of "JReviews:Macroable Controllers"

From JReviews Documentation
Jump to: navigation, search
(Macroable overrides for controller routing methods)
Line 1: Line 1:
= Macroable overrides for controller routing methods =
 
 
 
Starting with JReviews 3.6.4.1 it is possible to override specific routing methods in controllers without having to copy the entire controller file to overrides. To get started with this override feature you would create an empty controller in the same path as regular controller files (mentioned above), but add the _macros suffix. So for example to override a routing method in, categories_controller.php, you create an empty file named categories_controller_macros.php. Inside this file, you override an existing routing method like this:
 
Starting with JReviews 3.6.4.1 it is possible to override specific routing methods in controllers without having to copy the entire controller file to overrides. To get started with this override feature you would create an empty controller in the same path as regular controller files (mentioned above), but add the _macros suffix. So for example to override a routing method in, categories_controller.php, you create an empty file named categories_controller_macros.php. Inside this file, you override an existing routing method like this:
  
Line 30: Line 28:
  
 
This approach, allows you to override internal methods, in addition to routing methods. Just make sure that you use a different method name for your internal method overrides because creating a macro using an existing method name will not allow the system to use the macro. The original method will be used instead. For example, creating a macro for 'category' using '''CategoriesController::macro('category', function() {})''' won't work because the original category method will be called instead of your macro.
 
This approach, allows you to override internal methods, in addition to routing methods. Just make sure that you use a different method name for your internal method overrides because creating a macro using an existing method name will not allow the system to use the macro. The original method will be used instead. For example, creating a macro for 'category' using '''CategoriesController::macro('category', function() {})''' won't work because the original category method will be called instead of your macro.
 
 
 
'''Plugins'''
 
 
In Joomla:
 
 
*<span style="color: blue">SITE_ROOT/templates/jreviews_overrides/plugins</span>
 
 
In WordPress:
 
 
*<span style="color: blue">SITE_ROOT/jreviews_overrides/plugins</span>
 
 
'''Models'''
 
 
In Joomla:
 
 
*<span style="color: blue">SITE_ROOT/templates/jreviews_overrides/models</span>
 
 
In WordPress:
 
 
*<span style="color: blue">SITE_ROOT/jreviews_overrides/models</span>
 
 
'''View Helpers'''
 
 
In Joomla:
 
 
*<span style="color: blue">SITE_ROOT/templates/jreviews_overrides/views/helpers</span>
 
 
In WordPress:
 
 
*<span style="color: blue">SITE_ROOT/jreviews_overrides/views/helpers</span>
 
 
The framework will always check in the jreviews_overrides folder first to see if the file is there and if found it will use it instead of the core file.
 
 
Keep in mind that anytime you add a file to the overrides folder you need to clear the File Registry in the JReviews administration. Doing this allows JReviews to recognize the new file. Otherwise, it will continue reading the file from the main component folder.
 

Revision as of 23:36, 4 December 2019

Starting with JReviews 3.6.4.1 it is possible to override specific routing methods in controllers without having to copy the entire controller file to overrides. To get started with this override feature you would create an empty controller in the same path as regular controller files (mentioned above), but add the _macros suffix. So for example to override a routing method in, categories_controller.php, you create an empty file named categories_controller_macros.php. Inside this file, you override an existing routing method like this:

<?php
defined('MVC_FRAMEWORK') or die;
 
CategoriesController::macro('category_override', function() {
 	return "Hello World!";
 });

The above code overrides the CategoriesController::category method and will output "Hello World" in category pages. Notice that the macro name needs to be the original routing method "category" with the "_override" suffix => category_override.

This approach ONLY works for routing methods, which are methods that are directly called when routing a request. Any internal methods that used within the controller class cannot be overridden like this, but you could create new methods using the same approach and then call these new methods from within your macro override. For example, the following code will also output "Hello World!" in the category page, but it does so by calling a new 'custom_method' that is also dynamically created through a macro:

<?php
defined('MVC_FRAMEWORK') or die;
 
CategoriesController::macro('category_override', function() {
	return $this->custom_method("Hello World!");
});
 
CategoriesController::macro('custom_method', function($string) {
	return $string;
});

This approach, allows you to override internal methods, in addition to routing methods. Just make sure that you use a different method name for your internal method overrides because creating a macro using an existing method name will not allow the system to use the macro. The original method will be used instead. For example, creating a macro for 'category' using CategoriesController::macro('category', function() {}) won't work because the original category method will be called instead of your macro.