6
class SomeController extends Controller
{

        public function actionIndex() {
                echo 'This is some controller';
        }
}


class AnotherController extends SomeController
{

        public function actionIndex() {
                echo 'This is another controller';
        }
}

This works:

index.php?r=some

but ...

index.php?r=another

says:

PHP warning

include(SomeController.php): failed to open stream: No such file or directory

Both of the files are in

test\protected\controllers\

BTW in the past I also tried using the Gii Controller Generator with "SomeController" as the base class...

It said:

The controller has been generated successfully. You may try it now.

Generating code using template 
"C:\xampp\htdocs\yii\framework\gii\generators\controller\templates\default"...
generated controllers\YetAnotherController.php
generated views\yetAnother\index.php
done!

When I clicked on "try it now" it also said:

PHP warning

include(SomeController.php): failed to open stream: No such file or directory

2 Answers 2

12

Edit:

Classes inside protected/controllers are not autoloaded, therefore you'll have to import the parent class file before extending from it:

In AnotherController.php:

Yii::import('application.controllers.SomeController');
public class AnotherController extends SomeController {
    // ...
}

Incase you need to access the base class from url also, you can use the above method. Otherwise you can put your base class inside protected/components as you have already figured out.


Yii autoloading works only when you have the same name for the file as the class that the file contains. Meaning class SomeController should be within SomeController.php file.

Make those changes and it should work.

A helpful wiki: Understanding Autoloading Helper Classes and Helper functions.

Guide link:

Class files should be named after the public class they contain.

Sign up to request clarification or add additional context in comments.

6 Comments

Yep it's in \protected\controllers\SomeController.php BTW like I said "localhost/yii/testapp/index.php?r=some" works... But gii's "YetAnotherController.php" (index.php?r=yetAnother) and my "AnotherController.php" (index.php?r=another) give errors about their base class (SomeController.php). There seems to be a problem with the autoloading of SomeController.php when it is the base class but it works fine if I'm using index.php?r=some. Also I'm getting the same error when I'm referencing Post.php within a controller... it is in models/Post.php...
oh ok, then will you be accessing the base controller from url? or is it just a base class, and you wish to only use it as such?
Sorry it looks like the base class is meant to go in /components/ - I wasn't reading the ebook properly.
yes but then you wont be able to use it from url, for that you'll have to import
This has to be one of the most aggravating things about Yii.
|
3

To extend any class just go to the config file and add the class in the import section

'import' => array('application.controllers.SomeController')

this will make it available in the entire application without importing explicitly.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.