Change controller directory for admins

Not for 'how-to' coding questions but PHP theory instead, this forum is here for those of us who wish to learn about design aspects of programming with PHP.

Moderator: General Moderators

Post Reply
User avatar
Luke
The Ninja Space Mod
Posts: 6424
Joined: Fri Aug 05, 2005 1:53 pm
Location: Paradise, CA

Change controller directory for admins

Post by Luke »

I'm building an application within the Zend Framework. Within my application, if you are an admin you have access to the administration panel, which is separate from the application (like phpbb's). What I want to know, is how I would tell the controller object to load its controllers from the controllers/admin directory if the user is logged in as an administrator. Should I write a plugin? Should I abstract the Action Class? (I've already done so for other reasons, but should I put the functionality in there somewhere?).
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

In your index.php, you could do

Code: Select all

$user = new Zend_Session('user');
if ($user->permission > 0)
{
   $controller->setControllerDirectory('Application/Admin/');
}
else
{
   $controller->setControllerDirectory('Application/Controller');
}
I personally wouldn't seperate the controllers into two seperate folders.. after all they are all controllers.
User avatar
Luke
The Ninja Space Mod
Posts: 6424
Joined: Fri Aug 05, 2005 1:53 pm
Location: Paradise, CA

Post by Luke »

The only reason I want to is so that I can tell at a glance (in the directory) which controllers are for admins and which are just users. Is there a better way to accomplish what I want?

By the way, I had thought of that method you posted, but it feels like there should be a better way. Thank you. I'll just do that.
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

After taking a look at the source,

Code: Select all

public function setControllerDirectory($path)
    {
        $dirs = (array) $path;
        foreach ($dirs as $key => $dir) {
            if (!is_dir($dir) or !is_readable($dir)) {
                throw new Zend_Controller_Dispatcher_Exception("Directory \"$dir\" not found or not readable");
            }
            $dirs[$key] = rtrim($dir, '/\\');
        }

        $this->_directories = $dirs;
        return $this;
    }
You can pass an array of paths. Enjoy.
User avatar
Luke
The Ninja Space Mod
Posts: 6424
Joined: Fri Aug 05, 2005 1:53 pm
Location: Paradise, CA

Post by Luke »

Thanks! :D
Post Reply