Jump to content
Karl_T

Template for API endpoint - Remove the language part in URL

Recommended Posts

I would like to make a template for API Endpoint, i.e. www.domain.com/api/.

The issue is that most of my sites are multi-lang. The URL will become like www.domain.com/en/api/ or www.domain.com/de/api/, which make some issues when calling ajax from the frontend. What I want to achieve is to remove the language part of the URL, which is exactly the www.domain.com/processwire/ looks like. The admin URL does not contain the language part. I look into the Admin's edit (The admin root), it does have the language name enabled, but its children, e.g. Admin>Pages, all the language names are showing /processwire/pages, instead of /en/processwire/pages/. I want to replicate this to /api/ and its children pages, which are also api endpoint like /api/login/.

I have tried to disable the template multi-language support, but, unfortunately, the /en/ part is still there.

Share this post


Link to post
Share on other sites
17 minutes ago, Karl_T said:

I would like to make a template for API Endpoint, i.e. www.domain.com/api/.

How is your API setup? Do you have this one single endpoint to retrieve everything?

You could allow URL segments in your home (root) template (if you need just one, define only one and name it "api"). Then you would have to include some logic in your home.php template to handle the API requests.

if($input->urlSegment1 == 'api') {
  // ...
} else {
  // regular display of homepage
}

https://processwire.com/docs/front-end/how-to-use-url-segments/

  • Thanks 1

Share this post


Link to post
Share on other sites
2 minutes ago, dragan said:

How is your API setup? Do you have this one single endpoint to retrieve everything?

I want multiple api endpoint setups and have their own templates. I want to use them by some custom modules, each having their own endpoint under /api/. And I don't want to mess with other templates. Thanks for your suggestion!

Share this post


Link to post
Share on other sites

I found the solution.

By amending this line in LanguageSupportPageNames module, we can set any template to bypass the language segment of the URL. It would be great if this can be editable in the module setting. Right now, we can just copy the module to /site/ and override the module inside /wire/. Hope this help somebody looking for the same api approach for multilingual sites.

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


  • Recently Browsing   0 members

    No registered users viewing this page.

×
×
  • Create New...