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 make a route accept only integer parameters

i want to only accept integers in the parameters i cant seen to get it to work my route

<?php

$router = $di->getRouter();

$router->add('/import/{([0-9]{6,8}+)}', [ 'controller' => 'import', 'action' => 'index', 'query' => 1
] ); $router->handle($_SERVER['REQUEST_URI']);

the controller

<?php

declare(strict_types=1);

class Importcontroller extends IndexController { public function indexAction() { try { $query = $this->request->getQuery("query"); var_dump($query);

    } catch (Exception $e) {
        echo $e->getMessage();
    }
}

}

You dont need to do

$query = $this->request->getQuery("query");

because data are passed in parametres

so you do : public function indexAction(int $query ){}

also you can do this :

// Matches '/admin/posts/edit/100'
$router->add(
    '/admin/:controller/:action/:int',
    [
        'controller' => 1,
        'action'     => 2,
        'id'         => 3,
    ]
);

Source: https://docs.phalcon.io/3.4/fr-fr/routing#usage-examples

edited Jun '20

here you go

$router->add("/import/{number:[0-9]+}", [
  'controller' => 'import',
  'action'     => 'index'
]);

then grab it in the controller :


$number = $this->dispatcher->getParam("number");