Reputation: 1828
I am a complete Yii newbie so please forgive a simple question. I've been reading up on various posts and can't find anything that works. All I'm trying to do is setup a Yii site (which I've done) and then link to a static page using my Main layout.
Below are the three files I think are relevant:
Within the footer of the main layout, the link to the static page is:
<a href="index.php?r=site/page&view=terms_of_use">Terms of Use</a>
When I click on it, it generates what I think is the correct url in the brower address bar:
http://localhost/Company/index.php?r=site/page&view=terms_of_use
but what gets shown is the content of index.php, not terms_of_use.php. I'm using the default SiteController. Is there something special about index.php I don't know about, or am I doing something else dumb? Thanks for any help.
views/site/index.php:
<?php
/* @var $this SiteController */
$this->pageTitle=Yii::app()->name;
?>
<div id="content" class = "clearfix">
<div class="threeColBlock">
<div class="padded">
<h2 class="pageTitle">Heading 1</h2>
<p>Blurb 1</p>
</div>
</div>
<div class="threeColBlock">
<div class="padded">
<h2 class="pageTitle">Heading 2</h2>
<p>Blurb 2</p>
</div>
</div>
<div class="threeColBlock">
<div class="padded">
<h2 class="pageTitle">Heading 3</h2>
<p>Blurb 3</p>
</div>
</div>
</div>
views/site/pages/terms_of_use.php:
<?php
/* @var $this SiteController */
$this->pageTitle=Yii::app()->name . ' - About';
$this->breadcrumbs=array(
'About',
);
?>
<div id="content" class = "clearfix">
<h2 class="pageTitle">Terms of Use</h2>
<div class = "smallText">
<p>
Some legal junk
</p>
</div>
</div>
views/layouts/views/main.php:
<?php /* @var $this Controller */ ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="language" content="en" />
<!-- blueprint CSS framework -->
<link rel="stylesheet" type="text/css" href="<?php echo Yii::app()->request->baseUrl; ?>/css/screen.css" media="screen, projection" />
<link rel="stylesheet" type="text/css" href="<?php echo Yii::app()->request->baseUrl; ?>/css/print.css" media="print" />
<!--[if lt IE 8]>
<link rel="stylesheet" type="text/css" href="<?php echo Yii::app()->request->baseUrl; ?>/css/ie.css" media="screen, projection" />
<![endif]-->
<link rel="stylesheet" type="text/css" href="<?php echo Yii::app()->request->baseUrl; ?>/css/main.css" />
<link rel="stylesheet" type="text/css" href="<?php echo Yii::app()->request->baseUrl; ?>/css/form.css" />
<link rel="stylesheet" type="text/css" href="<?php echo Yii::app()->request->baseUrl; ?>/css/marketing.css">
<link rel="stylesheet" type="text/css" href="<?php echo Yii::app()->request->baseUrl; ?>/css/cssmenu.css" media="screen" />
<title><?php echo CHtml::encode($this->pageTitle); ?></title>
</head>
<body>
<div id="header" class="clearfix">
<div id="logo"><image src="images/logo,56x38,trans(white).gif"></div>
<div id="logoName">Company</div>
<div id="topRight">Bla bla bla</b></div>
</div>
<!-- Menu -->
<div id='cssmenu'>
<ul>
<li class='active'><a href='dummy.com'><span>home</span></a></li>
<li class='has-sub'><a href='#'><span>products</span></a>
<ul>
<li><a href='dummy.com'><span>prod1</span></a></li>
<li><a href='dummy.com'><span>prod2</span></a></li>
<li class='last'><a href='dummy.com'><span>prod3</span></a></li>
</ul>
</li>
<li><a href='dummy.com'><span>about</span></a></li>
<li class='last'><a href='dummy.com'><span>contact</span></a></li>
</ul>
</div>
<div id="mainImageContainer">
<div id="mainImage">
<image src = "images/main_image.jpg">
</div>
</div>
<?php echo $content; ?>
</div><!-- page -->
<div id="footer">
Copyright © 2011 Company. All rights reserved. | <a href="index.php?r=site/page&view=terms_of_use">Terms of Use</a>
</div>
</body>
</body>
</html>
Upvotes: 2
Views: 3994
Reputation: 17478
Try not to hardcode urls, instead use functions like createUrl
, (there are other variants of it).
When you hardcode urls the problem you faced could arise, createUrl
takes into account your urlManager configuration, and generates a url accordingly, so when you make changes you don't have to go to every view and change the url again.
You can use it like so:
<a href="<?php echo Yii::app()->createUrl('/site/page',array('view'=>'terms_of_use')); ?>">About</a>
Or use CHtml::link
:
echo CHtml::link('About',array('/site/page', 'view'=>'temrs_of_use'));
Upvotes: 0
Reputation: 1828
Found it. I had enabled the urlManager in config/main.php. If I change my original link to the following then I get what I want.
http://localhost/Company/index.php/site/page/view/terms_of_use
Upvotes: 2
Reputation: 6150
In regards to your specific question, I believe you may have forgotten the step of overriding the actions()
method in the default site controller - though that raises the question: Which version of the Yii framework are you using? The override is already included in Yii 1.1.12.
Also, as per the comments on that yii wiki article:
If you happen to use accessRules, don't forget to add 'page' (or whatever name you have in) to allowed action as well.
http://www.yiiframework.com/wiki/22/how-to-display-static-pages-in-yii/ is a great post on using static pages in Yii, the bulk of it:
First, in the default SiteController (or other controller if you like), override the actions() method as follows,
public function actions()
{
return array(
'page'=>array(
'class'=>'CViewAction',
),
);
}
Second, create a folder protected/views/site/pages.
Third, save each static page as a PHP file under this folder. For example, we can save the "about this site" page as about.php. Note, these pages will use the application's default layout. Therefore, only the main content needs to be saved in each file.
We are done! To access a static page, e.g., the about page, we can use the following URL:
http://www.example.com/index.php?r=site/page&view=about
Upvotes: 0