How to print url path without parameters?

王林
Release: 2024-02-11 18:57:23
forward
1157 people have browsed it

How to print url path without parameters?

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!

Question content

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)
Copy after login

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?

Solution

In the createproject function you can use this:

func CreateProject(w http.ResponseWriter, r *http.Request) {
    path, _ := mux.CurrentRoute(r).GetPathTemplate()
    fmt.Println(path)
}
Copy after login

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!

source:stackoverflow.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!