How to create AppController in a Admin area in CakePHP 3

950 views Asked by At

I created Admin area by setting prefix "admin" in routes.php file:

Router::prefix('admin', function ($routes) {
    // All routes here will be prefixed with `/admin`
    // And have the prefix => admin route element added.
    $routes->fallbacks(DashedRoute::class);
});

Now when I run

bin/cake acl_extras aco_sync

it give me the following error:

Welcome to CakePHP v3.3.10 Console

App : src
Path: C:\wamp\www\d3\src\
PHP : 5.5.12

Fatal error: Class 'App\Controller\Admin\AppController' not found in C:\wamp\www\d3\src\Controller\Admin\ErrorController.php on line 25

From the error above I understand that it is looking for an AppController in the src/Controller/Admin/ area so I do create an AppController there with the following code:

namespace App\Controller\Admin;

use App\Controller\AppController;

class AppController extends Controller
{

}

Edit: I was wrong. We cannot extend Controller here as we did not include it,. We need to and actually want to extend AppController and hence we need to name our Admin/AppController class to something like AdminAppController extends AppController?? If so, how other local Controllers use this? It seems quite confusing to me right now.

but now new error occurs in the console which says:

Fatal error: Cannot declare class App\Controller\Admin\AppController because the name is already in use in C:\wamp\www\d3\src\Controller\Admin\AppController.php on line 7

I am not sure what is the best way to have a local AppController.php for /Admin/ name space. Do I really need one anyways, I think yes in order to override the admin related functionality and actually to fix the acl_extra acl_sync related error in the console, on the first hand.

1

There are 1 answers

1
BAMABARA Aristide On

I got it by doing :

namespace App\Controller\Admin;

use Cake\Controller\Controller;

class AppController extends Controller
{
    .....
}

AND in Any other Admin Controller, Just do, Example: For UsersController :

use App\Controller\**Admin**\AppController;


class UsersController extends AppController
{
   ......
}