Pregunta

Is there a helper function in phalcon (volt) to create Links to routes with GET-Parameters ? I have pagination-links on which I want to add ?cat=category and ?year=year depending on whether they are set or not.

<a href="{{ url.getBaseUri() }}tags/{{ tagname.tag }}">First</a>
<a href="{{ url.getBaseUri() }}tags/{{ tagname.tag }}?page=<?= $page->before; ?>">Previous</a>
<a href="{{ url.getBaseUri() }}tags/{{ tagname.tag }}?page=<?= $page->next; ?>">Next</a>
<a href="{{ url.getBaseUri() }}tags/{{ tagname.tag }}?page=<?= $page->last; ?>">Last</a>

so that

http://site.tld/tags/xyz?page=2

would become:

http://site.tld/tags/xyz?cat=a&year=b&page=2

or this, if cat is not set or null:

http://site.tld/tags/xyz?year=b&page=2

edit

this way it seems to work:

<a href="{{ pagingurl ~ page.first }}">First</a>
<a href="{{ pagingurl ~ page.before }}">Previous</a>
<a href="{{ pagingurl ~ page.next }}">Next</a>
<a href="{{ pagingurl ~ page.last }}">Last</a>

the rest happens in the controller

¿Fue útil?

Solución

IMO it's easier to do that in the controller than using volt. First, generate the base URL for your pagination links with the URL Service:

$pagingUrl = $this->url->get('tags/' . $tagname->tag);

Now you can get 'cat' and 'year' with something like $this->request->getPost('cat'); to check if it's set and append it to $pagingUrl as GET parameters. Leave a '&page=' at end of the $pagingUrl to easily append the page number later.

Set $page and $pagingUrl as variables for your view so you can easily access it from volt:

$this->view->setVar('page', $page);
$this->view->setVar('pagingUrl', $pagingUrl);

Finally in the view you could use something like that:

{{ link_to(pagingUrl ~ '1', 'First') }}
{{ link_to(pagingUrl ~ page.before, 'Previous') }}
{{ link_to(pagingUrl ~ page.next, 'Next') }}
{{ link_to(pagingUrl ~ page.last, 'Last') }}

EDIT

The solutions above seems hackish because Phalcon designers aimed to work more with clean URLs than explicit GET parameters. If you were passing your parameters this way, your TagController could have an action that supports pagination like this:

class TagController
{
  ...
  public function ListAction($page = 1, $category = 'default-cat', $year = 1997)
  {
    ...

Working that way you can easily create links like these:

  • tags/list
  • tags/list/2/stuff
  • tags/list/9/stuff/2014
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top