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

Community
  • 1
  • 1
Luke Wenke
  • 1,149
  • 2
  • 23
  • 43

2 Answers2

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.

bool.dev
  • 17,508
  • 5
  • 69
  • 93
  • Yep it's in \protected\controllers\SomeController.php BTW like I said "http://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... – Luke Wenke Jan 04 '13 at 03:19
  • 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? – bool.dev Jan 04 '13 at 03:21
  • Sorry it looks like the base class is meant to go in /components/ - I wasn't reading the ebook properly. – Luke Wenke Jan 04 '13 at 03:29
  • yes but then you wont be able to use it from url, for that you'll have to import – bool.dev Jan 04 '13 at 03:30
  • see update to use controller both from url and as a base class – bool.dev Jan 04 '13 at 03:37
  • 1
    This has to be one of the most aggravating things about Yii. – Andrew Ellis Aug 28 '13 at 20:59
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.

jlordo
  • 37,490
  • 6
  • 58
  • 83