可以在 Yii2 中更改操作类吗? [英] Possible to change action class within Yii2?

查看:39
本文介绍了可以在 Yii2 中更改操作类吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以更改动作类 Yii2 以某种方式使用,类似于如何在配置文件中设置许多其他组件的类?

Is it possible to change the action class Yii2 uses somehow, similar to how you can set the class of many other components within the config file?

我想扩展这个类,以便我可以向它添加另一个成员变量.

I want to extend this class so I can add another member variable to it.

我想我无论如何都可以动态地向其中添加一个,但更愿意以适当的方式进行.

I guess I could just add one to it anyway dynamically, but would prefer to do it in a proper fashion.

查看核心应用程序组件 它没有列出,所以不确定是否可能?

Looking at the list of core application components it isn't listed, so not sure if it's possible?

推荐答案

解决这个问题的正确方法是扩展控制器和动作类.如果你查看源代码,yii\base\Controller 有一个 createAction 方法,如果没有找到类动作,它将创建一个 InlineAction.

The proper way to solve this problem is to extend both controller and action classes. If you look at the source code, yii\base\Controller has a createAction method that, if no class action is found, will create an instance of InlineAction.

由于每次创建自己的控制器时都会扩展某种控制器类(class MyController extends Controller),因此您只需覆盖原始的 createAction 方法和其中使用您自己的 InlineAction 类实现.

Since you're extending some kind of controller class every time you make your own controller (class MyController extends Controller), you can just override the original createAction method and in it use your own implementation of the InlineAction class.

这篇关于可以在 Yii2 中更改操作类吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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