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

Concat Router Parameters

Hi,

I want to concat router parameters but i couldnt find how to.

My URLs like that:

/settings/users

/settings/users/add

/settings/users/edit/47

/settings/users/delete/47

My Controller like that:

class Settings extends Controller
{
    public function users() {}
    public function usersAdd() {}
    public function usersEdit() {}
    public function usersDelete() {}
}

My router like that:

$router->add('/settings/users/:action', array(
    'controller' => 'settings',
    'action' => 'users' // Should be with action name with ucfirst like usersAdd
));

I want to create once route for multiple action. For ex: users + add so usersAdd

Just change action to 1, but it won't work like this anyway, you would need to use some logic in beforeExecuteRoute anyway. Also it's really bad having /add /edit/ delete routes. You should use POST, PUT, DELETE queries and just have only /settings/users



3.4k
edited Apr '17

Save the future headache from coming and set your routes up properly.

https://docs.phalcon.io/en/3.0.2/reference/routing.html#http-method-restrictions

$objRouter->add(
    "/settings/users",
    array(
        "controller" => "settings",
        "action"  => "users"
    )
)->via("GET");

//Add a user
$objRouter->add(
    "/settings/users",
    array(
        "controller" => "settings",
        "action"  => "usersAdd"
    )
)->via("POST");

//Edit a user
$objRouter->add(
    "/settings/users/([0-9]+)",
    array(
        "controller" => "settings",
        "action"  => "usersEdit",
        "userid" => 1
    )
)->via("PUT");

//Remove a user
$objRouter->add(
    "/settings/users/([0-9]+)",
    array(
        "controller" => "settings",
        "action"  => "usersDelete",
        "userid" => 1
    )
)->via("DELETE");