【问题标题】:using "extends CAction" class of Yii framework使用 Yii 框架的“扩展 CAction”类
【发布时间】:2012-03-22 17:49:34
【问题描述】:

在这个 Yii 框架教程中 http://www.yiiframework.com/doc/guide/1.1/en/basics.controller#action

我想把我的动作从控制器放到一个单独的动作文件中 并按照说明“创建一个 Action 类”

这是我的动作类文件

class LoginAction extends CAction
{

    private $contents = array();
    public function run(){
        $loginmodel = new LoginForm;

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

        //collect user input data to do login
        if(isset($_POST["LoginForm"]))
        {
            $loginmodel->attributes = $_POST["LoginForm"];
            // validate user input and redirect to the previous page if valid
            if($loginmodel->validate() && $loginmodel->login()){ //<--invoking here the login and validate function
                $this->redirect(Yii::app()->user->returnUrl);
            }
        }

        $this->contents["loginmodel"] =  $loginmodel;
        $this->render('index',$this->contents); 
    }    

}

在我的控制器中

class SandboxController extends Controller{       
    public function actions(){
        // return external action classes, e.g.:
            return array(
            'authlog'=>'application.controllers.authentication.LoginAction',
            // page action renders "static" pages stored under 'protected/views/site/pages'
            // They can be accessed via: index.php?r=site/page&view=FileName
            'page'=>array(
                'class'=>'CViewAction',
            ),
        );   
    }
}

我使用浏览单独的动作控制器

http://localhost/mysite/index.php/sandbox/authlog/login

我的错误是

LoginAction 及其行为没有名为的方法或闭包 “渲染”。

我是不是做错了什么?谢谢。

这里是堆栈跟踪

CException LoginAction 及其行为没有方法或 名为“render”的闭包。

D:\xampp\htdocs\mysite\framework\base\CComponent.php(266)

254 公共函数 __call($name,$parameters) 255 { 256
if($this->_m!==null) 257 { 258 foreach($this->_m 作为 $object) 259 { 260
if($object->getEnabled() && method_exists($object,$name)) 261
返回 call_user_func_array(array($object,$name),$parameters); 262
} 263 } 264 if(class_exists('Closure', false) && $this->canGetProperty($name) && $this->$name instanceof Closure) 265
返回 call_user_func_array($this->$name, $parameters); 266
throw new CException(Yii::t('yii','{class} 及其行为不 有一个名为“{name}”的方法或闭包。',267
array('{class}'=>get_class($this), '{name}'=>$name))); 268 } 269 270 /** 271 * 返回命名的行为对象。 272 * “asa”这个名字代表“as a”。 273 * @param 字符串 $behavior 行为名称 274 * @return IBehavior 行为对象,或 如果行为不存在,则为 null 275 */ 276 public 函数 asa($behavior) 277 { 278 返回 isset($this->_m[$behavior]) ? $this->_m[$behavior] : null;堆栈跟踪

0

  • D:\xampp\htdocs\mysite\protected\controllers\authentication\LoginAction.php(26): CComponent->__call("render", array("index", array("loginmodel" => 登录表单)))

    1

  • D:\xampp\htdocs\mysite\protected\controllers\authentication\LoginAction.php(26): LoginAction->render("index", array("loginmodel" => LoginForm))

    2

  • D:\xampp\htdocs\mysite\framework\web\actions\CAction.php(75): LoginAction->run()

    3

  • D:\xampp\htdocs\mysite\framework\web\CController.php(309): CAction->runWithParams(array("login" => ""))

    4

  • D:\xampp\htdocs\mysite\framework\web\CController.php(287): CController->runAction(LoginAction)

    5

  • D:\xampp\htdocs\mysite\framework\web\CController.php(266): CController->runActionWithFilters(LoginAction, array())

    6

  • D:\xampp\htdocs\mysite\framework\web\CWebApplication.php(276): CController->run("authlog")

    7

  • D:\xampp\htdocs\mysite\framework\web\CWebApplication.php(135): CWebApplication->runController("sandbox/authlog/login")

    8

  • D:\xampp\htdocs\mysite\framework\base\CApplication.php(162): CWebApplication->processRequest()

    9

  • D:\xampp\htdocs\mysite\index.php(13): CApplication->run() 2012-03-05 09:37:43 Apache/2.2.21 (Win32) mod_ssl/2.2。 21 OpenSSL/1.0.0e PHP/5.3.8 mod_perl/2.0.4 Perl/v5.10.1 Yii 框架/1.1.10

【问题讨论】:

  • 请提供与错误相关的完整堆栈跟踪。
  • 刚刚添加了错误堆栈跟踪

标签: php yii magic-methods


【解决方案1】:

问题出在这行代码上:

$this->render('index',$this->contents);

如果它在控制器内部会很好,但是一旦将代码移动到专用操作类中,就不再需要在 $this 上调用 render 方法,因此会出现错误。

您只需要先获取对控制器的引用,然后在上面调用render

$controller=$this->getController();
$controller->render('index',$this->contents);

【讨论】:

    【解决方案2】:

    您可以扩展您的操作以访问任何控制器方法:

    //MyAction.php
    <?php
    
    class MyAction extends CAction
    {
        public function render($view, array $options=array())
        {
            $this->getController()->render($view, $options);
        }
    }
    
    //LoginAction.php:
    <?php
    
    class LoginAction extends MyAction
    {
    (...)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-09-21
      • 1970-01-01
      • 2012-08-11
      • 2016-01-14
      • 1970-01-01
      相关资源
      最近更新 更多