基本介绍

WebServer提供服务需要方法/对象的支持,ghttp包支持多种路由注册模式,为开发者提供非常强大和灵活的接口功能。

路由注册是整个WebServer最核心的部分,也是goframe框架中最精心设计的一个模块。

接口文档: https://pkg.go.dev/github.com/gogf/gf/v2/net/ghttp

注意事项

从框架v2版本开始,增加了规范路由的特性,更符合业务项目工程化的场景,推荐在复杂的业务项目中使用规范路由:路由注册-规范路由

相关内容


Content Menu

  • No labels

5 Comments

  1. 请教一下 有没有类似自定义404 page的处理函数, 类似ginrouter

        // 未知路由处理
        router.NoRoute(func(context *gin.Context) {
            context.String(http.StatusNotFound, "Not router")
        })
    
        // 不支持调用方式
        router.NoMethod(func(context *gin.Context) {
            context.String(http.StatusNotImplemented, "Not method")
        })
  2. v2的ctx中包含了request对象,那原先controller方式注册路由时func(r *ghttp.Request)的入参r是否应该改成func(ctx context.Context)

    1. 文档中很多还是V1沿用下来的,V2需要兼容V1版本,保留了func(ctx context.Context),但同时增加了新的signature,在源码中checkAndCreateFuncInfo方法可以看到

      func (s *Server) checkAndCreateFuncInfo(f interface{}, pkgPath, structName, methodName string) (info handlerFuncInfo, err error) {
      	handlerFunc, ok := f.(HandlerFunc)
      	if !ok {
      		reflectType := reflect.TypeOf(f)
      		if reflectType.NumIn() != 2 || reflectType.NumOut() != 2 {
      			if pkgPath != "" {
      				err = gerror.NewCodef(
      					gcode.CodeInvalidParameter,
      					`invalid handler: %s.%s.%s defined as "%s", but "func(*ghttp.Request)" or "func(context.Context, BizRequest)(BizResponse, error)" is required`,
      					pkgPath, structName, methodName, reflect.TypeOf(f).String(),
      				)
      			} else {
      				err = gerror.NewCodef(
      					gcode.CodeInvalidParameter,
      					`invalid handler: defined as "%s", but "func(*ghttp.Request)" or "func(context.Context, BizRequest)(BizResponse, error)" is required`,
      					reflect.TypeOf(f).String(),
      				)
      			}
      			return
      		}
      	// omit the rest of the code here...
      }

      这块希望在未来的文档中可以体现出来



  3. z

    有没有像beego的注解路由呢