如何在Yii 2中创建BaseController扩展控制器?

时间:2022-10-05 09:39:31

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./

作为标题,我将在Yii 2中创建自定义控制器并覆盖核心控制器,下面是我的代码。创建BaseController,我将这个文件放在根/组件中。

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.

我改变更多的方式,但这不行,请帮助我。谢谢所有。

1 个解决方案

#1


16  

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.

您应该首先阅读PHP中的名称空间,然后了解一下使用名称空间的Yii 2风格,以及如何在正在使用的特定应用程序(基本/高级)中组织名称空间。

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.

您希望向控制器添加什么功能?大多数时候,最好重写特定的控制器(例如,web将是yii\web\ controller),而不是基类。

Assuming you are using the basic application, the code should look something like this:

假设您正在使用基本的应用程序,那么代码应该如下所示:

BaseController

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.

注意UserController是如何扩展您的自定义BaseController的。如果让应用程序的所有控制器扩展BaseController,则可以在所有应用程序的控制器上拥有相同的功能。

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.

为什么?假设你想要你的整个前端都需要登录。通常,您必须手动修改每个控制器中的规则。您可以在BaseController中声明规则,使所有内容都需要登录,并排除登录、错误、注册和任何其他允许公共访问的页面。

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.

Yii2的新手应该知道一些其他的事情。在“高级”模板中,实际上有多个应用程序。“前端”和“后端”是他们自己的应用程序。你可以复制“前端”(或“后端”)目录,给它起个类似“主站点”的名字,并有第三个应用程序(搜索并重命名“前端”的所有实例到“主站点”。在“environment”目录中,您可以复制frontend,将其命名为“mainsite”,并根据需要修改它,以便在需要时可以通过init合并它的文件。您还需要编辑环境/索引。php来添加您自己的init环境。

"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.

“控制台”实际上也是一个应用程序,但不是用于web访问,而是用于通过命令行访问,通常用于您自己的目的,如处理cron作业或删除旧数据。也许你可以提供网络托管,在“控制台”你可以添加代码来创建他们的托管帐户。我很少使用控制台,但是它很有用。

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!

我最后想说的是,你可以随心所欲地创建你自己的应用!Yii2框架不是您的基本或高级应用程序,它实际上位于您的供应商目录(通过Composer安装):)中。按照他们的做法,您可以创建自己的文件结构,但是您需要。你可以放弃它,然后从头开始创建你自己的Yii应用程序(不使用高级的或基本的!)不要拘泥于基本或高级!

#1


16  

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.

您应该首先阅读PHP中的名称空间,然后了解一下使用名称空间的Yii 2风格,以及如何在正在使用的特定应用程序(基本/高级)中组织名称空间。

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.

您希望向控制器添加什么功能?大多数时候,最好重写特定的控制器(例如,web将是yii\web\ controller),而不是基类。

Assuming you are using the basic application, the code should look something like this:

假设您正在使用基本的应用程序,那么代码应该如下所示:

BaseController

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.

注意UserController是如何扩展您的自定义BaseController的。如果让应用程序的所有控制器扩展BaseController,则可以在所有应用程序的控制器上拥有相同的功能。

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.

为什么?假设你想要你的整个前端都需要登录。通常,您必须手动修改每个控制器中的规则。您可以在BaseController中声明规则,使所有内容都需要登录,并排除登录、错误、注册和任何其他允许公共访问的页面。

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.

Yii2的新手应该知道一些其他的事情。在“高级”模板中,实际上有多个应用程序。“前端”和“后端”是他们自己的应用程序。你可以复制“前端”(或“后端”)目录,给它起个类似“主站点”的名字,并有第三个应用程序(搜索并重命名“前端”的所有实例到“主站点”。在“environment”目录中,您可以复制frontend,将其命名为“mainsite”,并根据需要修改它,以便在需要时可以通过init合并它的文件。您还需要编辑环境/索引。php来添加您自己的init环境。

"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.

“控制台”实际上也是一个应用程序,但不是用于web访问,而是用于通过命令行访问,通常用于您自己的目的,如处理cron作业或删除旧数据。也许你可以提供网络托管,在“控制台”你可以添加代码来创建他们的托管帐户。我很少使用控制台,但是它很有用。

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!

我最后想说的是,你可以随心所欲地创建你自己的应用!Yii2框架不是您的基本或高级应用程序,它实际上位于您的供应商目录(通过Composer安装):)中。按照他们的做法,您可以创建自己的文件结构,但是您需要。你可以放弃它,然后从头开始创建你自己的Yii应用程序(不使用高级的或基本的!)不要拘泥于基本或高级!