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

forward-slash in get parameter returns null

Hello,

I use the the folliwing code:


    /**
     * @Get(
     *     '/{sample}'
     * )
     * @param $sample
     * @return \Phalcon\Http\Response
     */
    public function singleAction($sample)
    {
        return ResponseUtil::createJSONResponse (
            HTTPStatusCodeEnumeration::OK,
            true,
            'yes, found',
            $sample
        );
    }
$sample looks like this:

$sample = ABC123456789/12 -> returns: ROUTE NOT FOUND

$sample = ABC123456789 -> returns: ABC123456789

What do I have to do to make the router ignore the forward-slash in the sample-parameter?

Url-Encoding the forward-slash with "%2F" is not working. It destroys the routing functionality.

Thank you for your help!



381
Accepted
answer

Sometimes you miss the forest for the trees...

The solutions is to just pass the number after the slash as a second argument.

    /**
     * @Get(
     *     '/{orderNumber}/{sampleNumber}'
     * )
     * @param $orderNumber
     * @param $sampleNumber
     * @return \Phalcon\Http\Response
     */
    public function singleAction($orderNumber, $sampleNumber)
    {
        return ResponseUtil::createJSONResponse(
            HTTPStatusCodeEnumeration::OK,
            true,
            'yes, found',
            $orderNumber . "/" . $sampleNumber
        );
    }