extending controller


I just wanted to check I had the right approach here. I am trying to make all my existing controllers extend my own super controller. From the guide I see that I need something like this. But where should I be putting this file?

namespace app\controllers;

use yii\web\Controller;

class MyController extends Controller



Where should I put this file though?

I usualy create a folder named components and put it in there. But you can place it anywhere you like.

Don’t forget to adjust the namespace in your file too.

// This is what you have right now.

// It means the path of MyController is app/controllers/MyController.php

namespace app\controllers; 

// And this means it's stored in app/components/MyController.php

namespace app\components; 

Also since Yii2 uses namespace, you don’t need to use a different name like ‘MyController’. You can name it ‘Controller’ and it won’t cause a name conflict because it is not in the same namespace.

Thanks Alain

Which directory would you suggest? In the Document where the config, controller,model folders are?

Well say your project is called myApp, your directory structure would look a bit like this:


-- assets

---- ...

-- components // this (remember, you can name it whatever you like, that's just what I chose)

---- Controller.php // and this (again, you can name it whatever you like).

---- ...

-- config

---- main.php

---- ...

-- controllers

---- SiteController.php


---- SignupForm.php

---- ...

-- runtime

---- ...

-- views

---- index.php

---- ...

-- web

---- ...