First you should read about namespaces in PHP , and then learn a little about the Yii 2 style using namespaces and how it is organized in the particular application (main / advanced) that you use.
What functionality do you want to add to your controller? In most cases, it is better to override a specific controller (for example, for the Internet it will be yii \ web \ Controller ) rather than the base class.
Assuming you are using a basic application, the code should look something like this:
Basecontroller
namespace app\components; class BaseController extends \yii\web\Controller { public function init() { parent::init(); } }
Usercontroller
namespace app\controllers; class UserController extends \app\components\BaseController { public actionIndex() {
Notice how the UserController extends the custom BaseController . If you have added all BaseController application BaseController , you can have the same functions / functions for all your application controllers.
Why? Say you want your entire frontend to be needed to log in. Typically, you need to manually change the rules in each of your controllers. You can declare rules in BaseController so that everything requires login and excludes login , error , signup and any other pages that need to be allowed public access.
Something else for beginners Yii2 should know. In the "advanced" template, you actually have several applications. "frontend" and "backend" are your application. In fact, you can copy the frontend directory (or backend) and name it mainsite and have a third application (just search and rename all instances of frontend to mainsite. In the environment directory, you can copy the interface name it "mainsite" and change it to suit your needs, so you can merge the files with init if necessary.You also need to edit environments/index.php to add your own init environment.
the “console” is actually also an application, but not for web access, but for access via the command line, usually for your purposes, such as processing cron jobs or trimming old data. Perhaps you offer web hosting, in the "console" you can add code to create your hosting account. I rarely use the console, but it can be useful.
It is mainly used to create background and service tasks that need to be completed for a website.
The last thing I want to mention is that you can create your own application , but you want to! The Yii2 framework is not your basic or advanced application, it is actually located in your vendor directory (installed through Composer) :) The files you work with are actually just Yii, a way to get things done for you. Follow what they do. and you can create your own file structure as you wish. You can abandon it and create your Yii application from scratch (without using advanced or basic at all!). Do not narrow down with basic or advanced !