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 help in Micro Collection Route

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

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

    $PCollection
        // VERSION NUMBER SHOULD BE FIRST URL PARAMETER, ALWAYS
        ->setPrefix('/v1')
        // Must be a string in order to support lazy loading
        ->setHandler('\NAppRest\App\Controllers\AppController')
        ->setLazy(true);

    /**
    *
    *   Select Project Database
    *
    */

    $PCollection->get('/', 'getIndex');
    $PCollection->post('/', 'getIndex');

    // project

    $PCollection->get('/{projectName:'.($Allow_Regex).'}', 'getProjectInfo');
    $PCollection->post('/{projectName:'.($Allow_Regex).'}', 'getProjectInfo');

    $PCollection->get('/{projectName:'.($Allow_Regex).'}/table', 'getTableDirect');
    ...

i want to set one time this : "/{projectName:'.($Allow_Regex).'}"

for all collection !

is that possible ?

mean :

<?php 
    $PCollection->get('/', 'getProjectInfo');
    $PCollection->post('/', 'getProjectInfo');

    $PCollection->get('/table', 'getTableDirect');

i dont want repeat that every time !

any idea ?

thanks !



8.4k

if you mean mapping a route for all http requests:

$PCollection->map('/', 'getProjectInfo');

can you explain me the map work

because i dont understand how really that work
thanks



8.4k

Phalcon\Mvc\Micro\Collection::map() Map allows you to attach the same endpoint to more than one HTTP method

<?php

$PCollection->get('/', 'getIndex');

$PCollection->post('/', 'getIndex');

$PCollection->get('/{projectName:'.($Allow_Regex).'}', 'getProjectInfo');

$PCollection->post('/{projectName:'.($Allow_Regex).'}', 'getProjectInfo');

$PCollection->get('/{projectName:'.($Allow_Regex).'}/table', 'getTableDirect');

//or

$PCollection->map('/', 'getIndex'); // GET, POST, PUT, HEAD, OPTIONS, DELETE, PATCH, etc

$PCollection->map('/{projectName:'.($Allow_Regex).'}', 'getProjectInfo');

$PCollection->map('/{projectName:'.($Allow_Regex).'}/table', 'getTableDirect');

//or

$PCollection->mapVia('/', 'getIndex', ['GET', 'POST']); // GET, POST

$PCollection->mapVia('/{projectName:'.($Allow_Regex).'}', 'getProjectInfo', ['GET', 'POST']);

$PCollection->mapVia('/{projectName:'.($Allow_Regex).'}/table', 'getTableDirect', ['GET']);

check the documentation

V3

V4

oh that interesting !

also is the are any way to not repeate the '/{projectName:'.($Allow_Regex).'}' every time ?



8.4k
Accepted
answer

you can use Phalcon\Mvc\Micro\Collection::setPrefix() as you did in your original code

$PCollection
        // VERSION NUMBER SHOULD BE FIRST URL PARAMETER, ALWAYS
        ->setPrefix('/v1')

Okey ! i understand now

that work also ! i forget to try that !

thanks so much

also i have anotehr question

for exemple if i have


$PCollection
        // VERSION NUMBER SHOULD BE FIRST URL PARAMETER, ALWAYS
        ->setPrefix('/v1/{projectName:'.($Allow_Regex).'}')
        // Must be a string in order to support lazy loading
        ->setHandler('\NAppRest\App\Controllers\AppController')
        ->setLazy(true);

how i can use that projectName into AppController :: __construct

Thanks

i found it

$this->router->getParams()['projectName']

but is that correct and easy for use or the are another easy and short way ?

thanks



8.4k

you can also use Phalcon\Mvc\Micro::map()but the downside here is it dosen't have a mapVia() method:

// $app is Phalcon\Mvc\Micro

$app->get(
    '/v1/{projectName:'.($Allow_Regex).'}',
    function ($projectName) {
        $appController = new \NAppRest\App\Controllers\AppController;
        $appController->getProjectInfo($projectName);
    }
);

$app->get(
    '/v1/{projectName:'.($Allow_Regex).'}/table',
    function ($projectName) {
        $appController = new \NAppRest\App\Controllers\AppController;
        $appController->getTableDirect($projectName);
    }
);

yeah , but i not short :)

i use setPrefix for projectName

and save it in controller by using __construct

and using get params from router projectName

just an problem when i try to use

$PCollection->map('/table/{table:'.($Allow_Regex).'}', 'getTable');

this problem i need to add this two params in function

function getTable($project,$table) 

and i dont want to do that i want to use just table in params

like :

function getTable($table)


8.4k

sure go with whats working and feels right

but dont forget never ever trust user input

Okey !

yeah dont worry ! i dont miss any input without checking it :p

thanks for your help :)