I am working with the Yii2 basic template. In layouts/main.php, in the head section, I have set
<link rel="shortcut icon" href="<?php echo Yii::$app->getHomeUrl(); ?>/favicon.ico" type="image/x-icon" />
or
<link rel="shortcut icon" href="/favicon.ico" type="image/x-icon" />
or ... (so on), but anything seems to work. My application keeps on showing the basic template favicon instead of mine, that is in the root web folder as usual.
I know I could publish this file to avoid this problem, but I think there should be no need.
What am I doing wrong? And how does Yii2 displays its standard favicon anyway?
Just copy favicon. ico into your webroot directory. That's it. Simply copy your favicon.
You can't set a favicon from CSS - if you want to do this explicitly you have to do it in the markup as you described. Most browsers will, however, look for a favicon. ico file on the root of the web site - so if you access http://example.com most browsers will look for http://example.com/favicon.ico automatically.
Another way to set the favicon as described in the documentaion is by adding something like this to the main.php
file located under views/layouts
.
$this->registerLinkTag(['rel' => 'icon', 'type' => 'image/png', 'href' => '/favicon.png']);
You can do this as simple as adding an extra line to your AppAsset.php
file:
public $css = [
[
'href' => 'path-to-your-icon/icon-32x32.png',
'rel' => 'icon',
'sizes' => '32x32',
],
[
'href' => 'path-to-your-icon/icon-192x192.png',
'rel' => 'icon',
'sizes' => '192x192',
],
[
'href' => 'path-to-your-icon/icon-180x180.png',
'rel' => 'apple-touch-icon-precomposed',
],
'css/site.css',
];
Hope it will helps.
as rkm mentioned it's because of browser cache. Easy fix is to add version like ../favicon.ico?v=1
and browser will load new version.
I have the favicon.ico in app/web directory and the code below is working properly
<link rel="shortcut icon" href="<?php echo Yii::$app->request->baseUrl; ?>/favicon.ico" type="image/x-icon" />
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With