>  기사  >  백엔드 개발  >  如何给 controller 或 action 附加属性?

如何给 controller 或 action 附加属性?

WBOY
WBOY원래의
2016-06-06 20:41:02902검색

在web开发过程中,有没有一种方式给 controller 或 action 附加一种属性,类似给 controller 或 action 打上不同的 tag,用于区分不同的类型,如下:

<code>/*
 * @Tags("All")
 */
class UserController extends Controller {
    /*
     * @Tags("Read")
     */
    public function index() {
    }

    /*
     * @Tags("Write")
     */
    public function add() {
    }

    /*
     * @Tags("Write")
     */
    public function foo() {
    }
}
</code>

回复内容:

在web开发过程中,有没有一种方式给 controller 或 action 附加一种属性,类似给 controller 或 action 打上不同的 tag,用于区分不同的类型,如下:

<code>/*
 * @Tags("All")
 */
class UserController extends Controller {
    /*
     * @Tags("Read")
     */
    public function index() {
    }

    /*
     * @Tags("Write")
     */
    public function add() {
    }

    /*
     * @Tags("Write")
     */
    public function foo() {
    }
}
</code>

撸 C# 不? 里面有 Attribute 可以给成员进行标注。
python 也可以用装饰器来实现。
其他的就不熟了。


UPDATE: PHP 可以通过反射拿到注释,黑魔法……
ReflectionClass::getDocComment
Faking method attributes in PHP?

Java有annotation,问题在于你想怎么利用这些tag?

<code>@All //也可以@Tags("All")
class UserController extends Controller {
    @Read
    public Object index() {
    }

    @Write
    public Object add() {
    }

    @Write
    public Object foo() {
    }
}
</code>
성명:
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.