Can you override controllers?

I know there are three ways to utilize hooks:

  • pre
  • post
  • override



    but it appears that full controllers can only be modified by pre or post.



    Is this true???



    For example:



    I would like to insert some code around line 110 in the:

    \controllers\customer\products.php file, just before:
$view->assign('product', $product);
```<br />
<br />
obviously using a pre or post controller addon will not acheive this.<br />
<br />
I have tried to create  \addons\my_changes\controllers\customer\products.override.php (which is a copy of the regular products.php file with my extra bit of code added) to do this, but it doesn't seem to work.<br />
<br />
Can I not override the entire controller, or is there another way I can do what I want to do?<br />
<br />
Thanks in advance for any input.

Short answer is no you can’t.

But you can use a pre controller to modify what’s in the $_REQUEST (POST or GET) parameters or there are probably PHP hooks to add/modify/delete elements of the product_data array where functions are called by the products controller. As an example, you can add a hook to modify what’s returned by fn_get_product_data() by adding this line to your init.php in my_changes:


fn_register_hook('get_product_data_more');


Then add a function to func.php of:


function fn_my_changes_get_product_data_more(&$product_data, &$auth) {
Modify the product data as you want
}




I would think the latter method would be more of what you’re looking for but you’re not saying what you want to accomplish.

Any one knows how to over write any controller?

Need More easily specification for how to over write any controller through add-on.

Please Help… :-(

I want override checkout and product controller with my user defined functions.

[quote name=‘chirag’ timestamp=‘1352288165’ post=‘148508’]

Please Help… :-(

I want to override checkout and product controller with my user defined functions.

[/quote]

Please Help... icon_sad.gif
I want to override checkout and product controller with my user defined functions.

**very** late answer, but yes you can.

You can use the 'dispatch_assign_template' hook. Use a function as follows (object oriented style):

public function onDispatchAssignTemplate(
    string $controller, string $mode, string $area, array &$controllers_cascade
): void {
    if (!SiteArea::isStorefront($area) || $controller !== 'categories' || $mode !== 'view') {
        return;
    }
$default_controller_path  = DIR_ROOT . '/app/controllers/frontend/categories.php';
$override_controller_path = DIR_ROOT . '/app/addons/my_addon/controllers/frontend/categories.post.php';

$controllers_cascade = array_filter(
    $controllers_cascade,
    static function ($item) use ($override_controller_path) {
        return $override_controller_path !== $item;
    }
);

array_walk(
    $controllers_cascade,
    static function (string &$controller_path) use ($default_controller_path, $override_controller_path): void {
        if ($controller_path === $default_controller_path) {
            $controller_path = $override_controller_path;
        }
    }
);

}

This will replace the controller `categories.post.php` from the addon `my_addon` when the dispatch equals `categories.view`.

As I can understand, we can even simplify @harmsmitsdev approach.

function fn_myaddon_dispatch_assign_template($controller, $mode, $area, &$controllers_cascade) {
	if ($controller !== 'categories' || $mode !== 'view') {
        return;
    }

	//excluded default categories controller 
	$default_controller_path  = DIR_ROOT . '/app/controllers/frontend/categories.php';
	
	$controllers_cascade = array_filter(
		$controllers_cascade,
		static function ($item) use ($default_controller_path) {
			return $default_controller_path !== $item;
		}
	);
}
  1. We pass &$controllers_cascade parameter by reference.
  2. We exclude default controller from $controllers_cascade array.
1 Like