Not entirely sure how (controller) forwarding works in Symfony

Viewed 594

I tried to move a bit of code with as function to add pagination to my page, to a certain controller that now looks like this:

<?php

namespace AppBundle\Controller;

use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\JsonResponse;

use AppBundle\Entity\Movie;

class PaginationController extends Controller
{
    public function paginationAction(Request $request, $pageNumber)
    {
        $recordsPerPage = 5;
        $page = $pageNumber;

        // Positioning
        $start = ($page > 1) ? ($page * $recordsPerPage) - $recordsPerPage : 0;

        // Query
        //Records:
        //$movies = $this->getDoctrine()->getEntityManager()->getRepository('AppBundle:Movie')->FindAll();
        $repository = $this->getDoctrine()->getRepository('AppBundle:Movie');
        //Data to count:
        $qb = $repository->createQueryBuilder('m');
        $query = $qb
        ->getQuery();
        $totalAmountRows = count($query->getResult());

        //For display:
        $query = $qb
        ->setFirstResult($start)
        ->setMaxResults($recordsPerPage)
        ->getQuery();
        $movies = $query->getResult();

        //Pagination: 
        $filterVariables = "";
        //Always round up number (whole pages):
        $totalPages = ceil($totalAmountRows / $recordsPerPage);

        return array(
            'movies' => $movies,
            'filtervariables' => $filterVariables,
            'totalPages' => $totalPages,
            'totalAmountRows' => $totalAmountRows,
            'pageNumber' => $pageNumber
        );
    }
}

And the controller that should receive the values looks like this:

<?php

namespace AppBundle\Controller;

use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\JsonResponse;

use AppBundle\Entity\Movie;

class MovieDisplayController extends Controller
{

    public function showAction(Request $request, $pageNumber)
    {
        $this->forward('AppBundle:Pagination:pagination', array(
            'pageNumber' => $pageNumber
        ));

        return $this->render('movies/index.html.twig', array(
            'movies' => $movies,
            'filtervariables' => $filterVariables,
            'page' => $page,
            'totalPages' => $totalPages,
            'totalAmountRows' => $totalAmountRows,
            'pageNumber' => $pageNumber
        ));
    }
}

But when I look at the page I see: Notice: Undefined variable: movies. How should I change my code so that it knows that I am trying to give the $movies variable from one controller to the other?

Edit: In order for me to entirely understand the answers below, I want to add the following to my question (thanks to @Rvanlaak):

How should it look like when I want to handle the pagination in my MovieRepository?

2 Answers
Related