GithubHelp home page GithubHelp logo

davellanedam / phalcon-micro-rest-api-skeleton Goto Github PK

View Code? Open in Web Editor NEW
57.0 13.0 20.0 103 KB

This is a basic API REST skeleton written on Phalcon PHP. Great For building an MVP for your frontend app (Vue, react, angular, or anything that can consume an API)

License: MIT License

PHP 89.92% Shell 1.02% HTML 2.40% TSQL 6.67%
phalcon phalcon-micro-api postman skeleton php api micro rest-api rest token jwt jwt-token jwt-authentication phalconphp phalcon-skeleton

phalcon-micro-rest-api-skeleton's Introduction

Phalcon Micro REST API Basic Project Skeleton

Author Software License Release

Getting started

This is a basic API REST skeleton written on the ultra hyper mega fastest framework for PHP phalcon. A full-stack PHP framework delivered as a C-extension. Its innovative architecture makes Phalcon the fastest PHP framework ever built!

This project is created to help other developers create a basic REST API in an easy way with phalcon. This basic example shows how powerful and simple phalcon can be. Do you want to contribute? Pull requests are always welcome to show more features of phalcon.

Features

  • JWT Tokens, provide login with Authorization header with value Basic username:password where username:password MUST BE ENCODED with Base64.
  • Make requests with a token after login with Authorization header with value Bearer yourToken where yourToken is the signed and encrypted token given in the response from the login process.
  • Use ACL so you can have roles for users.
  • Timezone ready: Work UTC time (GMT+0). Responses with iso8601 date/time format.
  • Pagination ready.
  • Filters (JSON).
  • Easy deploy to staging and production environments with rsync.
  • Internationalization ready. API responses use JSON format to make life easier at the frontend.
  • Separate database for logs.
  • User profile.
  • Users list.
  • Cities. (Example of use: call cities API, then send name of the city when creating or updating a user.
  • Integrated tests

Requirements

  • Apache 2
  • PHP 5.6+
  • Phalcon 3.2+
  • MySQL 5.5+

How to install

Using Git (recommended)

  1. First you need to install composer if you haven´t already.
  2. Clone the project from github. Change 'myproject' to you project name.
git clone https://github.com/davellanedam/phalcon-micro-api.git ./myproject

Using manual download ZIP

  1. Download repository
  2. Uncompress to your desired directory

Install composer dependencies after installing (Git or manual download)

cd myproject
composer install
composer update

Database Configuration and Security

There are 3 files in the /myproject/config directory, (development, staging and production) each one is meant to be used on different environments to make your life easier on deployment.

  1. Create a MySQL database with your custom name and then import myproject.sql (in the /schemas directory)
  2. Create a second MySQL database with your custom name and then import myproject_log.sql (in the /schemas directory).
  3. Open /myproject/config/server.development.php and setup your DEVELOPMENT (local) database connection credentials
  4. Open /myproject/config/server.staging.php and setup your STAGING (testing server) database connection credentials
  5. Open /myproject/config/server.production.php and setup your PRODUCTION (production server) database connection credentials

This is the structure of those 3 files, remember to change values for yours.

return [
    'database' => [
        'adapter' => 'Mysql', /* Possible Values: Mysql, Postgres, Sqlite */
        'host' => 'your_ip_or_hostname',
        'username' => 'your_db_username',
        'password' => 'your_db_password',
        'dbname' => 'your_database_schema',
        'charset' => 'utf8',
    ],
    'log_database' => [
        'adapter' => 'Mysql', /* Possible Values: Mysql, Postgres, Sqlite */
        'host' => 'your_ip_or_hostname',
        'username' => 'your_db_username',
        'password' => 'your_db_password',
        'dbname' => 'myproject_log',
        'charset' => 'utf8',
    ],
    'authentication' => [
        'secret' => 'your secret key to SIGN token', // This will sign the token. (still insecure)
        'encryption_key' => 'Your ultra secret key to ENCRYPT the token', // Secure token with an ultra password
        'expiration_time' => 86400 * 7, // One week till token expires
        'iss' => 'myproject', // Token issuer eg. www.myproject.com
        'aud' => 'myproject', // Token audience eg. www.myproject.com
    ]
];

Setting up environments

The ENV variable is set on an .htaccess file located at /public/.htaccess that you must upload once to each server you use. Change the environment variable on each server to what you need. To make your life easy this .htaccess file is on the excluded files list to upload when you make a deploy. Possible values are: development, staging and production.

############################################################
# Possible values: development, staging, production        #
# Change value and upload ONCE to your server              #
# AVOID re-uploading when deployment, things will go crazy #
############################################################
SetEnv APPLICATION_ENV "development"

Usage

Once everything is set up to test API routes either use Postman or any other api testing application. Remember to change the URL of the provided example Postman JSON file. Default username/password combination for login is admin/admin1234.

If you use Postman please go to manage environments and then create one for each of your servers. Create a new key authToken with token value (the token you got from the login process), each time you make a request to the API it will send Authorization header with the token value in the request, you can check this on the headers of users or cities endpoints in the Postman example.

This is a REST API, so it works using the following HTTP methods:

  • GET (Read): Gets a list of items, or a single item
  • POST (Create): Creates an item
  • PATCH (Update): Updates an item
  • DELETE: Deletes an item

Testing

There are some tests included, to run tests you need to go to the command line and type:

composer test

Creating new models

If you need to add more models to the project there´s an easy way to do it with phalcondevtools (If you did composer install, you already have this). Step into a terminal window and open your project folder, then type the following and you are set!

phalcon model --name=your_table_name --schema=your_database --mapcolumn

Creating new controllers

If you need to add more controllers to the project there´s an easy way to do it with phalcondevtools (If you did composer install, you already have this). Step into a terminal window and open your project folder, then type the following.

phalcon controller --name=your_controller_name_without_the_controller_word

When it´s done, it creates your new controller, but if you want to use ControllerBase.php functions in your newly created controller you must change the following line in the new controller:

class MyNewController extends \Phalcon\Mvc\Controller

to this:

class MyNewController extends ControllerBase

Creating new routes

You can add more routes to your project by adding them into the /app.php file. This is an example of /users routes:

/**
* Users
*/
$users = new MicroCollection();
$users->setHandler('UsersController', true);
$users->setPrefix('/users');
// Gets all users
$users->get('/', 'index');
// Creates a new user
$users->post('/create', 'create');
// Gets user based on unique key
$users->get('/get/{id}', 'get');
// Updates user based on unique key
$users->patch('/update/{id}', 'update');
// Changes user password
$users->patch('/change-password/{id}', 'changePassword');
// Adds users routes to $app
$app->mount($users);

Remember to add the controller (without the controller word) and methods of endpoints to the /config/acl.phpfile. Otherwise you will get this response from the API: 'common.YOUR_USER_ROLE_DOES_NOT_HAVE_THIS_FEATURE',

/*
 * RESOURCES
 * for each user, specify the 'controller' and 'method' they have access to ('user_type'=>['controller'=>['method','method','...']],...)
 * */
$arrResources = [
    'Guest'=>[
        'Users'=>['login'],
    ],
    'User'=>[
        'Profile'=>['index','update','changePassword'],
        'Users'=>['index','create','get','search','update','logout'],
        'Cities'=>['index','create','get','ajax','update','delete'],
    ],
    'Superuser'=>[
        'Users'=>['changePassword'],
    ]
];

Always keep in mind the following:

/*
 * ROLES
 * Superuser - can do anything (Guest, User, and own things)
 * User - can do most things (Guest and own things)
 * Guest - Public
 * */

Internationalization

API is designed to response with a JSON, so at the FRONTEND you can use lang files like this: Put your language files in 'langs' directory at frontend:

  • en.json
  • es.json

Example of language file en.json :

{
  "common": {
    "HEADER_AUTHORIZATION_NOT_SENT": "Header Authorization not sent",
    "EMPTY_TOKEN_OR_NOT_RECEIVED": "Empty token or not received",
    "YOUR_USER_ROLE_DOES_NOT_HAVE_THIS_FEATURE": "Your user role does not have this feature",
    "BAD_TOKEN_GET_A_NEW_ONE": "Bad token, get a new one",
    "SUCCESSFUL_REQUEST": "Succesfull request",
    "CREATED_SUCCESSFULLY": "Created successfully",
    "THERE_IS_ALREADY_A_RECORD_WITH_THAT_NAME": "There is already a record with that name",
    "UPDATED_SUCCESSFULLY": "Updated successfully",
    "DELETED_SUCCESSFULLY": "Deleted successfully",
    "THERE_HAS_BEEN_AN_ERROR": "There has been an error",
    "INCOMPLETE_DATA_RECEIVED": "Incomplete data received",
    "NO_RECORDS": "No records",
    "COULD_NOT_BE_CREATED": "Could not be created",
    "NOT_FOUND": "Not found",
    "COULD_NOT_BE_DELETED": "Could not be deleted",
    "COULD_NOT_BE_UPDATED": "Could not be updated"
  },
  "login": {
    "USER_IS_NOT_REGISTERED": "User is not registered",
    "USER_BLOCKED": "User blocked",
    "USER_UNAUTHORIZED": "User unauthorized",
    "WRONG_USER_PASSWORD": "Wrong user password",
    "TOO_MANY_FAILED_LOGIN_ATTEMPTS": "Too many failed login attempts"
  },
  "profile": {
    "PROFILE_NOT_FOUND": "Profile not found",
    "PROFILE_UPDATED": "Profile updated",
    "PROFILE_COULD_NOT_BE_UPDATED": "Profile could not be updated",
    "ANOTHER_USER_ALREADY_REGISTERED_WITH_THIS_USERNAME": "Another user already registered with this username"
  },
  "change-password": {
    "PASSWORD_COULD_NOT_BE_UPDATED": "Password could not be updated",
    "PASSWORD_SUCCESSFULLY_UPDATED": "Password updated successfully",
    "WRONG_CURRENT_PASSWORD": "Wrong current password"
  }
}

Example of language file es.json :

{
  "common": {
    "HEADER_AUTHORIZATION_NOT_SENT": "Cabezote Authorization no enviado",
    "EMPTY_TOKEN_OR_NOT_RECEIVED": "Token vacío o no recibido",
    "YOUR_USER_ROLE_DOES_NOT_HAVE_THIS_FEATURE": "Tu rol de usuario no tiene esta característica",
    "BAD_TOKEN_GET_A_NEW_ONE": "Token errado, solicita uno nuevo",
    "SUCCESSFUL_REQUEST": "Solicitud exitosa",
    "CREATED_SUCCESSFULLY": "Creado ecitosamente",
    "THERE_IS_ALREADY_A_RECORD_WITH_THAT_NAME": "Ya existe un registro con ese nombre",
    "UPDATED_SUCCESSFULLY": "Actualizado exitosamente",
    "DELETED_SUCCESSFULLY": "Eliminado exitosamente",
    "THERE_HAS_BEEN_AN_ERROR": "Ha ocurrido un error",
    "INCOMPLETE_DATA_RECEIVED": "Datos incompletos recibidos",
    "NO_RECORDS": "No hay registros",
    "COULD_NOT_BE_CREATED": "No se pudo crear",
    "NOT_FOUND": "No encontrado",
    "COULD_NOT_BE_DELETED": "No pudo ser eliminado",
    "COULD_NOT_BE_UPDATED": "No pudo ser actualizado"
  },
  "login": {
    "USER_IS_NOT_REGISTERED": "Usuario no está registrado",
    "USER_BLOCKED": "Usuario bloqueado",
    "USER_UNAUTHORIZED": "Usuario no autorizado",
    "WRONG_USER_PASSWORD": "Contraseña de usuario errada",
    "TOO_MANY_FAILED_LOGIN_ATTEMPTS": "Demasiados intento de ingreso fallidos"
  },
  "profile": {
    "PROFILE_NOT_FOUND": "Perfil no encontrado",
    "PROFILE_UPDATED": "Perfil actualizado",
    "PROFILE_COULD_NOT_BE_UPDATED": "Perfil no pudo ser actualizado",
    "ANOTHER_USER_ALREADY_REGISTERED_WITH_THIS_USERNAME": "Otro usuario ya se encuentra registrado con este nombre de usuario"
  },
  "change-password": {
    "PASSWORD_COULD_NOT_BE_UPDATED": "Contraseña no pudo ser actualizada",
    "PASSWORD_SUCCESSFULLY_UPDATED": "Contraseña actualizada exitosamente",
    "WRONG_CURRENT_PASSWORD": "Contraseña actual errada"
  }
}

Deployment

You can make a deploy to staging or production servers. It will run a bash file with rsync command to sync your project directory to the servers project directory. Step into a terminal window and open your project folder, then type the following.

Deploy to staging server

deploy-staging.sh

Deploy to production server

deploy-production.sh

Do not forget to change variables to yours on each .sh file

PROJECT=myproject
USER=server_username
URL=my_staging_server_url

Excluding files on deploy

You can exclude files on each deployment environment (/exclude-production.txt and /exclude-staging.txt), add files you want to be excluded on the corresponding .txt file. Each excluded file or directory must be on a new line. Staging exclusion list example:

.git/
*.DS_Store
.phalcon/
.php/
cache/
.htaccess
exclude-staging.txt
exclude-production.txt
deploy-staging.sh
deploy-production.sh
config/server.development.php
config/server.production.php
public/.htaccess
schemas/

Bugs or improvements

Feel free to report any bugs or improvements. Pull requests are always welcome.

I love this! How can I help

It´s amazing you feel like that! Send me a tweet https://twitter.com/davellanedam, share this with others, make a pull request or if you feel really thankful you can always buy me a beer! Enjoy!

License

This project is open-sourced software licensed under the MIT License. See the LICENSE file for more information.

phalcon-micro-rest-api-skeleton's People

Contributors

davellanedam avatar jeijei4 avatar l0wskilled avatar titonobre avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

phalcon-micro-rest-api-skeleton's Issues

Using this skeleton with Phalcon 4

Hi,

I am unable to get this skeleton to work with Phalcon 4.1.0 on my 64bit Windows 10 machine using Wampserver. It shows me errors in most initialization files as given below:

  • In config\services.php it says Undefined Phalcon\Di\FactoryDefault()
    $di = new FactoryDefault();

  • In config\acl.php it says Undefined Phalcon\Acl
    $acl->setDefaultAction(Phalcon\Acl::DENY);

  • In public\index.php
    $app = new Micro($di);

Do I have to run composer to complete the installation? Do I need to update the composer.json file for newer versions of Phalcon. I assume I will to update it. What do I specify in the dependencies?
composer update gives below errors on command prompt:

  Problem 1
    - phalcon/devtools[v3.2.0, ..., v3.2.13] require ext-phalcon ~3.1 -> it has the wrong version installed (4.1.0).
    - phalcon/devtools[v3.4.0, ..., v3.4.14] require ext-phalcon ~3.3 -> it has the wrong version installed (4.1.0).
    - Root composer.json requires phalcon/devtools ^3.2 -> satisfiable by phalcon/devtools[v3.2.0, ..., v3.4.14].

Phalcon 4.1.0. is installed and shown by phpinfo(). I am using Php 7.4.26 and Apache 2.4.51.
TIA.

Regards
Amal

help setting it up... 500/404 errors

Hi, i'm using nginx - i first had the root pointed to /public folder and got 500 error while accessing via postman or compose test.
I changed it to root of application and now i'm gettign 404. When i visit the page https://api.mydomain.com - i get an error on page mod_rewrite need to be enabled... I do have equavelent mod_rewrite block in ngixn i believe like below

try_files $uri $uri/ @rewrite;

location @rewrite {
    rewrite ^/(.*)$ /index.php?_url=/$1;
}

on the environment config file i've not made any change except DB values, i've left the default values ( how do i generate keys?) 'authentication' => [
'secret' => 'your secret key to SIGN token', // This will sign the token. (still insecure)
'encryption_key' => 'Your ultra secret key to ENCRYPT the token', // Secure token with an ultra password
'expiration_time' => 86400 * 7, // One week till token expires

thanks for any help!

Language files not being picked up?

I have created file langs/en.json + es.json as instructed in readme.md but they are not being picked up, and I can't see anything for internationalization in the code. Any guidance would be appreciated. Thank you

Unable to setup this skeleton

Hi,

I am using Wamp on Windows 7, 64bit. I setup the whole project by downloading zip file and setting it up. Ran Composer. I setup the database connection details in server.development.php. The setting
SetEnv APPLICATION_ENV "development" doesn't work in Windows so I have setup a Environment variable for the same.

I haven't modified app.php. I am just trying to get it the basic website going so that I can do more. It keeps giving 500 "Internal Server Error". I have modified the .htaccess and cleaned it, just leaving basic commands but still this error doesn't go away. There is nothing in Apache or PHP error logs.

Do I need to setup some of the Postman configuration. I have no knowledge of that. I did have a look at that file. I haven't touched the ACL files also.
I just want to first see index.phmtl to know everything is working fine. Then I will go to next step. I am working on REST API for the first time so kindly pardon my ignorance.

Amal

Logging in & getting authentication token

Hi,

I am stuck at the first step. I have setup the environment with Wamp but I am unable to login. Pardon my lack of knowledge. I read most of the code in app.php, how to setup routes and acl etc. I then went deeper and read a few articles and tutorials about JWT on internet. I have never used it. How do I send my User/Password to the API. Earlier I had created a small REST API using node.js using a tutorial where we sent the credentials in a json object. I assume that will not work here.

I also understand that after authentication in Response object I will get the token which I have to use for all further calls. Second question, whose solution is most probably related to the first one, how will I send this token with each API call. I do see the config file server.development.php having space for the token and a key.

Amal

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.