Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to use HandleMany in mvc #1292

Closed
gongchao opened this issue Jul 3, 2019 · 5 comments
Closed

How to use HandleMany in mvc #1292

gongchao opened this issue Jul 3, 2019 · 5 comments

Comments

@gongchao
Copy link
Contributor

gongchao commented Jul 3, 2019

type Package struct {
}

func (*Package) BeforeActivation(b mvc.BeforeActivation) {
	b.HandleMany("GET", "/{package:string} /{scope:string}/{package:string}", "MyHandle")
}
@gongchao
Copy link
Contributor Author

gongchao commented Jul 3, 2019

How should I achieve this?

@kataras
Copy link
Owner

kataras commented Jul 3, 2019

You can create two methods that calls your MyHandle, I don't think the HandleMany is necessary, at the opposite I think it will make it very difficult to debug it on the future, currently each handler is one controller method that their information are logged on debug logger mode. MVC is featured enough - no need to add more methods or coblexity there, however if you give me some arguments why you need this instead of a simple solution to make two methods and call the shared function inside them I'll rethink it, want to help you but in the same time to protect our apps by ourselves.

@gongchao
Copy link
Contributor Author

gongchao commented Jul 3, 2019

func (*C) BeforeActivation(b mvc.BeforeActivation) {
	b.Handle("GET", "/test1", "Get")
	b.Handle("GET", "/test2", "Get")
}

func (*C) Get(ctx iris.Context) {
	ctx.WriteString("context")
}

I use it like this, second route cannot match the handle. @kataras

@kataras
Copy link
Owner

kataras commented Jul 11, 2019

Registering the same controller method twice is not allowed by purpose, it's not a bug:

iris/mvc/controller.go

Lines 78 to 81 in 6c054a9

// the already-registered routes, key = the controller's function name.
// End-devs can change some properties of the *Route on the `BeforeActivator` by using the
// `GetRoute(functionName)`. It's a shield against duplications as well.
routes map[string]*router.Route

iris/mvc/controller.go

Lines 315 to 318 in 6c054a9

// add this as a reserved method name in order to
// be sure that the same func will not be registered again,
// even if a custom .Handle later on.
c.routes[funcName] = route

Why? because you can get a route based on the controller method through:
mvc.BeforeActivation/AfterActivation.GetRoute(controllerMethodName) iris.Route and because you can override a method's route as you want to do. The problem here is that you want to override existing method and adding more at the same time with the same Handle call, you get it.

Instead, this I purposed to you above was something like this:

func (*C) GetTest1(ctx iris.Context) {
	ctx.WriteString("context")
}

func (*C) GetTest2(ctx iris.Context) {
	c.GetTest1(ctx)
}

If we allow more than one routes to registered to one controller method then the API of BeforeActivation/AfterActivation.GetRoute(controllerMethodName) iris.Route must change to return a slice of []Route instead of a single instance or make a new one GetRoutes(controllerMethodName) []iris.Route. I am working on this for the v11.2, although I don't think it's necessary but if you need it so much, I have the obligation to implement it. Thanks @gongchao

@kataras
Copy link
Owner

kataras commented Jul 11, 2019

It's done on v11.2 without breaking changes with commits:

Example:

func (c *basicController) BeforeActivation(b mvc.BeforeActivation) {
b.HandleMany("GET", "/custom /custom2", "Custom")
}

This is also possible:

type Package struct {
    Ctx iris.Context
}

func (*Package) BeforeActivation(b mvc.BeforeActivation) {
    b.HandleMany("GET", " /{scope:string}/{package:string} /{package:string}", "MyHandle")
}

func (*Package) MyHandle(pkg, scope string) string {
    return pkg+scope
}

on GET: /mypackage/myscope it will response with mypackagemyscope - on GET: /mypackage it will response with mypackage.

Thanks @gongchao.

@kataras kataras added this to the v11.2.0 milestone Jul 11, 2019
kataras added a commit that referenced this issue Jul 11, 2019
…ng} /custom3/{ps:string}, CtrlMethodName) relative to: #1292
@kataras kataras closed this as completed Jul 23, 2019
github-actions bot pushed a commit to goproxies/github.com-kataras-iris that referenced this issue Jul 27, 2020
…ested at: kataras#1292

Former-commit-id: c021f49522a214ddc3978a1c8f5e8d32f029eb2d
github-actions bot pushed a commit to goproxies/github.com-kataras-iris that referenced this issue Jul 27, 2020
…ng} /custom3/{ps:string}, CtrlMethodName) relative to: kataras#1292

Former-commit-id: de08c5eeab7a1c2729fbff7260de00cc2516f78c
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants