0

I've been recently learning about REST APIs (concretely using MySQL and PHP) so it's hard for me to understand basic concepts since most of the sites I check have more advanced solutions.

My doubt is the following: I know that an endpoint is the place where we get the data, but I'm not sure about the URI format, sometimes I've seen it as a php file path and other times it doesn't have an extension, but I don't know which one is correct, or if both are.

  1. In case the URI format has no file extension (like performing a GET in the endpoint /api/update), do I forcefully have to have a "controller" file to which all the URIs are redirected and treated depending on the case, or is there a better way to handle them?

  2. I've also been asked to make a script to run a backend app and a frontend and backend app, as in a script that will execute everything when launched (calling other scripts if necessary and so on) but I don't know what they mean exactly by that or how to do it. I thought that having a index.php (for example) in which you can have a couple of buttons that would trigger the requests was already it but it's not, so what is it exactly?

Sorry for the basic questions but I've looked many solutions in here and other websites and I still can't grasp the concept.

Thanks in advance.

Jack M.
  • 51
  • 7
  • 1
    there is a bunch of resources if you look, imagine a "thing" call it car, then you have an endpoint for `/api/car` with GET (to get cars), POST (to add a car) then, `/api/car/:id` with GET (get single car), PUT (update car) and DELETE. each with own handlers/controllers which invoke and call car specific models etc, then it leaves space for `/api/car/:id/orders` and that in turn would repeat the GET, PUT, `/api/car/:id/orders/:order_id` etc as far nested as it needs. What you don't do is `/api/update` and put all the logic for updating all things in there. – Lawrence Cherone Mar 29 '21 at 21:06
  • @LawrenceCherone thank you for the response, I did hear about that way of using endpoints, though it seems they use endpoints like /api/car/update_car are widely used as a bad practice, I'll take it into account. – Jack M. Mar 30 '21 at 00:30

1 Answers1

2
  1. The answer to this question is yes. Common practice is to have a PHP application use the front controller pattern, where a single publicly available script (usually index.php) is solely responsible for delegating all incoming requests to the appropriate part of your application (your actual "controllers"), often relying on server configuration to do the actual "redirecting" (rewriting, allowing for omitted file extensions and "pretty url's"). This design approach is common because most popular frameworks support it out of the box, from the Laravel and Symfony big boys, to microframeworks like Slim, Silex and Lumen. Perhaps giving these frameworks a try will help you understand how this works and how they do it.

  2. Not sure if I understand your question correctly, but it sounds like you are being asked to provide/implement a deployment script; a script that runs a set of commands in order to easily install, bootstrap and start the entire application. Think of commands like composer install, commands that initialize/seed the database, or commands that build your frontend assets. The actual commands are specific to the application, but the goal is to easily provide a fresh installation and deployment of your application by executing a single script. These scrips are usually sh scripts executed from the command line.

  • Thank you very much, that was exactly what I was looking for and the links for the 1rst question are useful. Regarding the 2nd question, what do you mean exactly by 'build frontend assets'? like building or initialize a table with data? I've never made a script like that nor used Composer so I'll look up and see if I get the basics of it. Thanks again for the input. – Jack M. Mar 30 '21 at 00:37
  • Building _frontend assets_ means using tools like [npm](https://docs.npmjs.com/about-npm) and [webpack](https://webpack.js.org/concepts/) to neatly package and optimize your Javascript, HTML and CSS source code, amongst other things. This was just an example of a common _deployment script_ use case though, and perhaps might not apply to your situation. What's important is that this _deployment script_ makes it easy to install, build and start your application, executing commands that you would otherwise have to run manually. – Jeroen van der Laan Mar 30 '21 at 19:17
  • Just checked again the front and back end exercice and it said to deliver "a single powershell or bash script to build and launch both applications", that it can call other scripts and have any structure and I can assume all ports are available, is that the deployment script you're describing? – Jack M. Mar 31 '21 at 02:08
  • Yes, pretty much! – Jeroen van der Laan Mar 31 '21 at 09:22