Custom Icon in Yii2

I am working with a basic Yii2 template. In layouts / main.php, in the head section, I installed

<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 it works. My application continues to show the main template icon instead of mine, that is, in the root folder, as usual.

I know that I can publish this file to avoid this problem, but I think that there should be no need.

What am I doing wrong? And how does Yii2 display its standard icon anyway?

+5
source share
5 answers

as rkm mentioned this because of the browser cache. An easy fix is ​​to add a version like ../favicon.ico?v=1 , and the browser will download the new version.

+6
source

Another way to set the icon described in documentaion is to add something similar to the main.php file located in views/layouts , $this->registerLinkTag(['rel' => 'icon', 'type' => 'image/png', 'href' => '/favicon.png']);

+7
source

I have favicon.ico in the apps / websites directory and the code below works fine

  <link rel="shortcut icon" href="<?php echo Yii::$app->request->baseUrl; ?>/favicon.ico" type="image/x-icon" /> 
+1
source

instead of using:

Yii :: $ app-> request-> BaseUrl;

try using:

Yii :: $ app-> assetManager-> getPublishedUrl ('@ interface / assets / distance)

0
source

Step 1: Define the general parameter in ( common/config/params.php ), as shown below:

 return [ 'adminEmail' => ' admin@example.com ', 'supportEmail' => ' support@example.com ', 'user.passwordResetTokenExpire' => 3600, 'commonPath' => '/<your_project_name>/common', ]; 

Step 2: Use the defined commonPath to display the favicon icon as follows:

 <link rel="shortcut icon" href="<?= Yii::$app->params['commonPath']; ?>/favicon.ico" type="image/x-icon" /> 

Note. Put the favicon.ico directory in common/ .

0
source

All Articles