vignesh.D
vignesh.D

Reputation: 776

Joomla - How to override component controller?

I want to override a controller inside a component

ie;

File path : components/com_test/controllers/test.php

how to override test.php ?

Upvotes: 2

Views: 1410

Answers (2)

Riccardo Zorn
Riccardo Zorn

Reputation: 5615

As Pritesh mentioned, you can't*.

I can see a couple of ways to achieve the result:

  1. You can create a new controller which extends your test.php controller, and invoke that instead; in order to achieve this, the controller must never use JPATH_COMPONENT and you'll have to override the view as well to point to the right component.

  2. Add a special task to your view, and intercept it with a system plugin in OnAfterRoute(). You won't be touching the original controller, but your plugin will fire before the original controller, so it can take action, manipulate input and output, and eventually avoid invoking the original controller altogether.

--

if editing the original controller could seem like an option, please disregard it: the original component will be updated from time to time and you'd be entering a maintenance nightmare.

Very often I have to achieve just this result. And 90% of the time I achieve this in a system plugin. In case of improvements, I contribute the code back to the original developer, who usually integrates the features in their next release. Don't forget to let the original developers know, you'll help improve their products and save yourself time.

  • Actually you can override quite a large part of the core, by loading the class before Joomla does; this limits to non-core classes but it is feasible. That's what I was referring to in n.2

Upvotes: 3

Pritesh Mahajan
Pritesh Mahajan

Reputation: 5154

we can not override the controller and model in joomla we will override only views of the component.

Upvotes: 2

Related Questions