As a developer you have probably had to use a 3rd party API library to make your job easier. Take for example jQuery. jQuery abstracts many internal javascript constructs which make calling code much easier to work with, and jQuery works the same across most standard web browsers. Which brings me to the point of this post, what is the best method of bringing these 3rd party API libraries into your projects? Sure you could download the files and serve them from your own web-servers, but why? Why not let 3rd party API hosts such as Google or Yahoo, serve these API libraries for you?

Consider the following links:

Typical host construct: http://static.yourdomain.com/js/vendor/jquery/jquery.min.js

3rd party API host construct: http://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js

What’s the difference between the two files? Nothing (given that you abstracted out the version number using a symlink in the first example). However, with the 3rd party API host you are using their resources vs using your own resources. So that means more http connections for serving actual content rather than serving API libraries. Also, serving API libraries from these external hosts also ensures faster delivery of the data because there is a high probability that the 3rd party host is using a CDN (or cloud) to serve the request.

Now we see why it’s better to use 3rd party API hosts to serve common API libraries, but how do we configure Symfony to serve these API libraries. We don’t want to litter our templates with url constructs, so we are going to add some code to our top level app.yml and view.yml files. If you are unaware, a flexible feature of using yml files vs xml or ini is that with yml files you can add php code, which will be executed when the yml files are parsed.

First, lets set our 3rd party API host, and jquery version via our app.yml file. Note: You can set these same values at the global config level (/config/app.yml vs /apps/<app_name>/config/app.yml) so all your apps will benefit.

#/apps/<app_name>/config/app.yml

all:
jquery_api_version: 1.6.2
google_ajax_libs_api_uri: http://ajax.googleapis.com/ajax/libs

Ok, now lets configure our view.yml file. Same note as above, this can be set at the global config level so all your apps will benefit.


#/apps/<app_name>/config/view.yml

default:

javascripts:

- <?php echo sfConfig::get('app_google_ajax_libs_api_uri') ?>/jquery/<?php echo sfConfig::get('app_jquery_api_version') ?>/jquery.min.js
- <?php echo sfConfig::get('app_google_ajax_libs_api_uri') ?>/jqueryui/<?php echo sfConfig::get('app_jquery_ui_api_version') ?>/jquery-ui.min.js

Pretty easy right? Notice the – (hyphen) which starts the line, this is a flag to yml parsers that data within current tag will be an array. I found this syntax much easier to deal with vs having several entries on the same line. Also, check out the php echos, they are simply referring to settings we initialized in our app.yml file.

Not only does this solution allow you to serve 3rd party API libraries from 3rd party hosts, you can now upgrade all API libraries with a few minor changes in a centralized location.