# Cakephp 4.2 | How to override the MVC with the MVC from other plugin?

**URL:** <https://discourse.cakephp.org/t/cakephp-4-2-how-to-override-the-mvc-with-the-mvc-from-other-plugin/9308>\
**Category:** Uncategorized\
**Created:** [April 29, 2021, 11:39am UTC](https://discourse.cakephp.org/t/cakephp-4-2-how-to-override-the-mvc-with-the-mvc-from-other-plugin/9308 "2021-04-29T11:39:46Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Salines](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/salines/32/3166_2.png) [@Salines](https://discourse.cakephp.org/u/Salines)\
**Post date:** [April 29, 2021, 11:39am UTC](https://discourse.cakephp.org/t/cakephp-4-2-how-to-override-the-mvc-with-the-mvc-from-other-plugin/9308/1 "2021-04-29T11:39:46Z")

</div>

I have a complex web app, where special plugins are created for user groups. Eg Admin, Editor all have their groups. Each of these groups has its own tasks and customized forms in processing one saved entry, often with a different layout / elements.

It has worked great so far. But now we have one new request from the client, their special customer needs a completely different or customized flow, which is not compatible with the existing MVC structure. This means that in one user group we have to add some functionality from other groups, also change templates, validation, relations.

In this case we do the overriding template via an additional plugin, but how about the override controller and the model?

For example: When we call [example.com/editor/articles/add](http://example.com/editor/articles/add) the plugin Editor has its own MVC, for a special customer, the Editor should use the MVC from the SpecialEditor plugin.

How to override the existing MVC?

---

<div class="post-metadata">

**Author:** ![raul338](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/raul338/32/69_2.png) [@raul338](https://discourse.cakephp.org/u/raul338)\
**Post date:** [April 29, 2021, 3:17pm UTC](https://discourse.cakephp.org/t/cakephp-4-2-how-to-override-the-mvc-with-the-mvc-from-other-plugin/9308/2 "2021-04-29T15:17:28Z")

</div>

You can override controller via routes:

```php
// routes.php or Application::routes()
$routes->plugin('Editor', function (RouteBuilder $routes) {
    // Routes connected here are prefixed with '/debug-kit' and
    // have the plugin route element set to 'DebugKit'.
    $routes->connect('/articles/add', ['plugin' => 'SpecialEditor', 'controller' => 'Articles', 'action' => 'add']);
});

```

Inside the `ArticlesController` of the `SpecialEditor` plugin you can load a new model (eg extend original model) and also have the template inside that plugin too.

Also, if you only have to override the controller and not the model, set the modelClass

```php
namespace SpecialEditor\Controller;

class ArticlesController
{
   public $modelClass = "Editor.Articles";
}

```

---

<div class="post-metadata">

**Author:** ![Salines](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/salines/32/3166_2.png) [@Salines](https://discourse.cakephp.org/u/Salines)\
**Post date:** [May 5, 2021, 12:11pm UTC](https://discourse.cakephp.org/t/cakephp-4-2-how-to-override-the-mvc-with-the-mvc-from-other-plugin/9308/3 "2021-05-05T12:11:30Z")

</div>

I’m sorry, but it doesn’t work.

> You cannot define routes that conflict with the scope. Scope had plugin = Editor, while route had plugin = SpecialEditor

The question still applies, how to override the controller with another. Is there a similar method as when override a template?

It would be ideal if we could switch plugins for the user but keep the same url.

---

<div class="post-metadata">

**Author:** ![raul338](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/raul338/32/69_2.png) [@raul338](https://discourse.cakephp.org/u/raul338)\
**Post date:** [May 10, 2021, 1:30am UTC](https://discourse.cakephp.org/t/cakephp-4-2-how-to-override-the-mvc-with-the-mvc-from-other-plugin/9308/4 "2021-05-10T01:30:34Z")

</div>

I have tried this

in the plugin’s `src/Plugin.php`

```php
public function routes(RouteBuilder $routes): void
{
    parent::routes($routes);
    $params = ['controller' => 'ExportTypes', 'plugin' => 'MyPlugin'];
    $routes->scope('/export-types', $params, function (RouteBuilder $builder) {
        $builder->connect('/', ['action' => 'index']);
    });
   // ....
}

```

in the app `config/routes.php`

```php
$routes->prefix('api', function (RouteBuilder $builder) {
    $builder->scope('/export-types', ['plugin' => false, 'controller' => 'ExportTypes'], function (RouteBuilder $builder) {
        $builder->connect('/', ['action' => 'index']);
    });
    $builder->fallbacks();
});

```

`bin/cake routes` output:

```auto
+--------------------------------+--------------------+----------------------------------------------------------------------------------+
| Route name | URI template | Defaults |
+--------------------------------+--------------------+----------------------------------------------------------------------------------+
| api:0.exporttypes:index | /api/export-types | {"action":"index","controller":"ExportTypes","plugin":false,"prefix":"Api"} |
| api:myplugin.exporttypes:index | /api/export-types | {"action":"index","controller":"ExportTypes","plugin":"MyPlugin","prefix":"Api"} |
+--------------------------------+--------------------+----------------------------------------------------------------------------------+

```

I used the `api` prefix since I only use as a rest backend, but should work without it
