npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

generator-slimphp

v3.1.7

Published

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

Downloads

933

Readme

generator-slimphp

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

Requeriments

Nodejs

Documentation:

Yarn

Documentation:

Composer

For update local composer

./composer.phar self-update

Documentation:

Configuration

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://{mydomain.com}",
    "ssh": {
        "host": "{ip}",
        "username": "{username}",
        "password": "{password}",
        "path": "/var/www/vhosts/{mydomain.com}/httpdocs/",
        "folder": "{folder}"
	},
    "ftp": {
        "host": "ftp.{mydomain.com}",
        "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.

/logs/apicalls.log

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.

Usage

Develop code on folder /inc

/inc
    /app.php
    /config.php
    /routes
        /index.php
        /user-add.php
        /user.php
        /etc..

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
<?php

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

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

//$api->folder('api');

This will work for http://domain.com/api/ path.

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

// inc/routes/index.php

<?php

return function ($request, $response, $args) {
	global $api;
	$html = '
		<h2>Routes</h2>
		<ul>
		<li>/</li>
		<li>/user/{token}</li>
		<li>/user/add/</li>
		</ul>
		';

	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

<?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
<?php

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

GET or POST

You can do api call in GET or POST method:

// index.php
<?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

<?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-Detect

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:

#Templating

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'

Tools

validateData

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
}

validateEmptyData

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.

License

©2020 Zguillez.io

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

Changelog

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

Analytics