Yii cannot start the controller

As I see if we want to create an instance of the model (for example, with a name Post), we just need to call:

$post = new Post();

Now I also want to create an instance Controller(for example, with the name Postand php file for this controller with the name PostController.php). Therefore, I use this code:

$postController = new PostController();

However, when I run this code, I get an error.

I did a few searches and found that the following should be done to create the instance:

$postController = Yii::app()->createController('post/index');

It is working correctly. But I'm still wondering why the first approach does not work?

+5
source share
2 answers

Answering your exact question "why the first approach does not work." The folder is /protected/controller NOT in the "include path" of the project.

'import'=>array('application.controllers.*') include(Yii::app()->getBasePath().DIRECTORY_SEPARATOR.'controllers'.DIRECTORY_SEPARATOR.'PostController.php');

PostController. Ah , -

$controller = new PostController('post_controller');

, Yii, , , . , Yii::app()->createController()

+5
+1

All Articles