Yeoman generator for backend API development with Slimframework 3 (PHP)
npm install generator-slimphp



> Zguillez | Guillermo de la Iglesia

```
npm install -g yo
``
npm install -g generator-slimphp
Finally, initiate the generator:
``
yo slimphp
Documentation:
- https://nodejs.org
Documentation:
- https://yarnpkg.com
``
./composer.phar self-update
Documentation:
- https://getcomposer.org
FIRST OF ALL you need to edit the file .sshconfig.
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}"
}
}
Run de npm command prepare:local. This will edit the file inc/config.php with the .sshconfig data.
``
yarn prepare:local
Run de npm command prepare-remote. Install composer dependencies.
``
yarn prepare-remote
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';
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.
``
/inc
/app.php
/config.php
/routes
/index.php
/user-add.php
/user.php
/etc..
``
yarn serve
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:
``
//$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
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');
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:
* https://github.com/zguillez/slim-mobile-detect
* https://packagist.org/packages/zguillez/slim-mobile-detect
#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:
* https://github.com/bobthecow/mustache.php
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
}
Original code licensed under MIT Open Source projects used within this project retain their original licenses.
- Add prompt for web development project
Features:
* Slim micro framework
* External route files
* Grunt tasks
