i'm actually at test CakePHP3, and i don't know how do a administration, with Cake 2, in Core we can write
Configure::write('Routing.prefixes', array('admin'));
But with CakePHP3, we don't have Core, and in documentation i don't find this code ! I try the routing
Router::prefix('admin', function ($routes) {
//My route
$routes->fallbacks('InflectedRoute');
});
but without success !
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 your UsersController.php
file (which you would have otherwise created in the upper-level Controller
) into it. Add the following line right after the <?php
opening tag
namespace App\Controller\Admin;
Create the views.
Create a Template/Users/Admin
folder and dump all the add.ctp, edit.ctp, view.ctp files into it.
The Admin prefix will be used to refer to the admin section. Replace Admin
if you want a different prefix.
I just found this is the best way of doing it at the moment. If anyone has better way then please share it.
I found the solutions for admin prefix. Please have a look on 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 Controller src: src/Controller/Admin/UsersController.php
add this line of code in UsersController.php
namespace App\Controller\Wcbadmin;
Now make the view file: src/Template/Admin/Users/login.ctp
In cakephp 3.x change routing concept,
Configure::write('Routing.prefixes', array('admin'));
If u want to use it,
Please make a admin folder inside ur controller folder and write a same name controller name in this folder.
If u want to admin_index()
function like 2.x then u write only index()
function in admin folder controller .
Note one things, in your router.php
add this code
Router::prefix('admin', function ($routes) {
//My route
$routes->fallbacks('InflectedRoute');
});
Hey not sure if anyone ever got you the answer you needed. I was also having an issue getting prefix routing to work. I found that all of these solutions, even the CakePHP 3.X cookbook left out some details. Below is exactly what I did. 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 the cookbook on prefix routing. All I changed was changing the controller from "Pages" to "Index".
I then created a controller located at src/Controller/Admin/IndexController.php
The contents of my controller were originally this (beware this part was incorrect, I correct it afterwards)
<?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 an error message saying "Class IndexController not found" I then had to change the file to the following. I found this out by reading the entire error message. This was the information that I did not see in the cookbook or any of the answers you have 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
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With