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
2. Now all the controller functions prefixed with "admin_" are admin functions and will be accessible with URL's such as:
-
/admin/posts/add
-
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
-
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
-
<html xmlns="http://www.w3.org/1999/xhtml">
-
<head>
-
</head>
-
<body>
-
</div>
-
-
</body>
-
</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
-
class AppController extends controller
-
{
-
// this is a cool function which comes useful in many cases
-
function beforeFilter()
-
{
-
// if its the administrator/manager - change the layout
-
if($pos == true)
-
{
-
$this->layout='admin';
-
}
-
}
-
}
-
?>
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
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. […]
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. […]