Best way to implement admin panel in CakePHP

10,376

Solution 1

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

Solution 2

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

Share:
10,376
Jatinder Thind
Author by

Jatinder Thind

Updated on June 18, 2022

Comments

  • Jatinder Thind
    Jatinder Thind almost 2 years

    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?

  • Jatinder Thind
    Jatinder Thind about 11 years
    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.
  • Jatinder Thind
    Jatinder Thind about 11 years
    What about the plugin approach? I googled one or two admin plugins for CakePHP. Does that offer any additional benefits over separate apps?
  • thaJeztah
    thaJeztah about 11 years
    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 projects
  • thaJeztah
    thaJeztah about 11 years
    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 application
  • floriank
    floriank about 11 years
    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.
  • Jatinder Thind
    Jatinder Thind about 11 years
    Thanks for the explanation. Unfortunately can't upvote you answer because I don't have the require reputation points.
  • Jatinder Thind
    Jatinder Thind about 11 years
    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.
  • bowlerae
    bowlerae about 11 years
    I upvoted for you. thaJeztah has helped me a lot on my CakePHP problems.
  • thaJeztah
    thaJeztah about 11 years
    @bowlerae thanks for the up vote, I hope my answer will be educational for other people as well :)
  • user6972
    user6972 almost 11 years
    What admin plug-in do you recommend? And do you use prefix routing along with them?
  • thaJeztah
    thaJeztah almost 11 years
    @user6972 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)