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 set a custom router

Hi, I want to set my own site url in my site-url. Every 3rd parameter is different ie varying and i want to create one single common action which applies to specific controller. For Example www.example.com/dashboard/apache www.example.com/dashboard/php www.example.com/dashboard/jquery like above. I want to create only one action for above urls



10.0k
edited Sep '14

You can use forward or redirection(with persistent, i'am not sure)



27.6k

Give me an example same for



10.0k
edited Sep '14

e.g

dashboard
public action commonAction(){
    $this->view->setvar("common", "Is comon");
}

public action phpAction(){
    $this->dispatcher->forward(array(
        "controller" => "dashboard",
        "action" => "common"
    ));
}

etc..

I'm not sure, i don't tried.



27.6k

Hi i m beginner not getting how it can be done. It can be done using phalcon router but no idea about it pls help me someone



10.0k

https://docs.phalcon.io/en/latest/reference/routing.html#groups-of-routes

$blog->add('/dashboard/php', array(
    'controller' => 'dashboard',
    'action' => 'common'
));

Last route is more priority.



7.9k

take a look my example below

$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;
            }
        );