We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 here.

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



31.9k

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



10.5k
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;


31.9k

thanks man! I like code examples....