Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Administration with Cake 3 - Prefix

Tags:

cakephp-3.0

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 !

like image 574
Ayekan Avatar asked Feb 27 '15 19:02

Ayekan


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 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.

like image 186
Joel Fernandes Avatar answered Nov 13 '22 09:11

Joel Fernandes


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

like image 40
Amuk Saxena Avatar answered Nov 13 '22 11:11

Amuk Saxena


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');
}); 
like image 2
Pradeep Kumar Avatar answered Nov 13 '22 11:11

Pradeep Kumar


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


}
like image 1
bowlerae Avatar answered Nov 13 '22 09:11

bowlerae