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

i need a better way to use prefix /router/

I do not want every time to repeat the router prefix and I want to do it more cleanly So I need another method like this example:


<?php 
$ConsoleRouter = new \Phalcon\Mvc\Micro\Collection();

// config global
$NAPP_regex = $this->getShared('config')->project->regex;

$ConsoleRouter
        ->setPrefix('/console')
        ->setHandler('\NAppRest\Console\Controllers\ConsoleController')
        ->setLazy(true);

// Auth Keys
$ConsoleRouter->setPrefix('/console/apps/keys');

    $ConsoleRouter->get   ("/{id:$NAPP_regex}", 'getProjectKeys');
    $ConsoleRouter->post  ("/{id:$NAPP_regex}", 'getProjectKeys');

    $ConsoleRouter->get   ("/{id:$NAPP_regex}/create", 'createProjectKey');
    $ConsoleRouter->post  ("/{id:$NAPP_regex}/create", 'createProjectKey');

    // Tables
$ConsoleRouter->setPrefix('/console/apps/tables');

    $ConsoleRouter->get   ("/{id:$NAPP_regex}", 'getProjectTables');
    $ConsoleRouter->post  ("/{id:$NAPP_regex}", 'getProjectTables');

return $ConsoleRouter;

this exemple not work :)

i just want to try it

that why i need another way to do it

edited Jan '20

Maybe:

$ConsoleRouter
->setPrefix('/console/apps')
->setHandler('\NAppRest\Console\Controllers\ConsoleController')
->setLazy(true);
$ConsoleRouter->get("/keys/{id:$NAPP_regex}", 'getProjectKeys');
$ConsoleRouter->post("/keys/{id:$NAPP_regex}", 'createProjectKey');
$ConsoleRouter->get("/tables/{id:$NAPP_regex}", 'getProjectTables');

For me you should rethink some things. For example GET is ok for read data, POST is for creating, so I didn't understand why you have every operation twice. Maybe you should think about two controllers - first for 'keys' and second for 'tables'. That will be better for performance and you have clear code.

I Want to give to user , all permission to use any method he/she want to do that why i put those get/post ...

just for now i just want a structure to not reapeat the keys , table name ..?

for exemple: (if it exists)

$consoleRouter : keys > 
        -> get("{id:$NAPP_regex}", getProjectKeys)
        -> post("{id:$NAPP_regex}", createProjectKey)
$consoleRouter : tables > 
        -> get("{id:$NAPP_regex}", getProjectKeys)
        -> post("{id:$NAPP_regex}", createProjectKey)
i want do something like this at the minmum reapeat of words

p if that exists ^_^ !

Thanks