PHP on a diet : Up and running with Slim
It has been said that if there are n
PHP developers in the world then there are n+1
PHP frameworks. While that is most likely undeniably true, I have been perusing the so-called micro frameworks lately of which most are inspired (at least in part) by sinatra. I tend to enjoy working with the micro frameworks as they typically don't get in your way but just gently get you going. They don't force a whole stack on you as you generally select your best components (view engine, datastore, etc) and piece them together. For me it helps to quickly gain a good understanding of the framework and its inner workings and makes it easier to go through its source since its not trying to be everything to everyone. On the Slim homepage it even says, and I believe it to be true so far...
"The Slim micro framework is everything you need and nothing you don't."
Up until recently getting a PHP site running locally required the use of apache (using wammp/xampp), nginx or some other local webserver. With the release of 5.4 a development server is now included in PHP and makes using it locally a breeze. Even though it was introduced in 2010 I recently came across the Slim Framework. This post will quickly get you up and running with Slim using composer and the new internal development server.
mkdir slimapp
cd slimapp
curl -s http://getcomposer.org/installer | php
Next create a composer.json
file in the web root that indicates your dependency on Slim:
{
"require": {"slim/slim": "1.6.*"}
}
Next we kick off the install of Slim via composer and start the development server:
php composer.phar install
php -S 127.0.0.1:80
There is a nice feature in the PHP 5.4+ development server that helps us out when using friendly urls.
If a URI request does not specify a file, then either index.php or index.html in the given directory are returned.
As long as we use friendly urls and name our bootstrap file index.php
then we can start the development server in our web root and it will all just work. For those of you who want to use app.php
, router.php
or something else, it also has the ability to accept another command line parameter to act as a router script. You can read more about it here.
Finally, create an index.php
with the following contents:
<?
require 'vendor/autoload.php';
$app = new Slim();
$app->get('/', function () {
echo "Hello World!";
});
$app->run();
Then goto http://127.0.0.1
in your browser and you should see Hello World!
.
You can expect more from me on using Slim and you can read more about this micro framework here.