Yii2删除前端/ Web后不显示图像

时间:2016-12-29 12:23:37

标签: php yii2 yii2-advanced-app

我已经应用了漂亮的网址并从网址中移除了前端/网页,但图片无效。

帮助访问前端的图像

我已经引用此url来创建漂亮的网址

代码:myapp / .htaccess

Options +FollowSymlinks
RewriteEngine On

RewriteCond %{REQUEST_URI} ^/myapp/(assets|css|images)  
RewriteRule ^assets/(.*)$ frontend/web/assets/$1 [L]
RewriteRule ^css/(.*)$ frontend/web/css/$1 [L]
RewriteCond %{REQUEST_URI} !^/myapp/(frontend|backend)/web/(assets|css)/ 
RewriteCond %{REQUEST_URI} !index.php
RewriteCond %{REQUEST_FILENAME} !-f [OR]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ frontend/web/index.php/$1 [L]

代码:/myapp/frontend/config/main.php

use \yii\web\Request;

$baseUrl = str_replace('/frontend/web', '', (new Request)->getBaseUrl());

$params = array_merge(
    require(__DIR__ . '/../../common/config/params.php'),
    require(__DIR__ . '/../../common/config/params-local.php'),
    require(__DIR__ . '/params.php'),
    require(__DIR__ . '/params-local.php')
);


return [
    'id' => 'app-frontend',
    'basePath' => dirname(__DIR__),
    'bootstrap' => ['log'],
    'controllerNamespace' => 'frontend\controllers',

    'components' => [
        'request' => [
            'csrfParam' => '_csrf-frontend',
            'class' => 'common\components\Request',
            'web'=> '/frontend/web'
        ],
        'user' => [
            'identityClass' => 'common\models\User',
            'enableAutoLogin' => true,
            'identityCookie' => ['name' => '_identity-frontend', 'httpOnly' => true],
        ],
        'session' => [
            // this is the name of the session cookie used for login on the frontend
            'name' => 'advanced-frontend',
        ],
        'log' => [
            'traceLevel' => YII_DEBUG ? 3 : 0,
            'targets' => [
                [
                    'class' => 'yii\log\FileTarget',
                    'levels' => ['error', 'warning'],
                ],
            ],
        ],
        'errorHandler' => [
            'errorAction' => 'site/error',
        ],


        'urlManager' => [
                'enablePrettyUrl' => true,
                'showScriptName' => false,
                'rules' => [

                         '<alias:login|logout|signup>' => 'site/<alias>',   
                          'users'=>'user/index',                                             
                      ],
                ],
        ],
    'params' => $params,
];

2 个答案:

答案 0 :(得分:1)

修改
像这样应用漂亮的URL:

  1. 删除您在myapp内创建的.htaccess文件,并在myapp/backend/webmyapp/frontend/web文件夹中创建另一个文件夹,如下所示:
  2.   

    上的RewriteEngine      

    RewriteCond%{REQUEST_FILENAME}!-f

         

    RewriteCond%{REQUEST_FILENAME}!-d

         

    RewriteRule。的index.php

    1. common/config/main.php中添加urlManager组件,如下所示:

      enter image description here

      1. 删除您在myapp/frontend/config/main.php
      2. 中创建的urlManager组件
      3. 现在我想你会像下面一样容易得到你的照片:
    2. @web是当前运行的Web应用程序(前端/ Web)的基本URL。它与yii\web\Request::$baseUrl具有相同的值。

      所以你应该得到如下图像:

      <?php echo Html::img('@web/images/test.png', []); ?>
      

      <?php echo Html::img( Yii::$app->request->baseUrl.'/images/test.png', []); ?>
      

      假设你有一个文件夹&#34; images&#34;在包含图像的web目录中(示例中为test.png)

答案 1 :(得分:0)

试试这个:
<img src="<?= Yii::$app->request->baseUrl ?>/img/image1.jpg">