Difference between revisions of "JReviews:Macroable Controllers"
(Created page with "= Macroable overrides for controller routing methods = Starting with JReviews 3.6.4.1 it is now possible to override specific routing methods in controllers without having to...") |
|||
Line 1: | Line 1: | ||
= Macroable overrides for controller routing methods = | = Macroable overrides for controller routing methods = | ||
− | Starting with JReviews 3.6.4.1 it is | + | 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: |
<source lang="php"> | <source lang="php"> |
Revision as of 23:35, 4 December 2019
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:
<?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.
Plugins
In Joomla:
- SITE_ROOT/templates/jreviews_overrides/plugins
In WordPress:
- SITE_ROOT/jreviews_overrides/plugins
Models
In Joomla:
- SITE_ROOT/templates/jreviews_overrides/models
In WordPress:
- SITE_ROOT/jreviews_overrides/models
View Helpers
In Joomla:
- SITE_ROOT/templates/jreviews_overrides/views/helpers
In WordPress:
- SITE_ROOT/jreviews_overrides/views/helpers
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.