一次显示两个视图,并在更改时更改 URL


Display two views at once and change url on change

我有一个带有菜单的主视图,可以帮助我显示另一个视图。它类似于这样:

<div id="page">
    <div id="menu">
            <a href="controller/page1">Page1</a>
            <a href="controller/page2">Page2</a>
    </div>
    <div id="content">
            <!-- Page1 or Page2 are displayed here -->
    </div>
</div>

我正在使用php的Yii框架。这使我不要使用<?php include("menuview.php"); ?>.所以我正在寻找不同的解决方案。我可以用Ajax做到这一点,但我也希望链接更改为mypage/controller/Page2。使用Ajax,我只能得到这个:mypage/controller/index#Page2

在主视图中,而不是包含 do

<?php echo $this->renderPartial('_page1', array('model'=>$model)); ?>

更新:

受保护/视图/控制器/页面 1.php受保护/视图/控制器/页面 2.php 内容由您喜欢

受保护/视图/布局/自定义.php:

<?php $this->beginContent('//layouts/main'); ?>
<div id="page">
    <div id="menu">
        <?php echo CHtml::ajaxLink('Page1', array('controller/page1'), array('update' => '#content')); ?>
        <?php echo CHtml::ajaxLink('Page2', array('controller/page2'), array('update' => '#content')); ?>
    </div>
    <div id="content">
        <?php echo $content; ?>
    </div>
</div>
<?php $this->endContent(); ?>

受保护/控制器/控制器控制器.php:

class ControllerController extends Controller {
    /**
     * @var string the default layout for the views.
     */
    public $layout = '//layouts/custom';
    public function actionPage1() {
        if (Yii::app()->request->isAjaxRequest)
            $this->renderPartial('page1');
        else
            $this->render('page1');
    }
    public function actionPage2() {
        if (Yii::app()->request->isAjaxRequest)
            $this->renderPartial('page2');
        else
            $this->render('page2');
    }
}

UPDATE2:

如果您也需要更改地址栏中的链接,那么您唯一的选择是使用常规链接而不是 ajax <?php echo CHtml::link('Page1', array('controller/page1')); ?>

使用

Ajax 的首选方法是使用您提到的哈希。