Symfony 2.8 Jobeet Day 15: Web Services

With the addition of feeds on Jobeet, job seekers can now be informed of new jobs in real-time.

On the other side of the fence, when you post a job, you will want to have the greatest exposure possible. If your job is syndicated on a lot of small websites, you will have a better chance to find the right person. That’s the power of the long tail. Affiliates will be able to publish the latest posted jobs on their websites thanks to the web services we will develop along this day.

Affiliates

As per day 2 requirements:

Story F7: An affiliate retrieves the current active job list

The Fixtures

Let’s create a new fixture file for the affiliates:

In the fixtures file, tokens are hardcoded to simplify the testing, but when an actual user applies for an account, the token will need to be generated:

You can now reload the data:

php app/console doctrine:fixtures:load

The Job Web Service

To create the job web service we will use another bundle, the FOSRestBundle.

Start by installing it with the following command:

composer require friendsofsymfony/rest-bundle

Enable the bundle by adding the following line in the app/AppKernel.php file of your project:

This bundle also needs a serializer. We will use the JMSSerializerBundle. Installing it is almost the same.

Download (ignore the error if you get one, that’s because we enabled the FOSRestBundle without having a serializer configured):

composer require jms/serializer-bundle

And enable:

Now, for our API, let’s create a new bundle to hold all the related code using the command:

php app/console generate:bundle --namespace=ApiBundle --no-interaction

Remove the DefaultController.php file created in src/ApiBundle/Controller and the src/ApiBundle/Resources/views/Default folder as we will create our own files as needed.

Create a new JobController.php file to hold the job related API methods:

In it, add a new action to list all the available jobs:

To make it work, we have to change the type of the new route added to app/config/routing.yml (when we created the new Api bundle) from annotation to rest:

Now, if you load the http://jobeet.local/app_dev.php/api/jobs.json URL, you will receive a list with all the available jobs in JSON format. You can also try the XML format: http://jobeet.local/app_dev.php/api/jobs.xml.

If you look closely to the result, you will see that everything from the database is returned, including jobs and affiliates from the category of a job and that’s too much. To limit the returned data we will use some methods from the JMSSerializerBundle we installed earlier.

Open the Job.php entity file and add the following annotations and new methods:

We are still missing something though: the affiliate must use his or hers token to get the jobs. So lets’a add a token request parameter for this and change the method to return only relevant active jobs:

For this we need a new getActiveJobsForAffiliate() method in the JobRepository class:

Now you can access the list of active jobs for one of our affiliates with the following link: http://jobeet.local/app_dev.php/api/jobs/sensio_labs.json.

Web Service Tests

To test the web service, create a new JobControllerTest.php file in the src/ApiBundle/Tests/Controller folder with the following content:

Run the tests with the following command:

phpunit -c app/ src/ApiBundle/Tests/Controller/JobControllerTest.php

The Affiliate Application Form

Now that the web service is ready to be used, let’s create the account creation form for affiliates. We will use the same method as we did when created the job CRUD:

php app/console generate:doctrine:crud --entity=AppBundle:Affiliate --format=annotation --with-write --no-interaction

This command generated the classic CRUD actions and their corresponding templates but we only need the newAction and the new.html.twig template. Just delete the others.

Now open the app/Resources/view/affiliate/new.html.twig template and edit it to the following:

Also, create a new wait.html.twig template:

We also need to edit the affiliate form type and remove some fields from it:

After an affiliate submits the form, he or she will be redirected to the new wait page. Add the waitAction in the AffiliateController.php file:

And change the success redirect from the newAction to:

Last, change the link in the footer to point to the affiliate form (app/Resources/view/base.html.twig):

If you test the form now you will get some validation errors visible in the web debug toolbar:

Notice the highlighted “2”

Clicking on the form icon you will see the actual errors:

We need to remove the validation from the token and createdAt affiliate fields as those will be updated by the Doctrine lifecycle callbacks. We will also add a new validator to ensure the affiliate has at least one category:

Now everything should work fine and the affiliates will be able to register to our site.

Tests

The last step is to write some functional tests for the new feature. Add the following code to the src/AppBundle/Tests/AffiliateControllerTest.php file:

To simplify the task, a new getProgrammingCategory() method has been created in the AffiliateControllerTest class:

The Affiliate Backend

To add a new affiliate section in our admin we will need to create a new AffiliateAdmin class:

Create the two templates used above:

app/Resources/view/affiliateAdmin/list_action_activate.html.twig:

and app/Resources/view/affiliateAdmin/list_action_deactivate.html.twig:

Now create the AffiliateAdminController.php file that will contain our activate and deactivate actions:

Finally add the new admin section to the services.yml file:

That’s it, you can now activate and deactivate the affiliates that register to our website:

Sending Emails

Whenever an affiliate account is activated by the administrator, an email should be sent to the affiliate to confirm his subscription and give him his token.

We will use the SwiftmailerBundle, which leverages the power of the Swift Mailer library. This bundle comes with the Symfony Standard Edition.

First you need to configure the mailer in the parameters.yml file. We will use SendGrid for this example but you can use other email providers if you want:

Now edit the activate action to send an email when the administrator validates an affiliate:

Here we used a new emails/registration.html.twig template for the email:

That’s it! We’re done for today. Get the code from here: https://github.com/dragosholban/jobeet-sf2.8/tree/day15


About the Author

Passionate about web & mobile development. Doing this at IntelligentBee for the last 5 years. If you are looking for custom web and mobile app development, please contact us here and let’s have a talk.