14 Dec

CakePHP Tutorial - How to create an administrator panel in CakePHP

Articles, CakePHP

My new programmer wanted to know how to create administrator panel using CakePHP, so I thought to share my written tutorial with the community. Here it goes:

1. First, lets tell CakePHP that we need an admin panel to use for this app. Open config/core.php

PHP:
  1. define('CAKE_ADMIN', 'admin');   // uncomment this line if its commented already

2. Now all the controller functions prefixed with "admin_" are admin functions and will be accessible with URL's such as:

PHP:
  1. /admin/posts/add
  2. This URL will trigger Posts::admin_add()

You can define multiple function prefixed with 'admin_'. Remember all the views have to be with the same name i.e. admin_add.thtml

3. Now lets create a new layout for the admin in /app/views/layouts directory. Lets call it admin.thtml

PHP:
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  2. <html xmlns="http://www.w3.org/1999/xhtml">
  3. <head>
  4. <?php echo $html->css('cake.generic');?>
  5. </head>
  6. <body>
  7. <div id="content"><? echo $content_for_layout; ?></div>
  8. </div>
  9.  
  10. <?php echo $cakeDebug?>
  11. </body>
  12. </html>

4. Now we have the URL's for admin, we also have the controller functions for admin. Now we need to create a functionality which will automatically use the above layout for all of our admin functions. To do this, create a file app_controller.php in 'app' directory. Or you can also copy it from /cake.

PHP:
  1. <?php
  2. class AppController extends controller
  3. {
  4.     // this is a cool function which comes useful in many cases
  5.     function beforeFilter()
  6.     {
  7.         // if its the administrator/manager - change the layout
  8.         $pos = strpos($_SERVER['REQUEST_URI'], CAKE_ADMIN);
  9.         if($pos == true)
  10.         {
  11.             $this->layout='admin';
  12.         }
  13.     }
  14. }
  15. ?>

AppController::beforeFilter() function will assign admin.thtml immediately by checking the URL being called, and this will be reflected in all controllers of your application. Now you can access URL's like /admin/posts/add, /admin/posts/edit/1 or similar, but not /admin alone which carries out authorization.

That's all you need to do. Next step is adding a simple authorization for administrators, which I'll explain in my other tutorial.

- Abhimanyu Grover

2 Responses to “CakePHP Tutorial - How to create an administrator panel in CakePHP”

Subscribe to comments with RSS Feed or TrackBack from your own site.


  1. on Wed, 9 January, 2008 at 7:40 am

    […] As in my previous post about creating an administrator panel in CakePHP, my another programmer needed my help in telling him more about vendors. […]


  2. on Sat, 1 March, 2008 at 1:43 pm

    […] As in my previous post about creating an administrator panel in CakePHP, my another programmer needed my help in telling him more about vendors. […]

Leave A Reply