Symfony – Serve External Apis from External Hosts – jQuery – Yui
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.
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.
- Symfony – Share Template Across Multiple Apps
- PHP – Symfony – Speed up CLI Commands – Do Not Load Web Plugins
- PHP – Symfony – Build Model – No yml schema found
- Symfony – Propel – Determine Propel Version
- Symfony – Doctrine – Call to a member function evictAll on a non-object
- Symfony – Standard API for Logging Using __callStatic
- Symfony – Symfony 2 Security Audit Results
- Symfony – sfGuardPlugin – Use Email Instead of Username
- Symfony – PHP – Possibly Forking 1.x so Invested Companies don’t Lose Millions
- Symfony – Remove .php From Controller using Symlink