Ali Rasheed
Ali Rasheed

Reputation: 2817

How to extract special characters from route in Zend framework 3

I'm sending a url that has special characters in them.

/contacts?advanceSearch=true&advanceSearchType=rating&advanceSearchValue=A1A+

As you see the variable value of advanceSearchValue is A1A+

But when I retrieve this in controller

$this->params()->fromQuery("advanceSearchValue");

it shows me A1A. It adds space instead of +

This is my route config.

"contacts"                   => [
            "type"                   => "segment",
            "options"                => [
                "route"              => "/contacts[/:action[/:id]]",

                "defaults"           => [
                    "controller"     => Controller\ContactController::class,
                    "action"         => "index",
                ],
            ],
        ],

Upvotes: 0

Views: 208

Answers (2)

Gautam Rai
Gautam Rai

Reputation: 2505

You need to encode your request-url : You can encode it by php Or Javascript -

In javascript :

var url= "/contacts?advanceSearch=true&advanceSearchType=rating&advanceSearchValue=A1A+";
 url= encodeURI(uri);

In php :

$url = urlencode('/contacts?advanceSearch=true&advanceSearchType=rating&advanceSearchValue=A1A+');

Then use this encoded Url in your ajax.

Upvotes: 0

JGNI
JGNI

Reputation: 4013

This is because + has a special meaning in a URL and Zend knows this and correctly replaces it with a space.

To get a + character into the parsed data you need to URL escape it. This gives the value %2B.

So your full URL should be

/contacts?advanceSearch=true&advanceSearchType=rating&advanceSearchValue=A1A%2B

By the way, what is producing this URL, a web browser should be automatically converting the + character before sending it to the web server?

Upvotes: 1

Related Questions