search

Home  >  Q&A  >  body text

php - 如何给 controller 或 action 附加属性?

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

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

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

    /*
     * @Tags("Write")
     */
    public function foo() {
    }
}
阿神阿神2902 days ago338

reply all(2)I'll reply

  • 巴扎黑

    巴扎黑2017-04-10 15:10:25

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


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

    reply
    0
  • 怪我咯

    怪我咯2017-04-10 15:10:25

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

    @All //也可以@Tags("All")
    class UserController extends Controller {
        @Read
        public Object index() {
        }
    
        @Write
        public Object add() {
        }
    
        @Write
        public Object foo() {
        }
    }
    

    reply
    0
  • Cancelreply