Using the "extends CAction" Class of the Yii Structure

in this Yii framework tutorial http://www.yiiframework.com/doc/guide/1.1/en/basics.controller#action

I want to put my actions from the controller into separate action files and the instructions say "create an action class"

and this is my action class file

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); } } 

and in my controller

 class SandboxController extends Controller{ public function actions(){ // return external action classes, eg: 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', ), ); } } 

and I am viewing a separate action controller using

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

and my mistake

LoginAction and its behavior do not have a method or closure called Do.

Did I do something? thanks.

here is stacktrace

CException LoginAction and its behavior do not have a method or closure named "render".

D: \ XAMPP \ HTDOCS \ MySite \ framework \ base \ CComponent.php (266)

254 public function __call ($ name, $ parameters) 255 {256
if ($ this → _ m! == null) 257 {258 foreach ($ this → _ m as $ object) 259 {260
if ($ object-> getEnabled () && method_exists ($ object, $ name)) 261
return call_user_func_array (array ($ object, $ name), $ parameters); 262
} 263} 264 if (class_exists ('Closure', false) && & & $ this-> canGetProperty ($ name) && & & $ this → $ name instanceof Closure) 265
return call_user_func_array ($ this → $ name, $ parameters); 266
throw new CException (Yii :: t ('yii', '{class} and its behavior do not have a method or closure named "{name}"., 267
array ('{class}' => get_class ($ this), '{name}' => $ name))); 268} 269 270 / ** 271 * Returns a named behavior object. 272 * The name "asa" means "as a." 273 * @param string $ behavior name of the behavior 274 * @return IBehavior - object of behavior or null if the behavior does not exist 275 * / 276 public function asa ($ behavior) 277 {278 return isset ($ this → _ m [$ behavior] )? $ this → _ m [$ behavior]: null; Stack trace

0

  • D: \ XAMPP \ HTDOCS \ MySite \ protected \ Controllers \ authentication \ LoginAction.php (26): CComponent → __ call ("render", array ("index", array ("loginmodel" => LoginForm)))

    one

  • 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")

    eight

  • 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 Framework / 1.1.10

+4
source share
2 answers

The problem with this line of code:

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

It would be nice if it was inside the controller, but as soon as the code moves inside the selected action class, the render method will no longer be called in $this , therefore, an error.

You just need to first get a link to the controller and call render on this:

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

You can extend the action to access any controller method:

 //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 { (...) 
+2
source

Source: https://habr.com/ru/post/1399731/


All Articles