FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

ulangmeier/php-baapi: Simple API routing in PHP! Brings the easyness of API Routing known in ExpressJS, Python FastAPI, Gin & Fiber. Now for PHP · GitHub

Repository files navigation

Business Application API Router for PHP.

API-Routing for PHP.

Create an industry-standard API in PHP that follows best-practices, without any hazzle.

You do not have to switch to Express.js, Python or Go. Create cool API routes that you know from these frameworks. But in PHP!

This api router gives PHP developers an easy way to create any api route.

Instead of methods, we use simple IF blocks in our api router. You do not need complicated METHODS and frameworks!

You don't believe me that it can be so simple? Just have a look:

Simple API Route without parameter:

Hello World API route:

if (route("/hello", GET)) {
   echo json_encode(["message" => "Hello, World!"]);
}

Route with parameter:

Say Hello to a specific x-being:

Note: $params is an array that contains the api parameter (here the value of the passed {name}).

if (route("/hello/{name}", "GET", $params)) {
  echo json_encode(["message" => "Hello, $params[0]!"]);
  exit;
}

Getting an user id:

if (route("/user/{id}", GET, $params)) {
  echo json_encode(["user_id" => $params[0]]);
  exit;
}

A more advanced route could be:

Creates a customer with the vip option:

if ( route("/customer/create/{id}/{name}/as/vip", PUT, $params) ) {
  echo json_encode(["message" => "Customer $params[0] with Name $params[1] was created as VIP."]);
  exit;
}

How to test & call the routes:

Just use CURL from your commandline:

curl -X GET http://your-website.com/api/v1/hello/Urs+Langmeier -H "Authorization: Bearer your-secret-token"

If you need additional debug information use the -i option (shows the HTTP-Request Header):

curl -X GET https://your-server.name/api/v1/hello/Urs+Langmeier -H "Authorization: Bearer your-secret-token" -i

How to define your own routes:

Please go to the routes.php file and define your API routes there.

Also you can place any other php file into this directory where you add more routes for a specific topic.

For example sms.php for SMS specific API routes, or language.php for the translation of language.

DON'T place other php files into this directory, because all php files that you place into this folder will be executed to find the requtested API route.

See above some example routes, you find these routes also as pre-defined examples in the routes.php file already.

Installation

Rename .env.example to .env and store your secret api token in the .env file! This will be your bearer token for all api-calls.

Adding the site to XAMPP for Windows

If you use XAMPP for Windows, you may use xampp-create-vhost.bat and start it with Admin privileges to setup the test site in XAMPP.

📌 Apache

If you use Apache, the .htaccess file is already doing it's job in /api/v1/. No additional steps are necessairy to make your api work. Just write your routes to /api/v1/routes.php or to any other php file in /api/v1/.

This is how the content of .htaccess looks like:

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ baapi.php [QSA,L]

📌 Nginx

If you are using Nginx, you can define the forwarding with location blocks in your server configuration.

Nginx location block configuration

Add this block to /etc/nginx/sites-available/default:

server {
    listen 80;
    server_name yourdomain.com;

    location /api/v2/ {
        rewrite ^/api/v2/(.*)$ /api/v2/index.php last;
    }

    location ~ \.php$ {
        fastcgi_pass unix:/run/php/php8.1-fpm.sock;  # Anpassen je nach PHP-Version
        fastcgi_index index.php;
        include fastcgi_params;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    }
}

What’s happening here?

  • All requests under /api/v1/ are forwarded to index.php.
  • PHP files are processed via fastcgi_pass.
  1. Restart Nginx:

    sudo systemctl restart nginx
  2. Call your api with http://your-domain.com/api/v2/hello

About

Simple API routing in PHP! Brings the easyness of API Routing known in ExpressJS, Python FastAPI, Gin & Fiber. Now for PHP

Resources

Stars

0 stars

Watchers

1 watching

Forks

Releases

Packages

Contributors

Languages


Back | FazBrowse Home | New Git URL