Skip to content

Add support to non numeric IDs like strings or uuid

Latest
Compare
Choose a tag to compare
@tunecino tunecino released this 23 Aug 16:14
729c52a

This change Introduces a new variable $linkAttributePattern to allow changing the default RegEX parsing the main resource id from url.

    /**
     * @var string The Regular Expressions Syntax used to parse the id of the main resource from url.
     * For example, in the following final rule, $linkAttributePattern is default to that `\d+` to parse $brand_id value:
     * 
     *     GET,HEAD v1/brands/<brand_id:\d+>/items/<IDs:\d[\d,]*>
     * 
     * While that works fine with digital IDs, in a system using a different format, like uuid for example,
     * you may use $linkAttributePattern to define different patterns. Something like this maybe:
     * 
     * [
     *       // Nested Rules Brand
     *      'class' => 'tunecino\nestedrest\UrlRule',
     *      'modelClass' => 'app\modules\v1\models\Brand',
     *      'modulePrefix' => 'v1',
     *      'resourceName' => 'v1/brands',
     *      'relations' => ['items'],
     *      'tokens' => [
     *          '{id}' => '<id:[a-f0-9]{8}\\-[a-f0-9]{4}\\-4[a-f0-9]{3}\\-(8|9|a|b)[a-f0-9]{3}\\-[a-f0-9]{12}>',
     *          '{IDs}' => '<IDs:([a-f0-9]{8}\\-[a-f0-9]{4}\\-4[a-f0-9]{3}\\-(8|9|a|b)[a-f0-9]{3}\\-[a-f0-9]{12}(?:,|$))*>',
     *      ],
     *      'linkAttributePattern' => '[a-f0-9]{8}\\-[a-f0-9]{4}\\-4[a-f0-9]{3}\\-(8|9|a|b)[a-f0-9]{3}\\-[a-f0-9]{12}',
     *  ],
     */
    public $linkAttributePattern = '\d+';