Best way to implement admin panel in CakePHP
Asked Answered
P

6

6

I am trying to move from CodeIgniter to CakePHP and can't figure out the best way to implement an admin panel. In CI I would create two different applications, one for the frontend and one for the admin panel.

After Googling around, I have found three ways to implement admin panel in CakePHP:

  1. Routing - I don't want to use this as I want by Controllers/Models to be separate for frontend and admin panel
  2. Plugin
  3. Two separate apps

Should I use plugin to implement admin panel or should I have separate apps? Any benefits of one over the other?

Pfister answered 19/2, 2013 at 16:11 Comment(2)
#14474523Gilgilba
Go with 1. Routing, definitely.Duro
F
16

I normally develop the admin/backend as a plugin. This keeps your backend/admin controllers/views/models separated from the frontend and you don't have to jump through hoops to have separate stylesheets, layouts etc.

Another advantage is that both front- and backend are still part of the same application, so if desired, you can share logic/components, for example you'll be able to put helpers that are usable both for front- and backend in another plugin (e.g. plugins/Shared or plugins/Handytexttools) and use those both wherever you want

As a rule of thumb; put components that may be reuseable for other projects in a separate plugin, this way you can just add those plugins to other projects without problems. Keep your plugins simple; it's no problem to create a plugin containing just one or two helpers or models and a few files of JavaScript. This will make it easier to 'cherry pick' the plugins that you need for a project. Once Cake has 'cached' the file-locations of all classes in your plugins, the overhead of separate plugins should be minimal.

Coming back to the 'admin' plugin. Try to only include code specific for this project in your admin plugin and reusable parts in another one (e.g. Generic stylesheets and layouts for admin-panels). You'll be able to start a admin-plugin for your next project with minimal coding

Good luck with your project and enjoy CakePHP

Frigidarium answered 19/2, 2013 at 19:30 Comment(5)
Thanks for the explanation. Unfortunately can't upvote you answer because I don't have the require reputation points.Pfister
I upvoted for you. thaJeztah has helped me a lot on my CakePHP problems.Bunns
@Bunns thanks for the up vote, I hope my answer will be educational for other people as well :)Frigidarium
What admin plug-in do you recommend? And do you use prefix routing along with them?Scaremonger
@Scaremonger We (the company I work for) didn't use an existing plugin, but developed one in-house, using Twitter Bootstrap for the GUI (also developed a CakePHP plugin for that in-house). We don't use prefix routing, because having a plugin already gives you a prefix in your URLs (/myplugin/mycontroller/myaction)Frigidarium
F
4

If you want to keep your controllers and models separate - I'd go with a separate app, although you'll end up with a bunch of duplicate code between the apps (maintenance headache waiting to happen).

My choice would be admin routing and an admin theme.

Enable admin routing in /app/Config/core.php

In AppController beforeFilter():

$this->theme = isset($this->params['admin']) ? "Admin" : "Site";

Move all your site views and assets into /app/View/Themed/Site/

Create your admin themes in /app/View/Themed/Admin

Fauman answered 19/2, 2013 at 16:56 Comment(3)
What about the plugin approach? I googled one or two admin plugins for CakePHP. Does that offer any additional benefits over separate apps?Pfister
Please read my answer, I tried to explain the advantages of using a plugin. Developing a separate application for the admin panel is probably only desirable if the admin panel is designed to manage several websites. If not, I would not create a separate applicationFrigidarium
I personally feel routing based admin is useful only for simple admin panels. Plus I don't like that idea of admin and frontend code in the same file. Decided to go the plugin way for now.Pfister
A
0

Old and refers to CakePHP 1.3, but still is a question you should check: CakePHP admin panel

The Cake way is routing. I'd go with a plugin like CakeDC Users that makes things easier.

Anthea answered 19/2, 2013 at 16:30 Comment(2)
I have already checked the post you are referring to and that just points to a routing based admin panel which I don't want to use. I would prefer to keep my controllers/models separate if I can.Pfister
You can use an "admin" plugin but honestly this idea sucks and you'll end up with duplicate code. I had to deal with situations like this before in projects and refactored it to use prefix routing. In fact you gain nothing by putting the code in a separated plugin for example.Johanna
R
0

You could use admin-routing. Check out:

http://book.cakephp.org/2.0/en/development/routing.html#prefix-routing

Another solution -which I find really easy to implement- is like this:

In your AppController:

public function beforeFilter(){
        $this->set('current_user', $this->Auth->user());
         }  

This makes the $current_user available in your app.

Then in your view-files, you can check:

  <?php if ($current_user['role'] == 'admin'){/*place code for admin users to see here*/} ?>
  <?php if ($current_user){/*place code for logged-in users to see here*/} ?>
Resale answered 19/2, 2013 at 17:10 Comment(1)
Admin prefixing is a nice feature for quick, ad-hoc admin functionality, but will not separate front- and backend functionality. If you like to separate both, prefix-routing is not the best way to implement this. Also, putting too much logic in your views (if admin etc), is hard to maintain, especially for larger projectsFrigidarium
E
0

I know this is an old thread. But would like to ask if anyone had trouble implementing the admin panel as a plugin. Particularly duplication of code.

For example you're implementing an e-commerce site. You have an OrderController both in the main and admin plugin. Don't you think it's kinda hard to maintain the logic in two places?

How about just using one main controller. It's serves two purpose. One as an API then the controller for your Admin webapp.

Your public side would then basically communicate via API to fetch data.

Do you think it's a good idea?

Electrolyte answered 23/9, 2013 at 10:30 Comment(0)
E
0

You can use admin views like admin_index.ctp just change this

//Configure::write('Routing.admin', 'admin');

to

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

in core.php and in the controller add admin_index() function

Envy answered 23/9, 2013 at 11:31 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.