Building a basic RESTful API with Bullet PHP

Written by on April 7, 2015

Building a basic RESTful API with Bullet PHP

Last year I wrote an article covering best available PHP RESTful micro frameworks, now I want to show you how to create a simple RESTfull using Bullet PHP framework. This article will cover only installation, configuration, and basic example. Next one will include more Bullet PHP information and additional working examples. I will include Windows and Linux (Debian/Ubuntu) setup, one after another. Please notify me if you find any technical or information inaccuracies so I can update this article to address them, leave me a comment down below or you can even mail me to: dragan.gaic@gmail.com.

Before we start this tutorial make sure, you have everything installed/configured to handle PHP and Bullet PHP. To make it easier, we will use preconfigured development environments, WAMP for Windows and LAMP for Linux. I do not advise using WAMP/LAMP for your production environment.

Note: If this tutorial was helpful, you need further clarification, something is not working or you have a request for another Ionic post? Furthermore, leave me a comment below if you don't like something about this blog, if something is bugging you, don't like how I'm doing stuff here. Feel free to comment below, subscribe to my blog, mail me to dragan.gaic@gmail.com. Thanks and have a nice day!

Useful Links

Official Site
GitHub
Documentation

Table of Contents

Note: If you have already installed WAMP/LAMP + Composer just jump to the Bullet PHP Framework installation segment (links are provided below).

Windows Preparation
Linux Preparation
Bullet PHP Example

Windows Preparation

1a. Install WAMP

Go to the official WAMP site and get the latest version, or any other if you don’t want the use latest version. Depending on your Windows platform (32 bit or 64 bit), select Windows Installer (.msi) and download it.

Download Link

Bullet PHP 0001

When you click on download directly current page will change to sourceforge.net download location, don’t click on anything, download will start automatically. Begin the installation when download completes. During this process, you will be prompted to select your default browser, do so.

If you receive this error during the installation you should also install Visual Studio 2012 VC 11 vcredist_x64/86.exe (again select 32 or 64 bit version):

Bullet PHP 0002

Download Link

Restart WAMP or Windows. If you did everything correctly, WAMP icon (in the icon tray) should glow green, and you will be able to open your browser and go to the localhost. For the purpose of this article, WAMP is installed at this location:

c:\WAMP\

and PHP should be installed at this location:

c:\WAMP\bin\php\php5.5.12\

Copy this path and add it to your environment PATH. Take a look at this tutorial if you don’t know how to do that, you will find examples for Windows XP, Windows 2000 and Windows 7. It should look like this:

Bullet PHP 0003

Do the same thing with curl, find it here:

c:\WAMP\bin\apache\apache2.4.9\extra-modules\mod_security-2.8.0\mlogc\

If you are using different WAMP version then this there’s a good chance you won’t find curl in that folder. In this case just go to WAMP root folder and search for curl.exe. Copy this path and add it to your environment PATH, it should look like this:

Bullet PHP 0004

2a. Install Composer

First create a folder for the Composer. Go to this location:

c:\WAMP\bin

And create a folder named:

c:\WAMP\bin\composer

Copy this path and add it to your environment PATH, it should look like this:

Bullet PHP 0005

Open Windows command prompt (at this location: c:\WAMP\bin\) and execute:

curl -sS "http://getcomposer.org/installer" | php -- --install-dir=composer

If you did everything correctly, output should look like this:

#!/usr/bin/env php
All settings correct for using Composer
Downloading...

Composer successfully installed to: C:\WAMP\www\composer\composer.phar
Use it: php composer/composer.phar

While you are still in the command prompt, go to this newly created folder: C:\WAMP\bin\composer and execute this:

echo @php "%~dp0composer.phar" %*>composer.bat

3a. Install Bullet PHP

First create a project folder. Go to this location:

c:\WAMP\www\

And delete everything you find there, do this only once after the initial WAMP installation. The www folder (sometimes it’s also called public_html or htdocs) is the web root for your primary domain name. This means that www is the folder where you put all website files which you want to appear when someone types your primary domain (the one you provided when you signed up for hosting, or localhost in our case). Our project folder name will be (name it any way you want):

c:\WAMP\www\bullet_demo

And our Bullet PHP directory will be:

c:\WAMP\www\bullet_demo\bullet

Open command prompt and go to the previously mentioned folder (c:\WAMP\www\bullet_demo\bullet). Execute this line:

composer require vlucas/bulletphp *

File structure will look like this:

bullet
    --->composer.json
    --->composer.lock
    --->vendor
        --->composer
        --->pimple
        --->vlucas
        --->autoload.php

Now you need to open this file:

C:\WINDOWS\system32\drivers\etc\hosts

in Notepad or any other script editor. Look for the following line at the bottom:

127.0.0.1   localhost

Add a separate line, enter 127.0.0.1, followed by some space and the name of the virtual host you want to register. For instance, we will use:

127.0.0.1   bullet.localhost

Save it. If you can’t do that (Win 7 and 8) just copy hosts somewhere else, make all changes and copy it back. Windows will ask you for Administrative privileges.

Now we need to set up a named virtual host for our application, open the Apache configuration file (search for httpd.conf in your WAMP directory, if you find more than one file do this in all of them ) and look for these lines:

# Virtual hosts
# Include conf/extra/httpd-vhosts.conf

change them like this and save:

# Virtual hosts
Include conf/extra/httpd-vhosts.conf

Now search for httpd-vhosts.conf in your WAMP directory, and make it look like this:

# Virtual Hosts
#
# Required modules: mod_log_config

# If you want to maintain multiple domains/hostnames on your
# machine you can setup VirtualHost containers for them. Most configurations
# use only name-based virtual hosts so the server doesn't need to worry about
# IP addresses. This is indicated by the asterisks in the directives below.
#
# Please see the documentation at 
# <URL:http://httpd.apache.org/docs/2.4/vhosts/>
# for further details before you try to setup virtual hosts.
#
# You may use the command line option '-S' to verify your virtual host
# configuration.
<Directory C:/wamp/www>
  Require all granted
</Directory>

NameVirtualHost *
<VirtualHost *>
    ServerName bullet.localhost
    DocumentRoot "c:/wamp/www/bullet_demo/bullet"
    <Directory "c:/wamp/www/bullet_demo/bullet">
        Options -Indexes +FollowSymLinks +Includes
        AllowOverride All
        Order allow,deny
        Allow from All
    </Directory>
</VirtualHost>

If this is not your first virtual host, just add these lines.

<VirtualHost *>
    ServerName bullet.localhost
    DocumentRoot "c:/wamp/www/bullet_demo/bullet"
    <Directory "c:/wamp/www/bullet_demo/bullet">
        Options -Indexes +FollowSymLinks +Includes
        AllowOverride All
        Order allow,deny
        Allow from All
    </Directory>
</VirtualHost>

Restart the Apache server.

Now let’s make sure everything works just fine, create index.php in your bullet directory (c:\wamp\www\bullet_demo\bullet) and add this:

<?php
require __DIR__ . '/vendor/autoload.php';
 
// Your App
$app = new Bullet\App();
$app->path('/', function($request) {
    return "Hello World!";
});
 
// Run the app! (takes $method, $url or Bullet\Request object)
echo $app->run(new Bullet\Request());
 
?>

Now go to your browser and enter this to your address bar:

bullet.localhost

If everything worked correctly you should see: “Hello World!”, if not you will see “Not found”. In case of “Not found” make sure you did everything correctly.

Linux Preparation

This tutorial has been tested on a single VPS DigitalOcean 512MB droplet, Ubuntu 14.04 x64. It is much shorter than a Windows preparation tutorial … go figure.

1b. Install LAMP

Execute this first:

sudo apt-get update

If you don’t update package lists from the repositories, you will probably receive this error: tasksel: aptitude failed (100). Now we’ll install the LAMP stack:

sudo  tasksel

Taskel will open a GUI menu, select only LAMP server, press OK, wait some time and finally enter MySQL root password when GUI prompts you:

Bullet PHP 0006

Bullet PHP 0007

Bullet PHP 0008

If you still can’t manage to finish the installation (error mentioned above) install it directly like this:

sudo tasksel install lamp-server

2b. Install Curl

Do this step only if haven’t previously installed Curl:

sudo apt-get install curl

Bullet PHP 0009

3b. Install Composer

Execute these lines:

curl -sS https://getcomposer.org/installer | php
mv composer.phar /usr/local/bin/composer

It should look like this:

Bullet PHP 0010

Continue Reading

Categories

Leave a Reply