0

Im having problems with multiple routs, some of them not working.

/**
 * 
 * @url GET /fetch
 * @url GET /fetch/lazyload/:lazy
 * @url GET /fetch/:id/
 * @url GET /fetch/:id/lazyload/:lazy
 * @url GET /fetch/start/:offset/limit/:limit
 * @url GET /fetch/start/:offset/limit/:limit/lazyload/:lazy
 * @url GET /fetch/start/:offset/limit/:limit/sort/:sort
 * @url GET /fetch/start/:offset/limit/:limit/sort/:sort/lazyload/:lazy
 * @url GET /fetch/start/:offset/limit/:limit/sort/:sort/orderby/:order
 * @url GET /fetch/start/:offset/limit/:limit/sort/:sort/orderby/:order/lazyload/:lazy
 */
protected function fetch($id = null, $offset = 0, $limit = 25, $lazy = false, $sort = 'asc', $order = null){
    //override
    throw new RestException(501);
}

For instace

@url GET /fetch/start/:offset/limit/:limit

will not work if url /fetch/start/1/limit/2 but it will work if /fetch/start/1/2

and /fetch/:id/ will override /fetch/lazyload/:lazy

so /fetch/lazyload/true will not work, the recognized pattern will return /fetch:id => lazyload

am I missing something, or this kind of mappings are not possible?

Thank you.

Dr Casper Black
  • 7,350
  • 1
  • 26
  • 33

1 Answers1

1

Ambiguity is playing its part here! read this thread to understand more

In short, parameters in the url are greedy so we need to make sure they come after non greedy urls

For example lets talk about two urls

  1. simple\:param
  2. simple\name

if we keep the urls in the same order as above and call simple\name, only api method that is mapped to simple\:param will receive it setting param as "name". We can fix this by simply changing the order to

  1. simple\name
  2. simple\:param

Similarly you can fix your api with the following

/**
 *
 * @url GET /fetch/start/:offset/limit/:limit/sort/:sort/orderby/:order/lazyload/:lazy
 * @url GET /fetch/start/:offset/limit/:limit/sort/:sort/orderby/:order
 * @url GET /fetch/start/:offset/limit/:limit/sort/:sort/lazyload/:lazy
 * @url GET /fetch/start/:offset/limit/:limit/sort/:sort
 * @url GET /fetch/start/:offset/limit/:limit/lazyload/:lazy
 * @url GET /fetch/start/:offset/limit/:limit
 * @url GET /fetch/lazyload/:lazy
 * @url GET /fetch/:id/lazyload/:lazy
 * @url GET /fetch/:id/
 * @url GET /fetch
 */
protected function fetch($id = null, $offset = 0, $limit = 25, $lazy = false, $sort = 'asc', $order = null)
{
    //override
    throw new RestException(501);
}

HTH

Arul Kumaran
  • 983
  • 7
  • 23