Not sure if anyone ever got you the answer you need. I also had a problem with prefix routing. I found that all of these solutions, even the CakePHP 3.X cookbook, left some details. Below I definitely did it. I am using version 3.1.3.
In my routes.php file , I added
Router::prefix('admin', function ($routes) { // All routes here will be prefixed with `/admin` // And have the prefix => admin route element added. $routes->connect('/', ['controller' => 'Index', 'action' => 'index']); $routes->fallbacks('DashedRoute'); });
This is from a cookbook in prefix routing . All I changed is changing the controller from “Pages” to “Index”.
Then I created a controller located in src / Controller / Admin / IndexController.php
The contents of my controller were originally (be careful, this part was wrong, I fix it later)
<?php namespace App\Controller; // INCORRECT NAMESPACE // INCORRECTLY MISSING use Cake\Controller\AppController use Cake\Network\Exception\NotFoundException; use Cake\View\Exception\MissingTemplateException; use Cake\Event\Event; class IndexController extends AppController{ public function index(){ $this->set('title_for_layout', 'Admin Dashboard'); } // END INDEX ACTION }
After receiving the error "Class IndexController not found", I had to modify the file as follows. I found this by reading the entire error message. This was information that I did not see in the cookbook or any of the answers you received.
<?php namespace App\Controller\Admin; // THIS IS THE CORRECT NAME SPACE use App\Controller\AppController; // HAVE TO USE App\Controller\AppController use Cake\Network\Exception\NotFoundException; use Cake\View\Exception\MissingTemplateException; use Cake\Event\Event; class IndexController extends AppController{ public function index(){ $this->set('title_for_layout', 'Admin Dashboard'); } // END INDEX ACTION }
source share