Yii - Inheriting From Custom Controller Class - Not Found
Asked Answered
D

2

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

Deserved answered 4/1, 2013 at 2:56 Comment(0)
F
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.

Fourierism answered 4/1, 2013 at 3:8 Comment(6)
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...Deserved
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?Fourierism
Sorry it looks like the base class is meant to go in /components/ - I wasn't reading the ebook properly.Deserved
yes but then you wont be able to use it from url, for that you'll have to importFourierism
see update to use controller both from url and as a base classFourierism
This has to be one of the most aggravating things about Yii.Rameses
D
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.

Dissever answered 11/1, 2013 at 14:58 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.