Reputation: 67
I am facing issue in cakephp3. In routes.php
I have used
Router::prefix('admin', function ($routes) {
$routes->connect('/', ['controller' => 'TblUsers', 'action' => 'index']);
});
I have placed the TblUsers
inside Admin
folder
In app controller code:
$this->loadComponent('Auth', [
'authenticate' => [
'Form' => [
'fields' => [
'username' => 'username',
//'password' => 'password'
],
'scope' => ['role' => '1']
]
],
'loginAction' => [
'controller' => 'Users',
'action' => 'login',
'admin'=>true,
'prefix'=>true
],
'redirectUrl' => [
'controller' => 'TblUsers',
'action' => 'index',
'admin'=>true,
],
'loginRedirect' => [
'controller' => 'TblUsers',
'action' => 'index',
'admin'=>true,
]
]);
Now when I access the users/login its giving me error:
Error: A route matching "array ( 'controller' => 'Users', 'action' => 'login', 'admin' => true, 'prefix' => true, 'plugin' => NULL, '_ext' => NULL, )" could not be found.
OR
Is there any way in cakephp3 like cakephp2 to access the functions inside any controller start with admin_functionname
and call the admin_functionname.ctp
file and also add the admin inside the url.
Thanks
Upvotes: 1
Views: 1092
Reputation: 1413
This example is for Simple page with Admin Panel:
Structure fodlers and files:
src
_Controller
_admin
__AppController.php
__UsersController.php
__OtherAdminController.php
_AppController.php
_UsersController.php
_OtherPublicController.php
_Template
_admin
__Users
_login.ctp
_add.ctp
_etc.ctp
__OtherAdminTemplate
_Users
_Pages
_etc
config/routes.php
Router::prefix('admin', function (RouteBuilder $routes) {
$routes->connect('/',['controller' => 'Users', 'action' => 'login']);
$routes->connect('/:controller',['action' => 'index'],['routeClass' => 'DashedRoute']);
$routes->connect('/:controller/:action/*',[], ['routeClass' => 'DashedRoute']);
});
Controller/Admin/AppController.php
<?php
namespace App\Controller\Admin; // <---- Name your admin folder
use App\Controller\AppController as Controller;
use Cake\Event\Event;
use Cake\Core\Configure;
/**
* App Controller
*
* @property \App\Model\Table\AppTable $App
*/
class AppController extends Controller
{
public function initialize()
{
parent::initialize();
$this->loadComponent('Auth', array(
'authenticate' => array(
'Form' => array('fields' => array('username' => 'email', 'password' => 'password'))
),
'loginRedirect' => array(
'controller' => 'Users',
'action' => 'index'
),
'logoutRedirect' => array(
'prefix' => false,
'controller' => 'Pages',
'action' => 'home',
),
));
$this->Auth->deny();
}
public function beforeFilter(Event $event)
{
// If you want use admin.ctp layout
if ($this->request->params['prefix'] === 'admin') {
$this->viewBuilder()->layout('admin');
}
//Autorized acctions without registration
$this->Auth->allow(['forgotPassword', 'resetPassword']);
}
}
Controller/Admin/UsersController.php
<?php
namespace App\Controller\Admin; // <---- Name your admin folder
use App\Controller\Admin\AppController; // <---- Name your admin folder
use Cake\Mailer\Email;
use Cake\Routing\Router;
use Cake\Core\Configure;
/**
* Users Controller
*
* @property \App\Model\Table\UsersTable $Users
*/
class UsersController extends AppController
{
public function login()
{
//Admin Login function
}
//Rest of Users admin functions
}
Upvotes: 0
Reputation: 1714
You need to change your prefix to the actual prefix, not true
.
So change this;
'loginAction' => [
'controller' => 'Users',
'action' => 'login',
'admin'=>true,
'prefix'=>true
To this;
'loginAction' => [
'controller' => 'Users',
'action' => 'login',
'prefix'=> 'admin'
You'll need to do the same with your loginRedirect and redirectUrl
You can find more information here http://book.cakephp.org/3.0/en/development/routing.html#prefix-routing
Upvotes: 2