Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can create BaseController extends Controller in Yii 2

Tags:

yii2

As title, I would create custom controller and overwrite core controller in Yii 2, and bellow is my code. /create BaseController, I put this file in root/components./

namespace yii\base;
use Yii;
class BaseController extends \Controller{
    public function init() {
        parent::init();
    }
}

/* Extends BaseController.*/
namespace app\components;
use Yii;
class UsersController extends \BaseController
{
    /* more function is here.*/
    public actionIndex(){
        echo _FUNCTION_;
    }
}

I change more way, but this not works, plz help me. Thanks all.

like image 715
Nguyễn Thành Bồi Avatar asked Dec 13 '14 17:12

Nguyễn Thành Bồi


1 Answers

You should read about namespaces in PHP first, then learn a bit about Yii 2 style of using namespaces and how it's organized in the specific application (basic / advanced) you are using.

What kind of functionality do you want to add to your controller? Most of the time it's better to override the specific controller (for example for web it will be yii\web\Controller) and not the base class.

Assuming you are using the 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 UserController is extending your custom BaseController. If you make all of your app's controllers extend BaseController, you can have the same features/functions across all of your app's controllers.

Why? Say you want your entire frontend to be login required. Normally, you have to manually modify the rules in each one of your controllers. You could declare the rules in BaseController to make everything require login, and exclude login, error, signup, and any other pages you need to allow public access to.

Something else for newcomers to Yii2 should know. In the "advanced" template, you actually have multiple apps. "frontend" and "backend" are their own app. You can actually copy the "frontend" (or "backend") directory and name it something like "mainsite" and have a 3rd app (just search and rename all instances of "frontend" to "mainsite". In the "environments" directory, you can copy frontend, name it "mainsite" and modify it to fit your needs, so it's files can be merged via init if needed. You do need to also edit environments/index.php to add your own init environment.

"console" is actually an app too, but not for web access, but for access via the command line, typically for your own purposes like handling cron jobs or pruning old data. Maybe you offer web hosting, in "console" is where you could add code to create them their hosting account. I rarely use console, but it can be useful.

mainly used to create background and maintenance tasks that need to be performed for a website.

The last thing I want to mention, is that you can create your own app however you want! The Yii2 framework isn't your basic or advanced app, it is actually inside your vendor directory (installed via Composer) :) The files you are working with, are actually just Yii's way of laying things out for you. Follow what they do, and you can create your own file structure however you want. You could scrap it, and create your own Yii app from the ground up (not using advanced or basic at all!). Don't be constricted to basic or advanced!

like image 53
arogachev Avatar answered Oct 21 '22 13:10

arogachev