Thomas
Thomas

Reputation: 3

cakephp 3.0 same controller name for admin and users

I am using cakephp 3.0 and want to write some admin actions and some users actions in users controller. admin actions should be accessible from admin routing and user actions should be accessible without admin routing

I know in cakephp 3.0 for admin, users controller should be in

src/Controller/Admin/UsersController.php

and for normal users, users controller should be in

src/Controller/UsersController.php

Now the confusion is, Is this correct to put same controller name is in 2 different directories or am I missing something?

Thanks in advance.

Upvotes: 0

Views: 1123

Answers (2)

Arvind K.
Arvind K.

Reputation: 1304

You can keep them in the same UsersController by defining routes in routes.php file something like this:

// Connect routes for admin prefix.
Router::connect(
    'admin/users',
    ['plugin' => false, 'controller' => 'Users', 'action' => 'adminIndex']
);
Router::connect(
    'admin/users/:action/*',
    ['plugin' => false, 'controller' => 'Users']
);

If you notice in the code above, I have specified adminIndex action for admin index action. By doing so you could create a different set of actions and views to keep them more manageable while keeping them at one place.

For larger application however it is good idea to keep admin area separated from the front end area. It's your choice after all.

Upvotes: 1

Bayezid Alam
Bayezid Alam

Reputation: 380

Since Your access point is difference like /src/Controller/Admin/ControllerName and /src/Controller/ControllerName You can use both ControllerName and ActionName remaining same.

Suppose you want to make a UsersController in both admin and public section. you also want to make an actionName in the controller. It would be like:

//Name it in Admin Directory  /src/Controller/Admin/UsersController.php
public function index()
{
    $this->layout = 'admin_default';
    $this->set('users', $this->paginate($this->Users));
    $this->set('_serialize', ['users']);
}

//Name it in Public Directory  /src/Controller/UsersController.php
public function index()
{
    $this->set('users', $this->paginate($this->Users));
    $this->set('_serialize', ['users']);
}

Hope it will make you understand.

Upvotes: 0

Related Questions