4 ms·
I think most other languages and frameworks work via a more declarative API - "if a GET request to /this comes in, return that". I had to look it up / remind my
by Cthulhu_ 7y ago
I think most other languages and frameworks work via a more declarative API - "if a GET request to /this comes in, return that". I had to look it up / remind myself how it works in PHP as well the other day, and it's a bit more involved:
- Create a .htaccess file that rewrites /api/x/y/z to /api/index.php/x/y/z
- Read $_SERVER['PATH_INFO'] for the /x/y/z
- Read $_SERVER['REQUEST_INFO'] for the HTTP method (GET/POST/etc)
- Create the response starting with some calls to `header()`
- 'echo json_encode($resp);'
I'm sure all that is abstracted away in e.g Laravel though. The PHP standard library could also do with an overhaul for modern applications for requirements like this though.
- fraktl 7y ago- $_SERVER contains server and execution environment info - $_REQUEST contains request input parameter values (regardless of http verb) That's it. It's not a lot. They're 2 super simple arrays. PHP can also work from command line where $_REQUEST/$_POST/$_GET are unavailable. You can also entirely ignore the existence of the above variables. You can read from standard input and treat input as JSON - options are endless. This merely means that the example shown is not entirely correct - it's not "how it works in PHP". It's merely one of the ways. But the real problem is: no one bothered to research it and you blindly assumed that's how it works. Laravel (Http/Kernel library) standardized the interface towards the dev and that's great. But the input itself isn't as tied to PHP core as your comment makes it seem.