0

Hello guys I am new to Zendframework2.

I have the following line in view\partial\paginator.phtml

<a href="<?php echo $this->url($this->route);?>?page=<?php echo $page; ?>?id=<?php echo $this->id?>">

In browser it looks like http://new_project.localhost/districts?page=2?id=4

new_project is the name of my project, districts is route of controller and in id=4, 4 is id which I want to access in controller.

I tried:

$_GET['id'];                             

$this->params()->fromRoute('id');   //also

$this->params()->fromQuery('id');   //also

But non of these works.

How I will access this id in controller?

Skylink
  • 83
  • 1
  • 9

3 Answers3

0

I solve it, just by putting condition like below:

if (isset($_GET['id']))
{ 
    // perform operation here.      
} 
Skylink
  • 83
  • 1
  • 9
0

Don't build your A element tag href yourself but use the framework like you should.

Url view helper:

// $this->url($routeName, $routeParams, $options, $reuseMatchedParams = false)
$this->url('my/route', ['id' => $object->getId()], ['query' => ['page' => $page]])

Controller plugin:

$this->params('id') // will take ID either from routeParameters or queryParameters.
$this->params()->fromRoute('id') // will take the id from the routeParameters
$this->params()->fromQuery('page') // will take the page from the route query

So when you've got a route with a routeParameter id but your query also has the id within it's query its best practice to keep on using the $this->params()->fromRoute('id') or $this->params()->fromQuery('id') so you always know where the value comes from instead of using $this->params('id').

As with the params plugin you can also define default values on the second argument, like: $this->params()->fromQuery('page', 1). So page will always be 1 if the user did not provide one. The default of the second argument is null. Hope this helps and makes you stop using $_GET['bla']

Kwido
  • 1,382
  • 8
  • 21
0

I think your url should look like this:

http://new_project.localhost/districts?page=2&id=4
                                             ^
                                         not ?

Because of the mistake you made the parser probably doesn't parse the id in your url correctly meaning you cannot access it like you tried.

It would be better to use the url view helper for rendering the query parameters in the url then you wouldn't make such a mistake. You can check this post on StackOverflow for reference on how to add query parameters to a url using the url view helper. In your case it would look something like this:

<?php 
    $href = $this->url($this->route, [], ['query' => [
        'page' => $page,
        'id' => $id, 
    ]]);
?>
<a href="<?php echo $href ?>">

Now you get the correct url:

http://new_project.localhost/districts?page=2&id=4

Meaning the parser also manages to parse the id correctly. You should be able to access it now like you tried with:

$this->params()->fromQuery('id'); //returns your id from the url
Community
  • 1
  • 1
Wilt
  • 41,477
  • 12
  • 152
  • 203