Home » Php » php – Using a favicon in Zend Framework

php – Using a favicon in Zend Framework

Posted by: admin July 12, 2020 Leave a comment


What’s the best way of handling a favicon.ico in Zend framework? I have seen alternatives that requires modification of phtml files like:

$this->headLink()->headLink(array('rel' => 'favicon',
                              'href' => '/img/favicon.ico'),

or modifications on .htaccess file.


How to&Answers:

Three choices:

  • Do nothing – if the favicon is named favicon.ico and sitting in your public directory, most browsers will pick it up
  • Hard-code the <link> tag into your layout
  • Programmatically add the link tag as in your code example

Personally I would go for option 2. There’s little point programmatically adding the favicon if it will always be there. It might make sense if the favicon was different for different users, e.g. say your application had a themes system with different favicons for different themes, then you might want to use headLink().

Browsers tend to cache favicons for a long time, so as long as the link tag is appearing in your HTML source then it will eventually update. You can speed up the process by changing the filename, viewing the favicon directly in your browser and/or clearing your browser cache.


you just move the favicon.ico file into the folder public! 😉


I found that if rel attribute being “favicon” expected things not working, until i’ve change above attribute to “shortcut icon” (tested under Firefox 5.0 Ubuntu)


//in controller
$this->view->headLink(array('rel' => 'shortcut icon', 'href' => '/themes/kieuhung/images/favico.png', 'type' => 'image/x-icon'), 'PREPEND');

//and in layout
echo $this->headLink();

good luck


The ZF2 skeleton application includes a default favicon.ico at /public/img/favicon.ico. Just change that file and you’re good to go.

Can also help to force the browser cache to refresh the file by pointing it to that file path explicitly.