Php Yii在登录后重定向到其他查看页面

Php Yii在登录后重定向到其他查看页面,php,yii,Php,Yii,在我的web应用程序中,我有一个名为“MyHome”的页面,在成功登录后应该可以查看该页面。我已将该页面包含在网站文件夹下。但它显示以下错误 Error 404 The system is unable to find the requested action "MyHome". siteController的actionLogin的我的代码 public function actionLogin() { $model=new LoginForm; if(isset($

在我的web应用程序中,我有一个名为“MyHome”的页面,在成功登录后应该可以查看该页面。我已将该页面包含在网站文件夹下。但它显示以下错误

Error 404
The system is unable to find the requested action "MyHome".
siteController的actionLogin的我的代码

public function actionLogin()
{


    $model=new LoginForm;


    if(isset($_POST['ajax']) && $_POST['ajax']==='login-form')
    {
        echo CActiveForm::validate($model);
        Yii::app()->end();
    }


    if(isset($_POST['LoginForm']))
    {
        $model->attributes=$_POST['LoginForm'];

        if($model->validate() && $model->login())

    //  $this->redirect(Yii::app()->user->returnUrl);
        $this->redirect(array('/site/MyHome'));

    }

    $this->render('login',array('model'=>$model));
}
MyHome视图页面的我的代码

<?php /* @var $this Controller */ ?>
<?php Yii::app()->bootstrap->register(); ?>
<!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" />

    <title><?php echo CHtml::encode($this->pageTitle); ?></title>
</head>

<body>

<div class="container" id="page">

    <div id="header">
        <div id="logo"><?php echo CHtml::encode(Yii::app()->name); ?></div>
    </div><!-- header -->

    <div id="mainmenu">
        <?php $this->widget('zii.widgets.CMenu',array(
            'items'=>array(
                array('label'=>'All offers', 'url'=>array('/producerOffer/index'),'visible'=> Yii::app()->user->type==="consumer"),
                array('label'=>'My requirements', 'url'=>array('/consumerReq/view', array('id'=>Yii::app()->user->id)),'visible'=> Yii::app()->user->type==="consumer"),
                array('label'=>'All requirements', 'url'=>array('/ConsumerReq/index'),'visible'=> Yii::app()->user->type==="producer"),
                array('label'=>'My offers', 'url'=>array('/producerOffer/view',array('id'=>Yii::app()->user->id)), 'visible'=>Yii::app()->user->isGuest),
                array('label'=>'Logout ('.Yii::app()->user->name.')', 'url'=>array('/site/logout'), 'visible'=>!Yii::app()->user->isGuest),

            ),
        )); ?>
    </div><!-- mainmenu -->
    <?php if(isset($this->breadcrumbs)):?>
        <?php $this->widget('zii.widgets.CBreadcrumbs', array(
            'links'=>$this->breadcrumbs,
        )); ?><!-- breadcrumbs -->
    <?php endif?>

    <?php echo $content; ?>

    <div class="clear"></div>

    <div id="footer">
        Copyright &copy; <?php echo date('Y'); ?> by My Company.<br/>
        All Rights Reserved.<br/>
        <?php echo Yii::powered(); ?>
    </div><!-- footer -->

</div><!-- page -->

</body>
</html>

请尝试以下代码:

$this->redirect(array('site/MyHome'));
$this->redirect(array('/MyHome'));

您是否在该控制器中定义了操作?这个错误似乎是由于没有定义操作造成的。尝试在站点控制器中定义actionMyHome方法


public function actionMyHome() {
    $this->render('myHome',array());
}
试试这个

$url = Yii::app()->createUrl('site/MyHome');

Yii::app()-request->redirect($url);

OR

$this->redirect($url);

另外,请检查“SiteController”中的“actionMyhome”功能是否可用。

您缺少指向的操作

在您的登录代码中,您有:

$this->redirect(array('/site/MyHome'));
这将重定向到类SiteController中的actionMyHome()方法


public function actionMyHome() {
    $this->render('myHome',array());
}
因此,您需要创建缺少的类:

class SiteController extends Controller {

   public function actionMyHome() {
       echo 'I am still here';
   }

}

生成的
SiteController
有一个
页面
操作,显示静态页面。使用此功能,您无需每次添加静态内容时都添加新操作

要重定向到静态页面,请将页面的代码置于
受保护/视图/站点/页面
,并在URL中使用
页面
操作:

$this->redirect(array('/site/page', 'view'=>'MyHome'));
请尝试以下代码:

$this->redirect(array('site/MyHome'));
$this->redirect(array('/MyHome'));
它应该被定义

public function accessRules()   {       return array(           array('allow', 
                'actions'=>array('myhome'),
                'users'=>array('@'),            ),
                    );  }

我认为Web服务器中存在问题 您使用iis或apache或? 请测试下面的代码

class SiteController extends Controller {
   public function actionMyhome() {
       $this->render('myhome',array());
       }
    }

并在view/site/MyHome.php中将MyHome.php更改为MyHome.php文件

发布您的错误,并在您的问题中添加您的站点控制器代码。不,我没有在siteController中定义该操作,由于该操作是静态页面,并且没有呈现任何数据,我应该如何编写该操作?请您帮助meI将其添加到上述答案中。这就是你想要的吗?它指向我的主页,但只显示模板,而不是视图代码中的所有链接。你能告诉我该怎么做吗?就像它只显示了一个模板,而不是完整的视图页面,所以你是说它没有显示布局?我不知道你说它没有显示完整的查看页面是什么意思?它将在views/site目录中显示您放入$this->render中的任何视图。你是从里面打电话给分部的吗?如果是这样的话,把echo$this->renderPartialPlease放在上面,请改进您的答案代码。看,这足够了吗@ankityes,改进了很多。查看此以了解更多改进