Slim is a PHP micro framework that helps you quickly write simple yet powerful web applications and APIs. (Definition on its homepage http://www.slimframework.com/)
Here you can have a taste of it :)
I just got introduced to Slim Framework recently, and speaking frankly it is not a good experience working with it and the poor community is nothing like npmjs, which is a place where I hang around most of the time. All the tutorials online are mostly theory-based and really not organized, some of them don’t even run properly.
Hence, it will be great to do an example combining Slim with commonly known Jquery and Python to do a quick full stack web app, where you can run it immediately when thrown into XAMPP’s htdoc directory.
Let us make a very simple application where the HTML page is hosted for clients to input their name, then Slim takes the JSON api and call python to save the name onto a file called ‘user.log’. Nothing complicated!
under htdocs/public folder, we add ‘.htaccess’ and copy following code into it for server-side cross access
This is the easiest part, everyone knows how to write it, so i’m just gonna go ahead and write the code.
Within the HTML file we have written a function to parse form input and send the name to the SLIM whose router is ‘/savename’ by JSON.
Now we need to go into that ‘.php’ file and add handler function to handle requests at ‘/savename’ context.
The code handles any request at ‘/savename’ context, and parse the request by
$request->getQueryParams(), afterwards it returns a response to user through
$response->write to let the user know it has received the message.
Now we have a working HTML and Request Handler, we need a script to provide actually data saving and fetching as a database. Usually this is done via mySQL which is supported by XAMPP originally. To illustrate flexibility of Slim, we will use Python and .log text file to work as Database.
First we need a properly working python file that takes in an argument of String name and append it to .log file.
And we can initialize our user.log file with either null or random strings:
To put pieces together, we need call this python file in Slim’s php code. Easy:
And our index.php file now becomes
NOW CONGRATS, YOU HAVE GOT A SLIM APP RUNNING