Saturday, June 1, 2013

Install SLIM for PHP in a single stroke...

I use SLIM for my web service for a personal project that I have been working on. It works great, but I forget the steps to install it because I revisit this project every 3 months or so. The BASH script below downloads the slim framework, writes the initial index.php file and sets up the .htaccess file. I run this from the root of my website directory.
 #!/bin/sh  
 #  
 #This installs the SLIM framework in a single command.  
 WEBSERVICE_ROOT=api  
 WEBSERVICE_FILE=index.php  
 echo "installing SLIM framework and dependencies in ./$WEBSERVICE_ROOT"  
 if [ -d $WEBSERVICE_ROOT ]  
 then  
      echo "api exists... slipping creation"  
 else  
      mkdir $WEBSERVICE_ROOT  
 fi  
 echo "{\"require\": {\"slim/slim\": \"2.*\"}}" > $WEBSERVICE_ROOT/composer.json  
 cd $WEBSERVICE_ROOT > /dev/null ;  
 curl -s https://getcomposer.org/installer | php -d detect_unicode=Off  
 php composer.phar install  
 echo "RewriteEngine On\nRewriteCond %{REQUEST_FILENAME} !-f\nRewriteRule ^ $WEBSERVICE_FILE [QSA,L]" > .htaccess  
 if [ -f $WEBSERVICE_FILE ]  
 then  
   echo "$WEBSERVICE_FILE exists\n"  
 else  
      echo "<?php\nrequire 'vendor/autoload.php';\n" >> $WEBSERVICE_FILE  
      echo "\$app = new \Slim\Slim();\n" >> $WEBSERVICE_FILE  
      echo "\$app->get('/hello/:name', function (\$name) {\n" >> $WEBSERVICE_FILE  
   echo "echo \"Hello, \$name\";\n" >> $WEBSERVICE_FILE  
   echo "});\n" >> $WEBSERVICE_FILE  
      echo "\$app->run();?>\n" >> $WEBSERVICE_FILE  
 fi  
 echo "it should have worked, try http://127.0.0.1/api/hello/test and you should see \"Hello, test\""  

No comments:

Post a Comment