Linked style sheet woes with Zend Framework

One of the first frustrations I ran into with Zend framework was with linking images and style sheets to a web site. In my pre-Zend days of web programming, I’d just use a relative link, such as the following:

<link rel='stylesheet' href='theme/style.css' type='text/css' media='all' />

This link works fine within the default controller and action. However, when a different controller and/or action is specified, the URL changes, and the relative link breaks.  In search for an answer, some programmers suggested that the solution is to simply add a slash at the beginning of the href to make the link absolute, like so:

<link rel='stylesheet' href='/theme/style.css' type='text/css' media='all' />

Although this may work, it still breaks if you ever store the website in a sub-directory as opposed to the root of the webserver’s public directory.  In my case, this solution was a non-starter, as my development site uses sub-directories for each project.  I use a symbolic link to link a particular sub-directory on my development site to each of the Zend projects “public” directories.    (sym-linked to /zendapps/project1/public)    (sym-linked to /zendapps/project2/public)   (sym-linked to /zendapps/project3/public)

Using a non-changing absolute link would cause the code to break as soon as it is copied to my production server, where each project resides by itself in the webserver’s root directory.

The real solution to this problem is to use Zend’s built-in baseUrl() view helper.  Then your code will look like this:

<link rel='stylesheet' href='<?php echo $this->baseUrl(); ?>/theme/style.css' type='text/css' media='all' />

Doing it this way makes your code work without modification regardless of where your project ends up.  I use this anytime I’m linking to a resource (image, javascript file, or CSS file) in my Zend project’s public directory.

44 thoughts on “Linked style sheet woes with Zend Framework

  1. Pingback: glen

  2. Pingback: Shaun

  3. Pingback: mario

  4. Pingback: Casey

  5. Pingback: luis

  6. Pingback: Bryan

  7. Pingback: Mitchell

  8. Pingback: francis

  9. Pingback: allen

  10. Pingback: Roger

  11. Pingback: brett

  12. Pingback: oscar

  13. Pingback: Dustin

  14. Pingback: Brian

  15. Pingback: jeffery

  16. Pingback: warren

  17. Pingback: Ted

  18. Pingback: Brandon

  19. Pingback: William

  20. Pingback: gabriel

  21. Pingback: Paul

  22. Pingback: lloyd

  23. Pingback: Edgar

  24. Pingback: wallace

  25. Pingback: Alberto

  26. Pingback: phillip

  27. Pingback: alberto

  28. Pingback: Mathew

  29. Pingback: herman

  30. Pingback: jaime

  31. Pingback: jordan

  32. Pingback: Jorge

  33. Pingback: Sidney

  34. Pingback: Juan

  35. Pingback: alex

  36. Pingback: Willie

  37. Pingback: Michael

  38. Pingback: Reginald

  39. Pingback: calvin

  40. Pingback: dean

  41. Pingback: Raymond

  42. Pingback: Bernard

  43. Pingback: andre

  44. Pingback: Theodore

Comments are closed.