Search code examples
phpsymfonysymfony-3.3

Redirect to Same method in Same Controller in Symfony (Pagination links)


Well i have this Method:

/**
 * @Route("/articles/{category}/{id}/true/{page}", defaults={"page"=1}, name="articleAjax")
 */
public function getArticlesAjax($category, $id, $page)
{
}

Inside i want to create pagination link, like this:

    $html .= '<ul>';

    for ($i = 1; $i <= $totalPages; $i++) {
        $html .= '<li><a href="">' . $i . '</a></li>';
    }

    $html .= '</ul>';

But now, how to set href to same method and pass variables. Then in view file, just to render this variable $html Any ideas?


Solution

  • If you want to generate a url of a controller inside a controller use $this->generateUrl() in your exmaple you could do something like this:

    for ($i = 1; $i <= $totalPages; $i++) {
        $route = $this->generateUrl('articleAjax', [
            'category' => $category,
            'id' => $id,
            'page' => $i,
        ]);
    
        $html .= '<li><a href="'. $route. '">' . $i . '</a></li>';
    }
    

    But I would recomment to this in your twig template and there use the {{ path() }} function.