* This article is part of the original Jobeet Tutorial, created by Fabien Potencier, for Symfony 1.4.
Today we will make the Category page like it is described in the second day’s requirements:

“The user sees a list of all the jobs from the category sorted by date and paginated with 20 jobs per page“

The Category Route

First, we need to add a route to define a pretty URL for the category page. Add it at the beginning of the routing file:

To get the slug of a category we need to add the getSlug() method to our category class:

The Category Link

Now, edit the index.html.twig template of the job controller to add the link to the category page:

In the template above we used category.morejobs, so let’s define it:

The more_jobs property will hold the number of active jobs for the category minus the number of jobs listed on the homepage. Now, in JobController, we need to set the more_jobs value for each category:

The countActiveJobs function has to be added to the JobRepository:

Now you should see the result in your browser:

Day-7 - category link

Category Controller Creation

It’s now time to create the Category controller. Create a new CategoryController.php file in your Controller directory:

We could use the doctrine:generate:crud command like we did for the job controller, but we won’t need 90% of the generated code, so we can just create a new controller from scratch.

Update the Database

We need to add a slug column for the category table and lifecycle callbacks for setting this column value:

Remove from the Category entity (src/Ibw/JobeetBundle/Entity/Category.php) the getSlug method we created earlier and run the doctrine command to update the Category entity class:

Now you should have the following added to Category.php:

Change the setSlugValue() function:

Now we have to drop the database and create it again with the new Category column and load the fixtures:

Category Page

We have now everything in place to create the showAction() method. Add the following code to the CategoryController.php file:

The last step is to create the show.html.twig template:

Including Other Twig Templates

Notice that we have copied and pasted the tag that create a list of jobs from the job index.html.twig template. That’s bad. When you need to reuse some portion of a template, you need to create a new twig template with that code and include it where you need. Create the list.html.twig file:

You can include a template by using the include function. Replace the HTML <table> code from both templates with the mentioned function:

List Pagination

At the moment of writing this, Symfony2 doesn’t provide any good pagination tools out of the box so to solve this problem we will use the old classic method. First, let’s add a page parameter to the IbwJobeetBundle_category route. The page parameter will have a default value of 1, so it will not be required:

Clear the cache after modifying the routing file:

The number of jobs on each page will be defined as a custom parameter in the app/config/config.yml file:

Change the JobRepository getActiveJobs method to include an $offset parameter to be used by doctrine when retrieving jobs:

Change the CategoryController showAction to the following:

Finally, let’s update the template

The result:

Day 7 - pagination
Creative Commons License
This work is licensed under a Creative Commons Attribution-ShareAlike 3.0 Unported License.