Administration with Cake 3 - Prefix

I am actually testing CakePHP3, and I don’t know how to administer, with Cake 2, in Core we can write

Configure::write('Routing.prefixes', array('admin')); 

But with CakePHP3 we don’t have Core, and I don’t find this code in the documentation! I am trying to route

 Router::prefix('admin', function ($routes) { //My route $routes->fallbacks('InflectedRoute'); }); 

but without success!

+6
source share
4 answers

Three steps:

  • In config/routes.php : add the following to the end of the file:

     Router::prefix('Admin', function ($routes) { $routes->fallbacks('InflectedRoute'); }); 
  • Create src/Controller/Admin and move the UsersController.php file (which you would otherwise create at the top level of the Controller ) into it. Add the following line immediately after the <?php tag

     namespace App\Controller\Admin; 
  • Create views. Create the Template/Users/Admin folder and upload all the add.ctp, edit.ctp, view.ctp files to it.

The administrator prefix will be used to refer to the administrator section. Replace Admin if you want a different prefix.

I just found that this is the best way to do this at the moment. If anyone has a better way, please share it.

+12
source

In the concept of shadow conversion of tags 3.X, Configure :: write ('Routing.prefixes', array ('admin')); If you want to use it, create an administrator folder inside the ur controller folder and write the name of this name in this folder. If you want the admin_index () function, for example, 2.X, then u write only the index () function in the controller of the admin folder.

Please pay attention to one thing in ur router.php and add this code

 Router::prefix('admin', function ($routes) { //My route $routes->fallbacks('InflectedRoute'); }); 
+2
source

I found solutions for admin prefix. Please review the following code:

in app / config / routes.php

 Router::prefix('admin', function($routes) { $routes->connect('/', ['controller'=>'Users','action'=>'login']); $routes->fallbacks('InflectedRoute'); }); 

Make admin folder inside src controller: src / Controller / Admin / UsersController.php

add this line of code to UserController.php

 namespace App\Controller\Wcbadmin; 

Now make a file of the form: SRC / Template / Admin / Users / login.ctp

+1
source

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 } 
+1
source

Source: https://habr.com/ru/post/983125/


All Articles