Reputation: 181
I created a separate view controller called HotelController.php
and add the following inside:
<?php
namespace app\controllers;
use Yii;
use yii\web\Controller;
use yii\filters\AccessControl;
/**
* HotelController is only for displaying static pages.
*/
class HotelController extends Controller
{
/**
* Displays news story 1 page.
*
* @return string
*/
public function actionSunshine()
{
return $this->render('sunshine');
}
}
I then created a folder inside view
folder named hotel
. Inside this folder I created a view named sunshine.php
. This works well and displays the page when I visit mysite.com/hotel/sunshine
. The problem now is that images on the sites disappeared because instead of the page getting the image from this path mysite.com/img/location.jpg
, the browser is attempting to get it from mysite.com/hotel/img/location.jpg
.
In web.php, I have my rewrite rules set as below:
'urlManager' => [
'class' => 'yii\web\UrlManager',
'enablePrettyUrl' => true,
'showScriptName' => false,
'enableStrictParsing'=>false,
'rules' => [
'<alias:[\w\-]+>' => 'site/<alias>'
],
],
How can I stop this problem from happening. I only want the page url structure not rewrite other links on the page.
UPDATE:
In the sunshine.php file, we have a sample code below:
<?php
use yii\helpers\Html;
use worstinme\uikit\ActiveForm;
use yii\captcha\Captcha;
$this->title = 'Demo';
<?php
use yii\helpers\Url;
?>
<div class="uk-section" src="img/demo.jpg">
<div class="uk-container">
<div class="uk-width-1-1">
<div class="left-indent uk-light">
<h1>Lorem Ipsum </h1>
</div>
<p>Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aenean commodo ligula eget dolor. Aenean massa. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Donec quam felis, ultricies nec, pellentesque eu, pretium quis, sem. Nulla consequat massa quis enim.</p>
<p>Donec pede justo, fringilla vel, aliquet nec, vulputate eget, arcu. In enim justo, rhoncus ut, imperdiet a, venenatis vitae, justo. Nullam dictum felis eu pede mollis pretium. Integer tincidunt. Cras dapibus. Vivamus elementum semper nisi. Aenean vulputate eleifend tellus.</p>
</div>
</div>
</div>
The image in the html code fails to load for the reason I gave before.
Upvotes: 0
Views: 61
Reputation: 46
You have included the Url helper but you have not used it. You could change the src path to:
<?= Url::to('@web/img/demo.gif'); ?>
Where @web is the web root.
This will automatically updated the path to the webroot for you regardless of where the view is.
Upvotes: 1
Reputation: 22174
Your image is using relative URL, so it is always relative to current URL. You should use getBaseUrl()
to make it absolute:
<div class="uk-section" src="<?= Yii::$app->getRequest()->getBaseUrl() ?>/img/demo.jpg">
Upvotes: 0