Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add query parameter to routes in Lumen?

Tags:

lumen

I am trying to know how to add query parameters to routes in Lumen

this is an example of a route I created

$app->get('/product/{apikey}','ProductController@getProduct');

This works when I use

http://api.lumenbased.com/product/10920918

but I would like to use it like this

http://api.lumenbased.com/product/?apikey=10920918

I tried this

$app->get('/product/?apikey={apikey}','ProductController@getProduct');

But this gives me MethodNotAllowedHttpException

I would like to know how to write routes with query parameters in Lumen ?

like image 396
Rishabh Avatar asked Jul 31 '16 03:07

Rishabh


People also ask

How do I add parameters to a URL query?

Query parameters are a defined set of parameters attached to the end of a url. They are extensions of the URL that are used to help define specific content or actions based on the data being passed. To append query params to the end of a URL, a '? ' Is added followed immediately by a query parameter.

Can I pass URL as query parameter?

Yes, that's what you should be doing. encodeURIComponent is the correct way to encode a text value for putting in part of a query string. but when it is decoded at the server, the parameters of url are interpreted as seperate parameters and not as part of the single url parameter. Then the server is very broken indeed.

What is path and query parameter?

Path parameters are variable parts of a URL path. They are typically used to point to a specific resource within a collection, such as a user identified by ID. A URL can have several path parameters, each denoted with curly braces { } .

What are query and path parameters in REST API?

The path parameter defines the resource location, while the query parameter defines sort, pagination, or filter operations. The user's input (the query) is passed as a variable in the query parameter, while each path parameter must be substituted with an actual value when the client makes an API call.


1 Answers

Just do:

$app->get('/product','ProductController@getProduct');

and use:

$request->get('apikey')

in the ProductController@getProduct function.

(That said, validating an API key is better done via middleware...)

like image 156
ceejayoz Avatar answered Sep 23 '22 23:09

ceejayoz