Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Custom favicon in Yii2

Tags:

php

yii2

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?

like image 250
Jorgeska Avatar asked May 28 '15 16:05

Jorgeska


People also ask

How to set favicon in Yii2?

Just copy favicon. ico into your webroot directory. That's it. Simply copy your favicon.

How do I make a favicon in CSS?

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.


4 Answers

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']);

like image 60
johnsnails Avatar answered Oct 05 '22 05:10

johnsnails


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.

like image 22
Serghei Leonenco Avatar answered Oct 05 '22 05:10

Serghei Leonenco


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.

like image 24
temirbek Avatar answered Oct 05 '22 04:10

temirbek


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" />
like image 35
ScaisEdge Avatar answered Oct 05 '22 04:10

ScaisEdge