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 add namespace to module

when I make a request , it is taking default module namespace i.e (Frontend\Controllers) if the module is XXXXXXX



10.0k

What you mean? You want use another namespace?



2.7k

In multi module structure there are multi modules where one is default module say A and another module is installed B, but when make a request as https://localhost/B/controller/action , getting 404 error , when i analysed it in the router module,controller,action is correct but the name space was A/Controllers

On Thu, Sep 4, 2014 at 4:56 PM, Sweet-Bob [email protected] wrote:



10.0k
edited Sep '14

Show your routes.



7.9k

Here is example of my app routing that using multi module

Action with name : a-long-action-name in route will be translated to aLongActionNameAction

you can access admin module with /admin/controller/action/params

$di->set(
            $service,
            function () use ($config) {
                $router = new Router(false);
                $router->removeExtraSlashes(true);

                $router->setDefaultModule("customer");
                $router->setDefaultNamespace("LicMan\\Modules\\Customer\\Controllers\\");

                $router->add( "/:controller/([a-zA-Z\-]+)/:params", [
                        "controller" => 1,
                        "action" => 2,
                        "params" => 3
                    ])->convert("action", function ($action) {
                            return Text::camelize($action);
                        });

                $router->add("/:controller/([a-zA-Z\-]+)", [
                        "controller" => 1,
                        "action" => 2
                    ])->convert("action", function ($action) {
                            return Text::camelize($action);
                        });

                $router->add("/:controller", [
                        "controller" => 1,
                        "action" => "index"
                    ]);

                $admin = new Router\Group([
                    "module"        =>  "admin",
                    "controller"    =>  "index",
                    "namespace"     =>  "LicMan\\Modules\\Admin\\Controllers\\"
                ]);

                $admin->setPrefix("/admin");
                $admin->add( "/:controller/:action/:params", [
                        "controller" => 1,
                        "action" => 2,
                        "params" => 3
                    ])->convert("action", function ($action) {
                            return Text::camelize($action);
                        });

                $admin->add("/:controller/:action", [
                        "controller" => 1,
                        "action" => 2
                    ])->convert("action", function ($action) {
                            return Text::camelize($action);
                        });

                $admin->add("/:controller", ["controller" => 1,
                        "action" => "index"
                    ]);

                $admin->add("", array("controller" => "index", "action" => "index"));

                $router->mount($admin);

                return $router;
            }
        );


2.7k
edited Sep '14

My Custom Routes are

$defaultModule = ucfirst($registry->defaultModule);
        $router = new \Phalcon\Mvc\Router\Annotations(true);
        $router->removeExtraSlashes(true);
        $router->setDefaultModule(ucfirst($defaultModule));
        $router->setDefaultNamespace(ucfirst($defaultModule).'\Controllers');
        $router->setDefaultController("index");
        $router->setDefaultAction("index");

        $router->add('/:module/:controller/:action/:params', [
            'module' => 1,
            'controller' => 2,
            'action' => 3,
            'params'=>4
        ]);

        $router->add('/:module/:controller/:action', [
            'module' => 1,
            'controller' => 2,
            'action' => 3
        ]);

        $router->add('/:controller/:action', [
            'controller' => 1,
            'action' => 2
        ]);
        $router->add('/:controller', [
            'controller' => 1
        ]);
        $router->add('/', [
                'module' => ucfirst($defaultModule),
                'controller' => 'index',
                'action' => 'index'
                ]);

        $router->notFound(array(
            'module' => $defaultModule,
            'namespace' => ucfirst($defaultModule).'\Controllers',
            'controller' => 'Error',
            'action' => 'show404'
        ));

when I make a request to Module2/Controller/Action redirect to no page found After analyse try to print the eventmanager and saw namespace of default module i.e Module1/Controllers

How to resolve it ??



2.7k

Show your routes. My Custom Routes are

$defaultModule = ucfirst($registry->defaultModule);
        $router = new \Phalcon\Mvc\Router\Annotations(true);
        $router->removeExtraSlashes(true);
        $router->setDefaultModule(ucfirst($defaultModule));
        $router->setDefaultNamespace(ucfirst($defaultModule).'\Controllers');
        $router->setDefaultController("index");
        $router->setDefaultAction("index");

        $router->add('/:module/:controller/:action/:params', [
            'module' => 1,
            'controller' => 2,
            'action' => 3,
            'params'=>4
        ]);

        $router->add('/:module/:controller/:action', [
            'module' => 1,
            'controller' => 2,
            'action' => 3
        ]);

        $router->add('/:controller/:action', [
            'controller' => 1,
            'action' => 2
        ]);
        $router->add('/:controller', [
            'controller' => 1
        ]);
        $router->add('/', [
                'module' => ucfirst($defaultModule),
                'controller' => 'index',
                'action' => 'index'
                ]);

        $router->notFound(array(
            'module' => $defaultModule,
            'namespace' => ucfirst($defaultModule).'\Controllers',
            'controller' => 'Error',
            'action' => 'show404'
        ));

when I make a request to Module2/Controller/Action redirect to no page found After analyse try to print the eventmanager and saw namespace of default module i.e Module1/Controllers

How to resolve it ??



2.7k

Here is example of my app routing that using multi module

Action with name : a-long-action-name in route will be translated to aLongActionNameAction

you can access admin module with /admin/controller/action/params

$di->set(
           $service,
           function () use ($config) {
               $router = new Router(false);
               $router->removeExtraSlashes(true);

               $router->setDefaultModule("customer");
               $router->setDefaultNamespace("LicMan\\Modules\\Customer\\Controllers\\");

               $router->add( "/:controller/([a-zA-Z\-]+)/:params", [
                       "controller" => 1,
                       "action" => 2,
                       "params" => 3
                   ])->convert("action", function ($action) {
                           return Text::camelize($action);
                       });

               $router->add("/:controller/([a-zA-Z\-]+)", [
                       "controller" => 1,
                       "action" => 2
                   ])->convert("action", function ($action) {
                           return Text::camelize($action);
                       });

               $router->add("/:controller", [
                       "controller" => 1,
                       "action" => "index"
                   ]);

               $admin = new Router\Group([
                   "module"        =>  "admin",
                   "controller"    =>  "index",
                   "namespace"     =>  "LicMan\\Modules\\Admin\\Controllers\\"
               ]);

               $admin->setPrefix("/admin");
               $admin->add( "/:controller/:action/:params", [
                       "controller" => 1,
                       "action" => 2,
                       "params" => 3
                   ])->convert("action", function ($action) {
                           return Text::camelize($action);
                       });

               $admin->add("/:controller/:action", [
                       "controller" => 1,
                       "action" => 2
                   ])->convert("action", function ($action) {
                           return Text::camelize($action);
                       });

               $admin->add("/:controller", ["controller" => 1,
                       "action" => "index"
                   ]);

               $admin->add("", array("controller" => "index", "action" => "index"));

               $router->mount($admin);

               return $router;
           }
       );

My Custom Routes are

$defaultModule = ucfirst($registry->defaultModule);
        $router = new \Phalcon\Mvc\Router\Annotations(true);
        $router->removeExtraSlashes(true);
        $router->setDefaultModule(ucfirst($defaultModule));
        $router->setDefaultNamespace(ucfirst($defaultModule).'\Controllers');
        $router->setDefaultController("index");
        $router->setDefaultAction("index");

        $router->add('/:module/:controller/:action/:params', [
            'module' => 1,
            'controller' => 2,
            'action' => 3,
            'params'=>4
        ]);

        $router->add('/:module/:controller/:action', [
            'module' => 1,
            'controller' => 2,
            'action' => 3
        ]);

        $router->add('/:controller/:action', [
            'controller' => 1,
            'action' => 2
        ]);
        $router->add('/:controller', [
            'controller' => 1
        ]);
        $router->add('/', [
                'module' => ucfirst($defaultModule),
                'controller' => 'index',
                'action' => 'index'
                ]);

        $router->notFound(array(
            'module' => $defaultModule,
            'namespace' => ucfirst($defaultModule).'\Controllers',
            'controller' => 'Error',
            'action' => 'show404'
        ));

when I make a request to Module2/Controller/Action redirect to no page found After analyse try to print the eventmanager and saw namespace of default module i.e Module1/Controllers

How to resolve it ??