Skip to content
Go to file

Latest commit


Git stats


Failed to load latest commit information.
Latest commit message
Commit time


npm version Build Status Installs Gitter

Zguillez | Guillermo de la Iglesia

Yeoman generator for backend API development with PHP Slim micro framework

Getting Started

Install Yeoman

npm install -g yo

Yeoman Generators

To install generator-slimphp from npm, run:

npm install -g generator-slimphp

Finally, initiate the generator:

yo slimphp







For update local composer

./composer.phar self-update



FIRST OF ALL you need to edit the file .sshconfig.

configuration file

Edit the .sshconfig with the data of your SSH server access and data base configuration.

    "domain": "https://{}",
    "ssh": {
        "host": "{ip}",
        "username": "{username}",
        "password": "{password}",
        "path": "/var/www/vhosts/{}/httpdocs/",
        "folder": "{folder}"
    "ftp": {
        "host": "ftp.{}",
        "port": 21,
        "username": "{username}",
        "password": "{password}",
        "local": "./",
        "remote": "/"
	"database": {
		"host": "{ip}",
		"username": "{username}",
		"password": "{password}",
		"database": "{database}"

configure local environment

Run de npm command prepare:local. This will edit the file inc/config.php with the .sshconfig data.

yarn prepare:local

configure remote server

Run de npm command prepare-remote. Install composer dependencies.

yarn prepare-remote

Disable the database connection

If your api don't connect to a database, you need to remove the require of the config in the file index.php.

//require 'inc/config.php';

Log System

All api call create a log file on /log folder. This folder must be created and with writte permitions.


You can send traces to the log file from de routes by:

$api->log->insert ( $request->getUri () );

to disable the logs system remove or comment the line $this->log->insert($data); on app.php file.


Develop code on folder /inc


Local server

Run npm task serve for development server

yarn serve

API Routes

Put all your API urls into the file /inc/routes.php

// inc/routes

$api->route('/', 'GET', require 'inc/routes/index.php');

If your app is on a folder (for example '/api') uncomment this line:


This will work for path.

In the /inc/routes/ folder make a induvidual file for the path function:

// inc/routes/index.php


return function ($request, $response, $args) {
	global $api;
	$html = '

	return $api->response($response, $html, 200, 'text/html');

This will return an HTML file for the path http://localhost:9001/

You can change the status response, for example to 404:

return $api->response($response, $html, 404, 'text/html');

And the header content-type:

return $api->response($response, $html, 200, 'application/json');

Queries to the database

Make the SQL queries in the routes files to the global object $api:

// inc/routes/user.php


return function ($request, $response, $args) {
	global $api;
	$token = $request->getAttribute('token');
	$data = $api->query("SELECT * FROM users WHERE token='" . $token . "'");

	return $api->response($response, json_encode($data), 200, 'application/json');

This will return some data to the dummy api url:

// index.php

$api->route('/user/{token}', 'GET', require 'inc/routes/user.php');


You can do api call in GET or POST method:

// index.php

$api->route('/user/{token}', 'GET', require 'inc/routes/user.php');
$api->route('/user/add/', 'POST', require 'inc/routes/user-add.php');

To get POST data use the getParseBody() function:

// inc/routes/user-add.php


return function ($request, $response, $args) {
	global $api;
	$data = $request->getParsedBody();
	$sql = "INSERT IGNORE INTO users (name, surname, email, phone, contact, token, ip) VALUES('" . $data["name"] . "','" . $data["surname"] . "','" . $data["email"] . "','" . $data["phone"] . "','" . $data["contact"] . "','" . $data["token"] . "','" . $_SERVER['REMOTE_ADDR'] . "')";
	$leadid = $api->query($sql);
	if ($leadid > 0) {
		$status = 1;
		$result["leadid"] = $leadid;
	} else {
		$status = 0;
		$result["error"] = "Not inserted";

	return $api->response($response, json_encode(Array('status' => $status, 'result' => $result)), 200, 'application/json');


Mobile detection for api calls is implemented.

  "require": {
    "slim/slim": "^3.0",
    "zguillez/slim-mobile-detect": "^1.0"

So you can edit the response function on app.php file:

public function response($response, $data = '', $status = 200, $type = 'text/html')
	$response = new MobileResponse($response);
	return $response->withStatus($status)->withHeader('Content-type', $type)->write($data);

For more info check:


Mustache templates is implemented. You can load a template from the route file

$name = $request->getAttribute ( 'name' );

$html = $api->template ( 'hello', ['name' => $name] );

return $api->response ( $response , $html , 200 , 'text/html' );

The templates files are on folders /inc/views/ and /inc/views/partials/.

For more info check:

Publish to production

If you have SSH access to your production server, you can publish and upload the api files to the server by a npm task.

yarn deploy

To install composer dependencies run the task prepare-remote.

yarn prepare-remote

You must edit the PHP path on the task prepare-remote at the file packaje.json.

'/opt/plesk/php/5.6/bin/php composer.phar update'



This function will check if a list of parameter are passed in POST data:

if ($api->validateData($data, ['name', 'email'])) {
} else {
	//error: no 'name' or 'email' parameter


This function will check if a list of parameter are empty in POST data:

if ($api->validateEmptyData($data, ['name', 'email'])) {
} else {
	//error: 'name' or 'email' have empty value

Contributing and issues

Contributors are welcome, please fork and send pull requests! If you have any ideas on how to make this project better then please submit an issue or send me an email.



Original code licensed under MIT Open Source projects used within this project retain their original licenses.


v3.1.0 (March 20, 2020)

  • Update Slimphp 3 api template
  • Update Slimphp 3 web template
  • Update Slimphp 3 web++ template
  • Update dependencies

v3.0.0 (Octover 2, 2019)

  • Update to Slim 3.12.2
  • Add Slim 4.2.0 option
  • Add prompt for web development project

v2.6.0 (January 26, 2018)

  • Add prompt for web development project

v2.5.0 (October 26, 2017)

  • Update yeoman generator
  • Config on .sshconfig file
  • Nodejs config tools
  • Add yarn dependencie
  • Remove grunt
  • Remove virtual host

v2.0.0 (December 21, 2016)

  • Setup virtual host
  • Config on json file
  • Publish api to production by SSH
  • Log system
  • Templates with Mustache

v1.3.0 (August 23, 2016)

  • Core update
  • Update dependencies
  • Implements MobileResponse
  • validateData and validateEmptyData methods

v1.1.0 (March 24, 2016)

  • Allow DELETE and PUT methods

v1.0.0 (January 12, 2016)

  • Fix yo install version

v0.1.0 (January 7, 2016)

  • Initial Slim Framework skeleton Features:
  • Slim micro framework
  • External route files
  • Grunt tasks



Yeoman generator for backend API development with Slimframework 3 (PHP)





No packages published
You can’t perform that action at this time.