Solved thread

This post is marked as solved. If you think the information contained on this thread must be part of the official documentation, please contribute submitting a pull request to its repository.

how to solution the same controller in different sub dir

I created an sub folder admin in controllers folder, Now here have two file: controllers/loginController.php and controllers/admin/loginController.php, I want to call controllers/admin/loginController.php but the controllers/loginController.php always been called...how can i do..

The easiest way is to use namespaces in PHP



15.7k

Thanks for your answer, but the controller is called when the application is initialized, not manually calling. can it works?



7.0k
Accepted
answer

Yes, Its work easily. Move all admin controllers to Admin namespace then add following code to your application bootstrap:

    // Loader, registering namespaces
    $loader = new Phalcon\Loader();
    $loader->registerNamespaces(array(
        'Admin'   => YOUR_APP_PATH . '/controllers/admin',
    ));
    $loader->register();

and add these lines to your router settings:

        $admin = new \Phalcon\Mvc\Router\Group(array(
            'namespace'     => 'admin',
            'controller' => 'index',
            'action'     => 'index'
        ));
        $panel->setPrefix('/panel');

        $router->add('/admin(/:controller(/:action(/:params)?)?)?', array(
            'namespace'     => 'admin',
            'controller' => 2,
            'action'     => 4,
            'params'     => 6,
        ))->setName('admin');

        $router->mount($admin);

        return $router;


15.7k

thanks man! I like code examples....