Plugin how to overwrite controller

I don’t really understand plugins.

I have an action performed on my PageController.php (in my main App)
This action is “execute a query on a table”

  $seeds = $this->ProductSeeds->find('all', [
            'conditions' => [
                'ProductSeeds.is_active' => true
            ]
        ])->first();

My idea is to overwrite my PageController method … and not perform this query.
I want use a plugin to change this behaviour.

Do you never want that action to happen? Just change the PageController. It’s your app after all.

You right but I must do it with a plug-in.
With plug-in I must

  • change template : it works
  • change/controller : it doesn’t work
  • change/extend model

So you right but I must use plugins in my case

It’s theoretically possible to get a plugin to do this, but it’s not as easy as you probably think it is. If you make a new controller in your plugin, presumably extending the non-plugin version but just implementing that one action, that will by default be reachable through a different URL, e.g. /plugin/page/view/1 instead of /page/view/1. That should also be changeable, but requires more workarounds. Go ahead, if that’s what you want to do, but from what you’ve told us so far, I very much question “I must use plugins in my case”.

The reason that changing a template works is because of how Cake handles templates, specifically taking plugins into account for the purposes of making theme plugins.

Extending a model sort of works, because you can always specify which one you’re looking for in your controller, and tell it you’re looking for the plugin one. But if you just create a “Users” model in your plugin and expect that the Users controller in the main application would start to use it without additional changes, that’s not going to work.

You don’t overwrite controllers. You setup routes so the URL(s) map to controller you want.

So if you don’t want a particular URL to map to your PagesController’s action then add a route to connect that URL to your plugin’s controller instead.

1 Like

Thanks uhm
I can override any plugin views from inside your app using special paths, this is clear.
I supposed i can do the same with controllers basicaly this :

**src/Template/Plugin/[Plugin]/[Controller]/UserController.php

My idea was : my plugin Controller have higher priority compared app User Controller.

I supposed the same thing works with models also Models

In my case i want this :

  • disable some logic in the controller (esamble display function) … when plugin is active
  • disable some controllers when plugin is active
  • disable some models linked to not present tables when plugin is active.

Basically i have a main project with some features … and i want use plugins to remove or add functionality … in other custom projects.
1 website and 6 custom versions with less functionalities and more. This is why i asked this strange question.

Can you help me understand more of what i can and can’t do with plugins ? I think i’m confused section of overriding templates with plugin controllers and plugin models.

Thanks a lot

Your suppositions along these lines were all incorrect, I’m sorry to say. As we’ve said, plugins can override templates, but controllers are selected by changing your routing. There are various ways to do what you’re looking for, but even just describing the basics of different options is a lengthy discussion. Here is a topic I had a while ago about this sort of thing, maybe it will help. But plugins aren’t the only way to go; in addition to using plugins, my app has bits of functionality that are enabled or disabled through config files or through administrative setting pages.