2

I have a question, and I am a bit confused where i need to put this.

I have a controller which is mainly empty:

class ControllerProfiles extends Controller
{
    
    public function &getModel($name = 'Matches', $prefix = 'Model', $config = array())
    {
        $model = parent::getModel($name, $prefix, array('ignore_request' => true));

        return $model;
    }

}

Now I want to run a model function and based on that value (true or false) I want to redirect to a certain page (or do nothing).

But I don't know where I need to place this code. Do I need to add some sort of function to the controller above (load() or display() or something?)? Or do I need to use the view.html.php controller of the specific view I want to add this to?

EDIT:

I overrided the display function. but it doesn't get run.

public function display($cachable = false, $urlparams = false)
    {

        echo "test";
        die();
        
    }

Did some extra tests by removing the display function from the main controller and adding that to my specific controller. But now the page is empty. So the problem is that my controller isn't being used at all! How can I make sure the controller gets used?

4
  • The question is not fully clear for me, but if the request arrives to this controller than this controller will forward “a request to the model” (to a method of that model) and based on the returned value, this controller will control whether it displays the view or redirect to somewhere else (to another view). Place the logic in the model (if it has anything to do with any data handling), and place the controller logic(decision) in the controller (in a new method if needed).
    – Zollie
    Dec 9, 2021 at 5:04
  • No, view.html.php is a View file (it belongs to the controller, it is true), do not be bothered by that. This controller file itself is almost empty because it inherits its methods from its parent Class(Controller) which it extends. So, put your custom methods in this Controller file(ControllerProfile). (You do not need construct). From this controller you can call the model where you can place your custom method too if needed.
    – Zollie
    Dec 9, 2021 at 14:22
  • I start to understand where you want to go with this. So, you have to override the display() method of this controller. So, you have to write your own display method in this controller where you can put your logic too and based on that, this method will not display the default View file of this controller but it will do whatever you want, especially redirect to somewhere else, if special conditions. Study the base controller Class of Joomla a bit to understand more the display() method of that.
    – Zollie
    Dec 9, 2021 at 14:57
  • Great! I'll try to formulate a short, sensible answer on this.
    – Zollie
    Dec 9, 2021 at 15:14

1 Answer 1

2

Many years ago I had the same question: "where should I put my own code in Joomla?". It was very frustrating at that time for me too and Joomla has a long learning curve, comparing to WordPress for example. Many of us easily can forget how confusing Joomla can be if someone wants to customize it or understand it. No wonder why WordPress is used like on 5-600 million websites and Joomla is like on 3 million. That is an extremely big difference and I think the simple reason for this is told in this question even if the answer of this question could be only 1 line:

Use and override the display() method.

It is only my own opinion - Joomla is a good example of trying to make something clean and simple but at the end it gets way too complex to understand (Joomla4 for example took about 7 years to develop of course no-one can understand it in one week or two). I see thousands of packages in Javascript field (NextJS, Node js ie.) doing similarly in a smaller manner nowadays. With a good intention they try to provide "a solution" in a package or more, then not enough documentation, too complex logic, only few people have time to understand it, then it gets abandoned and wrecks applications. It is different of course, but the point is the frustration level you can find in full stack development when you just want to display "hello world" and it takes a week setup (and docker, and kubernetes, and npm, node js, and so on...)

Instead of writing any explanation here I would just link the Joomla documentation which describes how Joomla handles requests and how and why the Controller is responsible for deciding whether it loads the View (and which view) or redirect:

https://docs.joomla.org/Model-View-Controller

UPDATE:

Since it was not part of the original question I actually just avoided this but in Joomla 3 (similar in Joomla 4 ), in your component, you can extend BaseController, FormController or AdminController, or if you do not want to use the namespaced Controllers, then just extend JControllerAdmin for example (at list views), so you do not extend just Controller. So, that is wrong in your Controller code.

So, I suggest you to use either (this one was used in Joomla3) :

class MycomponentnameControllerProfiles extends JControllerAdmin {...}

or with namespaced (used now in Joomla4 can be used in Joomla 3 too):

// on the top of the file we include:
use Joomla\CMS\MVC\Controller\AdminController;
...

// and then the class (in Joomla4)
class ProfilesController extends AdminController {...}

// in Joomla 3
class MycomponentnameControllerProfiles extends AdminController {...}
4
  • @HuubS - I updated, then if your issue is OK, we have to delete these added comments, or a moderator will...
    – Zollie
    Dec 20, 2021 at 13:08
  • @HuubS - yes, please change it as I wrote in my answer, since that is the standard way in Joomla.
    – Zollie
    Dec 20, 2021 at 13:18
  • Extend the Joomla core controllers everywhere in your component, not only in your root…
    – Zollie
    Dec 20, 2021 at 13:20
  • @HuubS - I am sorry, I just corrected my example for Joomla 3 with namespaced version, please use that updated form. The last example. Since Joomla 3 is still looking for the old version class name structure.
    – Zollie
    Dec 20, 2021 at 13:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.