我已经应用了漂亮的网址并从网址中移除了前端/网页,但图片无效。
帮助访问前端的图像
我已经引用此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,
];
答案 0 :(得分:1)
修改强>
像这样应用漂亮的URL:
myapp
内创建的.htaccess文件,并在myapp/backend/web
和myapp/frontend/web
文件夹中创建另一个文件夹,如下所示:上的RewriteEngine
RewriteCond%{REQUEST_FILENAME}!-f
RewriteCond%{REQUEST_FILENAME}!-d
RewriteRule。的index.php
在common/config/main.php
中添加urlManager组件,如下所示:
myapp/frontend/config/main.php
@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">