AKKAweb
AKKAweb

Reputation: 3807

CakePHP 3 - Admin Routing to Plugin Not Working

I am creating a plugin that I want integrated within the admin section of my application. My application structure for the admin section looks like this:

src/Controller/Admin/AdminsController.php
src/Controller/Admin/ProductsController.php
src/Controller/Admin/BlogsController.php

AdminsController.php looks like this:

namespace App\Controller\Admin;

use App\Controller\AppController;
use Cake\Event\Event;
use Cake\Network\Exception\ForbiddenException;

class AdminsController extends AppController{

And my Admin controllers, ie BlogsController.php looks like this:

namespace App\Controller\Admin;

use App\Controller\Admin\AdminsController;

class BlogsController extends AdminsController {

My plugin has a FeedbacksController that looks just like the Blogs Controller above, which also uses AdminsController from the application: plugin/AkkaFeedback/src/Controller/Admin/FeedbacksController.php

namespace App\Controller\Admin;

use App\Controller\Admin\AdminsController;

class FeedbacksController extends AdminsController {`

Also, within my plugin I have plugin/AkkaFeedback/src/Controller/FeedbacksController.php

My Intention is to have /admin/feedbacks point to this controller. Is this even possible within CakePHP? I have tried many possibilities without success. Here is what I have tried as well as others without success:

Router::prefix('admin', function ($routes) {
    $routes->connect('/', ['controller' => 'Dashboards', 'action' => 'index']);

    $routes->connect('/feedbacks', ['plugin' => 'AkkaFeedback', 'controller' => 'Feedbacks', 'action' => 'index']);

    // I have tried this
    //$routes->connect('/feedbacks', ['plugin' => 'AkkaFeedback', 'controller' => 'Feedbacks', 'action' => 'index', 'prefix' => 'admin']); // I have also tried this

    // And this without succcess
    // /admin/akka_feedback/feedbacks
    // $routes->plugin('AkkaFeedback', function ($routes) {
    //    $routes->connect('/:controller');
    // });

    $routes->fallbacks('InflectedRoute');
});

The error I get is: Controller class Feedbacks could not be found., but there is a Feedbacks class, both in Controller and Controller/Admin within the plugin.

Not sure what else to try. Any ideas would be appreciated!

Upvotes: 3

Views: 1312

Answers (3)

In config/routes.php:

Router::prefix('admin', function ($routes) {
    $routes->fallbacks(DashedRoute::class);
});

In plugins/CustomPlugin/config/routes.php:

Router::prefix('admin', function ($routes) {
    $routes->plugin('CustomPlugin', ['path' => '/custom-plugin'], function ($routes) {
        $routes->fallbacks(DashedRoute::class);
    });
});

It seems the best method and it works properly. In this way i can have an Admin section in the root project and also in the plugin.

Upvotes: 1

AKKAweb
AKKAweb

Reputation: 3807

After some more researching I was able to make it work by adding the following to the plugin's routes.php file plugins/AkkaFeedback/config/routes.php:

Router::prefix('admin', function ($routes) {
    $routes->plugin('AkkaFeedback', function ($routes) {
        $routes->connect('/:controller', ['action' => 'index'], ['routeClass' => 'DashedRoute']);
        $routes->connect('/:controller/:action/*', [], ['routeClass' => 'DashedRoute']);
    });
});

In conjunction with the routes added to the routes.php file of the application.config/routes.php

Router::prefix('admin', function ($routes) {
    $routes->connect('/', ['controller' => 'Dashboards', 'action' => 'index']);
    $routes->connect('/feedbacks', ['plugin' => 'AkkaFeedback', 'controller' => 'Feedbacks', 'action' => 'index']);
    $routes->connect('/feedbacks/:action/*', ['plugin' => 'AkkaFeedback', 'controller' => 'Feedbacks']);

    $routes->fallbacks('InflectedRoute');
});

I am not sure if this is the best way, but it works for now.

Upvotes: 3

user3082321
user3082321

Reputation: 654

Your FeebacksController namespace declaration isn't correct. It should be:

namespace AkkaFeedback\Controller\Admin;

use App\Controller\Admin\AdminsController;

class FeebacksController extends AdminsController {`

Upvotes: 2

Related Questions