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

Fluent Route with Phalcon

Hi Guys,

I made this new functionality in services.php, but I think that is not better file to this, Could you help about better file or i just make a new file(name?)?


//services.php

Routing::Init()
        ->Controller(Controllers::INDEX)
            ->Action(Routes::INDEX, Actions::INDEX)
            ->Action(Routes::E404, Actions::E404)
            ->Action(Routes::E505, Actions::E505)
            ->Action(Routes::REFRESH, Actions::REFRESH)
        ->Controller(Controllers::USER)
            ->Action(Routes::SIGN_IN, Actions::SIGN_IN)
            ->Action(Routes::REGISTER, Actions::REGISTER)
            ->Action(Routes::LOGON, Actions::LOGON)
        ->Controller(Controllers::ADMIN)
            ->Action(Routes::USERS, Actions::USERS)
        ->Controller(Controllers::PRODUCT)
            ->Action(Routes::PRODUCT_GET, Actions::GET)
        ->Build();

the values of defines is :


// Actions.php
abstract class Actions {
    const INDEX         = 'index';
    const E404          = 'e404';
    const E505          = 'e505';
    const REFRESH       = 'refresh';
    const SIGN_IN        = 'signin';
    const REGISTER      = 'register';
    const LOGON         = 'logon';
    const GET           = 'get';
}

// Controllers.php
abstract class Controllers {
    const INDEX = 'index';
    const USER = 'user';
    const ADMIN = 'admin';
    const PRODUCT = 'product';
}

// Routes.php
define ('index', '');
define ('e404' , Actions::E404);
define ('e505' , Actions::E505);
define ('refresh', Actions::REFRESH);
define ('index_user', Controllers::USER);
define ('sign_in', Controllers::USER.'/'.Actions::SIGN_IN);
define ('register', Controllers::USER.'/'.Actions::REGISTER);
define ('alter_user', Controllers::USER.'/'.Actions::ALTER);
define ('logon', Controllers::USER.'/'.Actions::LOGON);
define ('orders_user', Controllers::USER.'/'.Actions::ORDERS);
define ('logoff_user', Controllers::USER.'/'.Actions::LOGOFF);
define ('remove', Controllers::USER.'/'.Actions::REMOVE);
define ('index_admin', Controllers::ADMIN);
define ('users', Controllers::ADMIN.'/'.Actions::USERS);
define ('user', Controllers::ADMIN.'/'.Actions::USER);
define ('categories', Controllers::ADMIN.'/'.Actions::USERS);
define ('category', Controllers::ADMIN.'/'.Actions::CATEGORY);
define ('orders_admin', Controllers::ADMIN.'/'.Actions::ORDERS);
define ('order_admin', Controllers::ADMIN.'/'.Actions::ORDER);
define ('logoff_admin', Controllers::ADMIN.'/'.Actions::LOGOFF);
define ('product_get', Controllers::PRODUCT.'/'.Actions::GET.'/{name}.json');
abstract class Routes {
    const INDEX = index;
    const E404 = e404;
    const E505 = e505;
    const REFRESH = refresh;
    const SIGN_IN = sign_in;
    const REGISTER = register;
    const LOGON = logon;
    const PRODUCT_GET = product_get;
}

Phalcon does a lot of routing by default. So for example, if you have a controller called "AdminController", with an action method called "usersAction", then Phalcon will automaticall handle the route: /admin/users/ - you don't need to add a custom route for that.

If you do need to do custom routes, I'm not sure what advantage you're getting from abstracting out the route values into constants & object constants. You're not really saving any typing and in my opinion it just makes things overly complicated.



951

I don't really like to use types of routes in my app, because i made myself for my app has a pattern, and I don't want to work with text in my code, I want that all text be isolated using defines.