{"id":19793261,"url":"https://github.com/zguillez/generator-slimphp","last_synced_at":"2025-05-01T02:30:40.237Z","repository":{"id":40296084,"uuid":"49222304","full_name":"zguillez/generator-slimphp","owner":"zguillez","description":"Yeoman generator for backend API development with Slimframework 3 (PHP)","archived":false,"fork":false,"pushed_at":"2023-01-06T01:32:09.000Z","size":6074,"stargazers_count":6,"open_issues_count":31,"forks_count":4,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-04-06T07:43:12.713Z","etag":null,"topics":["api","backend","php","slim-framework","yeoman-generator"],"latest_commit_sha":null,"homepage":"https://zguillez.io","language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/zguillez.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":".github/FUNDING.yml","license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null},"funding":{"github":null,"patreon":"zguillez","open_collective":null,"ko_fi":"zguillez","tidelift":null,"community_bridge":null,"liberapay":null,"issuehunt":null,"otechie":null,"custom":null}},"created_at":"2016-01-07T18:25:07.000Z","updated_at":"2023-10-21T21:27:44.000Z","dependencies_parsed_at":"2023-02-05T01:32:22.979Z","dependency_job_id":null,"html_url":"https://github.com/zguillez/generator-slimphp","commit_stats":null,"previous_names":[],"tags_count":13,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/zguillez%2Fgenerator-slimphp","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/zguillez%2Fgenerator-slimphp/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/zguillez%2Fgenerator-slimphp/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/zguillez%2Fgenerator-slimphp/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/zguillez","download_url":"https://codeload.github.com/zguillez/generator-slimphp/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":251812249,"owners_count":21647870,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["api","backend","php","slim-framework","yeoman-generator"],"created_at":"2024-11-12T07:09:19.724Z","updated_at":"2025-05-01T02:30:36.153Z","avatar_url":"https://github.com/zguillez.png","language":"PHP","funding_links":["https://patreon.com/zguillez","https://ko-fi.com/zguillez","https://ko-fi.com/Q5Q4329UT"],"categories":[],"sub_categories":[],"readme":"# generator-slimphp\n\n[![Analytics](https://ga-beacon.appspot.com/UA-1125217-30/zguillez/generator-slimphp?pixel)](https://github.com/igrigorik/ga-beacon)\n[![npm version](https://badge.fury.io/js/generator-slimphp.svg)](https://badge.fury.io/js/generator-slimphp)\n[![Installs](https://img.shields.io/npm/dt/generator-slimphp.svg)](https://coveralls.io/r/zguillez/generator-slimphp)\n[![Gitter](https://badges.gitter.im/zguillez/generator-slimphp.svg)](https://gitter.im/zguillez/generator-slimphp?utm_source=badge\u0026utm_medium=badge\u0026utm_campaign=pr-badge\u0026utm_content=badge)\n\n\u003e [Zguillez](https://zguillez.io) | Guillermo de la Iglesia\n\n[![ko-fi](https://www.ko-fi.com/img/githubbutton_sm.svg)](https://ko-fi.com/Q5Q4329UT)\n\n## Yeoman generator for backend API development with [PHP Slim micro framework](http://www.slimframework.com/)\n\n![](http://zguillez.github.io/img/slimphp.png)\n\n# Getting Started\n## Install Yeoman\n\n```\nnpm install -g yo\n```\n\n## Yeoman Generators\nTo install generator-slimphp from npm, run:\n\n```\nnpm install -g generator-slimphp\n```\n\nFinally, initiate the generator:\n\n```\nyo slimphp\n```\n\n## Requeriments\n\n### [Nodejs](https://nodejs.org)\n\n**Documentation:**\n- [https://nodejs.org](https://nodejs.org)\n\n\n### [Yarn](https://yarnpkg.com)\n\n**Documentation:**\n- [https://yarnpkg.com](https://yarnpkg.com)\n\n### [Composer](https://getcomposer.org)\nFor update local composer\n\n```\n./composer.phar self-update\n```\n**Documentation:**\n- [https://getcomposer.org](https://getcomposer.org)\n\n# Configuration\n\n**FIRST OF ALL** you need to edit the file *.sshconfig*. \n\n## configuration file\n\nEdit the **.sshconfig** with the data of your SSH server access and data base configuration.\n \n```\n{\n    \"domain\": \"https://{mydomain.com}\",\n    \"ssh\": {\n        \"host\": \"{ip}\",\n        \"username\": \"{username}\",\n        \"password\": \"{password}\",\n        \"path\": \"/var/www/vhosts/{mydomain.com}/httpdocs/\",\n        \"folder\": \"{folder}\"\n\t},\n    \"ftp\": {\n        \"host\": \"ftp.{mydomain.com}\",\n        \"port\": 21,\n        \"username\": \"{username}\",\n        \"password\": \"{password}\",\n        \"local\": \"./\",\n        \"remote\": \"/\"\n     },\n\t\"database\": {\n\t\t\"host\": \"{ip}\",\n\t\t\"username\": \"{username}\",\n\t\t\"password\": \"{password}\",\n\t\t\"database\": \"{database}\"\n\t}\n}\n```\n\n## configure local environment\n\nRun de npm command **prepare:local**. This will edit the file *inc/config.php* with the *.sshconfig* data.\n\n```\nyarn prepare:local\n```\n\n## configure remote server\n\nRun de npm command **prepare-remote**. Install composer dependencies.\n\n```\nyarn prepare-remote\n```\n\n## Disable the database connection\n\nIf your api don't connect to a database, you need to remove the *require* of the config in the file *index.php*.\n\n```\n//require 'inc/config.php';\n```\n\n## Log System\n\nAll api call create a log file on /log folder. This folder must be created and with writte permitions.\n\n```\n/logs/apicalls.log\n```\n\nYou can send traces to the log file from de routes by:\n\n```\n$api-\u003elog-\u003einsert ( $request-\u003egetUri () );\n```\n\nto disable the logs system remove or comment the line ```$this-\u003elog-\u003einsert($data);``` on **app.php** file.\n\n# Usage\nDevelop code on folder **/inc**\n\n```\n/inc\n    /app.php\n    /config.php\n    /routes\n        /index.php\n        /user-add.php\n        /user.php\n        /etc..\n```\n\n## Local server\nRun npm task *serve* for development server\n\n```\nyarn serve\n```\n\n# API Routes\n\nPut all your API urls into the file **/inc/routes.php**\n\n```\n// inc/routes\n\u003c?php\n\n...\n$api-\u003eroute('/', 'GET', require 'inc/routes/index.php');\n...\n```\n\nIf your app is on a folder (for example '**/api**') uncomment this line:\n\n```\n//$api-\u003efolder('api');\n```\nThis will work for **http://domain.com/api/** path.\n\nIn the **/inc/routes/** folder make a induvidual file for the path function:\n\n```\n// inc/routes/index.php\n\n\u003c?php\n\nreturn function ($request, $response, $args) {\n\tglobal $api;\n\t$html = '\n\t\t\u003ch2\u003eRoutes\u003c/h2\u003e\n\t\t\u003cul\u003e\n\t\t\u003cli\u003e/\u003c/li\u003e\n\t\t\u003cli\u003e/user/{token}\u003c/li\u003e\n\t\t\u003cli\u003e/user/add/\u003c/li\u003e\n\t\t\u003c/ul\u003e\n\t\t';\n\n\treturn $api-\u003eresponse($response, $html, 200, 'text/html');\n};\n```\n\nThis will return an HTML file for the path http://localhost:9001/\n\nYou can change the status response, for example to 404:\n\n```\nreturn $api-\u003eresponse($response, $html, 404, 'text/html');\n```\n\nAnd the header content-type:\n\t\n```\nreturn $api-\u003eresponse($response, $html, 200, 'application/json');\n```\n\n## Queries to the database\n\nMake the SQL queries in the routes files to the global object **$api**:\n\n```\t\n// inc/routes/user.php\n\n\u003c?php\n\nreturn function ($request, $response, $args) {\n\tglobal $api;\n\t$token = $request-\u003egetAttribute('token');\n\t$data = $api-\u003equery(\"SELECT * FROM users WHERE token='\" . $token . \"'\");\n\n\treturn $api-\u003eresponse($response, json_encode($data), 200, 'application/json');\n};\n```\n\nThis will return some data to the dummy api url:\n\n```\n// index.php\n\u003c?php\n\n...\n$api-\u003eroute('/user/{token}', 'GET', require 'inc/routes/user.php');\n...\n```\n\n# GET or POST\n\nYou can do api call in GET or POST method:\n\n```\n// index.php\n\u003c?php\n\n...\n$api-\u003eroute('/user/{token}', 'GET', require 'inc/routes/user.php');\n$api-\u003eroute('/user/add/', 'POST', require 'inc/routes/user-add.php');\n...\n```\n\nTo get POST data use the **getParseBody()** function:\n\n```\n// inc/routes/user-add.php\n\n\u003c?php\n\nreturn function ($request, $response, $args) {\n\tglobal $api;\n\t$data = $request-\u003egetParsedBody();\n\t$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'] . \"')\";\n\t$leadid = $api-\u003equery($sql);\n\tif ($leadid \u003e 0) {\n\t\t$status = 1;\n\t\t$result[\"leadid\"] = $leadid;\n\t} else {\n\t\t$status = 0;\n\t\t$result[\"error\"] = \"Not inserted\";\n\t}\n\n\treturn $api-\u003eresponse($response, json_encode(Array('status' =\u003e $status, 'result' =\u003e $result)), 200, 'application/json');\n};\n```\n\n# Mobile-Detect\n\nMobile detection for api calls is implemented. \n\n```\n{\n  \"require\": {\n    \"slim/slim\": \"^3.0\",\n    \"zguillez/slim-mobile-detect\": \"^1.0\"\n  }\n}\n```\n\nSo you can edit the **response** function on app.php file:\n\n```\npublic function response($response, $data = '', $status = 200, $type = 'text/html')\n{\n\t$response = new MobileResponse($response);\n\treturn $response-\u003ewithStatus($status)-\u003ewithHeader('Content-type', $type)-\u003ewrite($data);\n}\n```\n\nFor more info check:\n\n* [https://github.com/zguillez/slim-mobile-detect](https://github.com/zguillez/slim-mobile-detect)\n* [https://packagist.org/packages/zguillez/slim-mobile-detect](https://packagist.org/packages/zguillez/slim-mobile-detect)\n\n#Templating\n\nMustache templates is implemented. You can load a template from the route file\n\n```\n$name = $request-\u003egetAttribute ( 'name' );\n\n$html = $api-\u003etemplate ( 'hello', ['name' =\u003e $name] );\n\nreturn $api-\u003eresponse ( $response , $html , 200 , 'text/html' );\n```\n\nThe templates files are on folders **/inc/views/** and  **/inc/views/partials/**.\n\nFor more info check:\n\n* [https://github.com/bobthecow/mustache.php](https://github.com/bobthecow/mustache.php)\n\n# Publish to production\n\nIf you have SSH access to your production server, you can publish and upload the api files to the server by a npm task.\n\n```\nyarn deploy\n```\n\nTo install composer dependencies run the task *prepare-remote*.\n\n```\nyarn prepare-remote\n```\n\nYou must edit the PHP path on the task **prepare-remote** at the file *packaje.json*.\n\n```\n'/opt/plesk/php/5.6/bin/php composer.phar update'\n```\n\n# Tools\n\n## validateData\n\nThis function will check if a list of parameter are passed in POST data:\n\n```\nif ($api-\u003evalidateData($data, ['name', 'email'])) {\n\t...\n} else {\n\t//error: no 'name' or 'email' parameter\n}\n```\n\n## validateEmptyData\n\nThis function will check if a list of parameter are empty in POST data:\n\n```\nif ($api-\u003evalidateEmptyData($data, ['name', 'email'])) {\n\t...\n} else {\n\t//error: 'name' or 'email' have empty value\n}\n```\n\n# Contributing and issues\nContributors 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](mailto:mail@zguillez.io).\n\n# License\n©2020 [Zguillez.io](https://zguillez.io)\n\nOriginal code licensed under [MIT](https://en.wikipedia.org/wiki/MIT_License) Open Source projects used within this project retain their original licenses.\n\n# Changelog\n### v3.1.0 (March 20, 2020)\n- Update Slimphp 3 api template\n- Update Slimphp 3 web template\n- Update Slimphp 3 web++ template\n- Update dependencies\n\n### v3.0.0 (Octover 2, 2019)\n- Update to Slim 3.12.2\n- Add Slim 4.2.0 option\n- Add prompt for web development project\n\n### v2.6.0 (January 26, 2018)\n- Add prompt for web development project\n\n### v2.5.0 (October 26, 2017)\n- Update yeoman generator\n- Config on .sshconfig file\n- Nodejs config tools\n- Add yarn dependencie\n- Remove grunt\n- Remove virtual host\n\n### v2.0.0 (December 21, 2016)\n- Setup virtual host\n- Config on json file\n- Publish api to production by SSH\n- Log system\n- Templates with Mustache\n\n### v1.3.0 (August 23, 2016)\n- Core update\n- Update dependencies\n- Implements MobileResponse \n- validateData and validateEmptyData methods\n\n### v1.1.0 (March 24, 2016)\n- Allow DELETE and PUT methods\n\n### v1.0.0 (January 12, 2016)\n- Fix yo install version\n\n### v0.1.0 (January 7, 2016)\n- Initial Slim Framework skeleton\nFeatures:\n* Slim micro framework\n* External route files\n* Grunt tasks\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fzguillez%2Fgenerator-slimphp","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fzguillez%2Fgenerator-slimphp","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fzguillez%2Fgenerator-slimphp/lists"}