0

I need to create a custom url in my rest api. I am using fos rest bundle.

custom URL is like:

http://myapi.com/api/v1/public/users/confirm?cd=<some_code>.json

I have tried:

@GET("/users/confirm?cd={cd}")

and when I run:

php /app/console route:debug

I am getting this:

...
....
get_confirm GET ANY ANY  /api/v1/public/users/confirm?cd={cd}.{_format}
...
...

but in my test when I try to hit this URL I get:

No route found for &quot;GET /api/v1/public/users/confirm&quot; (404 Not Found)

can anyone help me in this. How to form such URLs.

My Controller Action Code:

/*
 * @GET("/users/confirm?cd={cd}")
 */
public function getConfirmAction($cd) {

    //Some code for user confirmation

    return return View::create(array('successmessage'=>'Your account has been verified successfully. Please login.', Codes::HTTP_OK);
}

PHPUnitTest Code:

public function testGetConfirmThrowsInvalidArgumentException() {
    $this->client->request(
                'GET', '/api/v1/public/users/confirm?cd=abcd123.json'
        );

        $response = $this->client->getResponse();

        print_r($response->getContent());
        exit(__METHOD__);
}
2
  • Try removing the GET parameters from your route. You can still attach them regardless, but you don't need them for routing. Commented Feb 18, 2015 at 9:04
  • @Dragony I have tried that but no help :( Commented Feb 18, 2015 at 9:26

2 Answers 2

2

Agreed with @john comment: you dont need to add the queryparameters to your route definition

I guess basically you are looking to have a parameter always supplied with the URL. If this is your requirement then FOSRestBundle has cool feature called as param fetcher. With it you can define your query string parameters with annotations, allow them nullable or not, set default values, define requirements.

for your case if you are expecting cd to be a number then you can have annotations as

@QueryParam(name="cd", nullable=true, requirements="\d+")

see below example for sample code

/*
* function desc
* @QueryParam(name="cd", nullable=true, requirements="\d+")
* @param ParamFetcher $paramFetcher
*/
public function getConfirmActionAction(ParamFetcher $paramFetcher)
{
   //access the parameter here
    foreach ($paramFetcher->all() as $name => $value) {
        echo $name."==>". $value;
    }

}
Sign up to request clarification or add additional context in comments.

3 Comments

But why loop for just reading a single parameter? I have tried this: $paramFetcher->get('cd') but not working
The loop was for demonstration purpose only that's not the only way to fetch params. $paramFetcher->get('cd') should work, have a look athe ParamFetcher.php class it does uses symfony's Request Obj to fetch params. github.com/FriendsOfSymfony/FOSRestBundle/blob/master/Request/…
finally got it you need to use w+: * @QueryParam(name="cd", nullable=true, requirements="\w+") cause my token is a big alphanumeric string
1

you dont need to add the queryparameters to your route definition

also they would come after the full url, e.g after ".json"

i.e:

/api/v1/public/users/confirm.json?cd=ejwffw

so i have not experience with annotation routing definitions, but it should rather be sth like that:

@GET("/users/confirm.{_format}")

and in your action you have access to your param through the request

sth like:

$request=$this->getRequest();
$code = $request->get('cd') ? $request->get('cd') : false;

2 Comments

Nope still getting same result: No route found for &quot;GET /api/v1/public/users/confirm.json/&quot; (404 Not Found)
Okay Finally its worked thanks :D. I was hitting the wrong url like this after your suggestion: /api/v1/public/users/confirm.json/?cd=<some> now its working :D

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.