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

11 Dec

A better way to send emails than CakePHP’s default Email Component ?

Best Practices, CakePHP, Latest Developments

While working on a big project today, I had to use this default Email component. I was disappointed when I had to create two views for every singly mail... This is really bugging.. Plus, It is not fast as it can be.
At the same time, I've always loved the magic Model functions: findAllby

Today, I realized how productive it would make us if there's some similar magic functions for email as well. Say to send a user approval letter, you just have to write following in your controllers:

PHP:
  1. $this->Email->send_UserApproval($email, $verification_code)// to send user approval mail
  2. $this->Email->send_UserWelcome($email)//welcome mail
  3. $this->Email->send_AdminPaymentNotification($from);   // payment notification to admin

This does not only increases the speed of writing code, but also makes code look beautiful.

While I was thinking about the idea, I dugg into model class of CakePHP to find how its done, I found the source of magic:

PHP:
  1. function __call($method, $params, &$return) {
  2. $db =& ConnectionManager::getDataSource($this->useDbConfig);
  3. $return = $db->query($method, $params, $this);
  4. if (isset($this->__backAssociation)) {
  5. $this->__resetAssociations();
  6. }
  7. return true;
  8. }

This is the function which creates all Model:findAllby functions. So I decided to write my own component which will definately save time in many of the projects.

One good way of developing this would be creating a language/email file like emails.php with something like this:

PHP:
  1. 'user'=>array(
  2. 'Approval'=>'Hi user, You are about to get approved.',
  3. 'Welcome'=>'Welcome user, you can now play with our system.'
  4.  
  5. ),

This file will be read by Email::__call() and our magic functions will be created on the run.

What do you think of this method ? Is it better than current Email component ? I'll blog again as soon as my email component is ready..

10 Dec

Whats this fight going on in CakePHP team ?

CakePHP

I recently came across Daniel's post, I see he did made a good point about a function not doing something which it was supposed to do. But this post has turned to a fight against Daniel and other core developers. I personally give most of the credit to Daniel (cakebaker.42dh.com) for me to teach CakePHP through his blog. He recently left CakePHP development team, but his contributing efforts to blog about CakePHP, had been wonderful.
This was not just the first time, when I've seen other Cake developers doing this thing in public. There had been previous threads on same talks and insults from long.
Cakebaker deserves much more credit in the community as the others do.