REST: Routes with default values #110

Closed
opened 2017-11-30 01:20:10 +00:00 by jamie · 6 comments
jamie commented 2017-11-30 01:20:10 +00:00 (Migrated from git.hazaar.io)

I would like to be able to set default values for methods when using the @route annotation. This would be useful to have a single method handle multiple routes and use different values for parameters.

Take the following for example:

/**
 * @route('/listofitems', method=['GET'])
 * @route('/item', method=['GET'], defaults={'single': true})
 */
public function getitems($single = false){

    $items = get_all_the_items();

    if($single === true)
        return array_shift($items);

    return $items;

}

This would have the $single parameter default to false and so when calling /listofitems the value of false is used. However if we call /item we are executing the same method except that the value of $single is now true.

I would like to be able to set default values for methods when using the @route annotation. This would be useful to have a single method handle multiple routes and use different values for parameters. Take the following for example: ```php /** * @route('/listofitems', method=['GET']) * @route('/item', method=['GET'], defaults={'single': true}) */ public function getitems($single = false){ $items = get_all_the_items(); if($single === true) return array_shift($items); return $items; } ``` This would have the `$single` parameter default to `false` and so when calling */listofitems* the value of false is used. However if we call */item* we are executing the same method except that the value of `$single` is now true.
jamie commented 2017-11-30 01:21:09 +00:00 (Migrated from git.hazaar.io)

changed the description

changed the description
jamie commented 2017-11-30 05:39:23 +00:00 (Migrated from git.hazaar.io)
created branch [`110-rest-routes-with-default-values`](https://git.hazaarlabs.com/hazaar/hazaar-mvc/compare/master...110-rest-routes-with-default-values)
jamie commented 2017-11-30 05:39:25 +00:00 (Migrated from git.hazaar.io)

mentioned in merge request !27

mentioned in merge request !27
jamie commented 2017-11-30 06:00:19 +00:00 (Migrated from git.hazaar.io)

closed via commit 4fd9a05d97

closed via commit 4fd9a05d97de3310740e4255575fdf8acb2a9a0d
jamie commented 2017-11-30 06:00:19 +00:00 (Migrated from git.hazaar.io)

closed via merge request !27

closed via merge request !27
jamie commented 2018-06-29 02:24:26 +00:00 (Migrated from git.hazaar.io)

changed milestone to %4

changed milestone to %4
jamie self-assigned this 2025-09-04 01:15:46 +00:00
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference: hazaar/framework#110
No description provided.