Search code examples
phpwildcardrestler

PHP Reslter 3 Wildcard and Custom Routing


I have this code on Restler and it returns 404 Not Found

class CRUDEntity {
  /**
   * @url GET /entity/{entity_id}/books/*
   */    
  function getBatch($entity_id) {
    var_dump(func_get_args());
  }
}

On the index page I have the following:

$r->addAPIClass('CRUDEntity','');

The idea is to get into the url /entity/1/books/10/12/13/14 but it returns the 404 error. Do you know how can I accomplish this?


Solution

  • Wildcard routes do not support dynamic parts yet! so you can do the following instead

    class CRUDEntity
    {
        /**
         * @param int $entity_id
         *
         * @url GET /entity/*
         */
        function getBatch($entity_id, $books = 'books')
        {
            if (!is_numeric($entity_id) || $books != 'books') {
                throw new RestException(404);
            }
            $dynamicArguments = func_get_args();
            array_shift($dynamicArguments);
            array_shift($dynamicArguments);
            var_dump($dynamicArguments);
        }
    }