如何在 Yii 2 中创建 BaseController 扩展控制器 [英] How can create BaseController extends Controller in Yii 2

查看:36
本文介绍了如何在 Yii 2 中创建 BaseController 扩展控制器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

推荐答案

你应该阅读PHP 中的命名空间,然后了解使用命名空间的 Yii 2 风格以及它在您正在使用的特定应用程序(基本/高级)中的组织方式.

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.

您想为控制器添加什么样的功能?大多数时候最好覆盖特定的控制器(例如对于 web,它将是 yii\web\Controller) 而不是基类.

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();
    }
}

用户控制器

namespace app\controllers;    

class UserController extends \app\components\BaseController
{
    public actionIndex()
    {
        // ...
    }
}

注意 UserController 如何扩展您的自定义 BaseController.如果您让所有应用的控制器都扩展BaseController,则您可以在所有应用的控制器中拥有相同的特性/功能.

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.

为什么? 假设您希望您的整个 frontend 需要登录.通常,您必须手动修改每个控制器中的规则.你可以BaseController中声明规则,让一切都需要登录,并排除loginerror注册,以及您需要允许公开访问的任何其他页面.

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.

Yii2 新手应该知道的其他内容.在高级"模板中,您实际上有多个应用程序.前端"和后端"是他们自己的应用程序.您实际上可以复制前端"(或后端")目录并将其命名为mainsite"并拥有第三个应用程序(只需搜索前端"的所有实例并将其重命名为mainsite".在环境"目录中,您可以复制前端,将其命名为mainsite"并对其进行修改以满足您的需要,因此如果需要,它的文件可以通过 init 合并.您还需要编辑 environments/index.html .php 添加自己的init环境.

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.

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

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

我想说的最后一件事是,您可以随心所欲地创建自己的应用!Yii2 框架不是您的 basicadvanced 应用程序,它实际上位于您的 vendor 目录中(通过 Composer 安装):) 您的文件正在使用,实际上只是 Yii 为您安排事情的方式.按照他们的做法,您可以根据需要创建自己的文件结构.你可以废弃它,从头开始创建你自己的 Yii 应用程序(根本不使用高级或基本!).不要局限于basicadvanced

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!

这篇关于如何在 Yii 2 中创建 BaseController 扩展控制器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆