我可以在PhalconPHP中使用多个名称空间

I am using the great PhalconPHP Framework. I have different user Levels and to load the right controllers, I am using namespaces in the dispacher class.

$di->set('dispatcher', function() {

    //Create an EventsManager
    $eventsManager = new EventsManager();

    //Remove extension before dispatch
    $eventsManager->attach("dispatch:beforeDispatchLoop", function($event, $dispatcher) {
         switch($user->getUserType()) {
             case 1:
                 //Student
                 $dispatcher->setNamespaceName('student');
                 break;
             case 2:
                  //Userlevel 2, yet to come
                  break;
             case 3:
                 //Admin &| Minion
                 $dispatcher->setNamespaceName('admin');
    }
    });

    $dispatcher = new MvcDispatcher();
    $dispatcher->setEventsManager($eventsManager);

    return $dispatcher;
});

The namespaces are registerd like so:

$loader->registerNamespaces(
    array(  
        'Student' => __DIR__ . '/../../app/controllers/student',
        'Admin' => __DIR__ . '/../../app/controllers/admin,
)

);

This works great but now I want also subdirectories, like "..controllers/admin/sub". The Controllers in "/sub" also have to be in the namespace "admin". (Because of the dispatcher) Or can I somehow do something like namespace Admin/* and php includes all subdirectories?

I hope I could explain my problem and someone can help. =)

IMHO, the better way of doing it would be in the router logic domain – your dispatcher dispatches things it's told to dispatch, and your router finds out what to dispatch and tells dispatcher that. You are mixing that between the both. I would extend the Router, overriding the handle method and set the namespace from there. The router would need to be registered with the DI (otherwise the base one will be used).

public function handle($uri = null)
{
    parent::handle($uri);

    switch($user->getUserType()) {
         case 1:
             //Student
             $this->_namespace = 'student';
             break;
         case 2:
              //Userlevel 2, yet to come
              break;
         case 3:
             //Admin &| Minion
             $this->_namespace = 'admin';
             break;
    }
}

For the actual question – it is also related to the way your routing should work. You need to specify sub namespaces in your routes or you can also do it in the handle function, like below. I tested and it works, but specifying it in the routes would be a better approach to keep related logic in one place.

public function handle($uri = null)
{
    $this->_controller = 'sub\\' . $this->_controller;
}

Edit: Also, probably not what you want to hear, but having sub folders for your controllers is probably not the best way to go forward. I don't know if there is a best practice, but it just seems to be a de facto to keep them flat in one directory per module / app. You can have multiple modules with their own controllers, Phalcon supports multimodules alright.