php editor Strawberry is here to introduce how to print the url path without parameters. In web development, we often need to obtain the URL of the current page and remove the parameter part, leaving only the path part. The advantage of this is that it can make the URL more concise, and it is also beneficial to SEO optimization. In PHP, we can implement this function through some simple code. Next, I will introduce the specific implementation method in detail. Let’s learn together!
I have a restful api using gorilla/mux. It is parameterized in the path. I create it like this:
r := mux.NewRouter() subr := r.PathPrefix("/v1").Subrouter() subr.Handle("/organizations/{orgId}/projects/{projectId}", CreateProject()).Methods(http.MethodPost)
However, when capturing the request and logging the results, I don't want to log, i.e.
/organization/fff-555-aaa9999/projects/amazing-project
This is the result I get by parsing the r *http.request.url
value.
Instead, I want to get the initial router value for /organizations/{orgid}/projects/{projectid}
so that I can filter that in my logging.
Using gorilla/mux, is there a way to get the router path without the actual parameters, but instead get the parameter variables?
In the createproject
function you can use this:
func CreateProject(w http.ResponseWriter, r *http.Request) { path, _ := mux.CurrentRoute(r).GetPathTemplate() fmt.Println(path) }
The above is the detailed content of How to print url path without parameters?. For more information, please follow other related articles on the PHP Chinese website!