vendredi 3 août 2018

What's the correct way to run a website and an API from the same domain/subdomain combination

I am just starting up with Laravel so it's possible that I haven't read through some docs which highlight how possible this is, but here is the requirement.

I have a website, let's call it https://www.foo.com , serving HTML to it's visitors.

While a subdomain of it https://api.foo.com is supposed to serve a RESTful API.

I am on a GoDaddy hosting and if I create the subdomain, the file structure will look something like this:

root/ 
├── website_code/  <- https://www.foo.com
├── ...
├── 
├── api/ <- https://api.foo.com
│   ├── ...
│   ├── 
│   └── ...
└── ...

Both will access the same database, and will serve same information, only their format will be different. The API will not need a view, while the HTML version will inject the fetched values into views.

What's the best way to achieve this using Laravel, bearing in mind that:

  • The API plans to support OAuth2 and Password Grant type authentication using Passport
  • The API may see multiple different version changes in future, with no version being the default choice

Should I run two different Laravel instances under each folder?
Or should I use something like acceptsJSON() to ascertain if the Accept: header has been set to JSON, and consider it an API request.

Maintaining api.foo.com is kind of important for the SEO purposes, but if there's no easy way available, I can look at https://www.foo.com/api/ format too.

Please suggest.



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire