https://github.com/erykmika/trim-it
URL Shortener REST API written in PHP 8
https://github.com/erykmika/trim-it
api mysql php php8 rest-api
Last synced: 5 months ago
JSON representation
URL Shortener REST API written in PHP 8
- Host: GitHub
- URL: https://github.com/erykmika/trim-it
- Owner: erykmika
- Created: 2024-02-21T00:17:48.000Z (over 2 years ago)
- Default Branch: main
- Last Pushed: 2024-03-24T19:57:19.000Z (over 2 years ago)
- Last Synced: 2025-10-03T13:47:56.615Z (9 months ago)
- Topics: api, mysql, php, php8, rest-api
- Language: PHP
- Homepage:
- Size: 47.9 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# trim-it
A simple REST API written in PHP 8 without using any particular frameworks. It enables a user to shorten ("trim") a URL and generate its hash that can be used to retrieve the original URL. The application code was structured in such a way it follows the MVC architecture pattern. **The project is still in development**.
## Tech stack
| Type | Technology used |
| ---------------------------- |----------------------------------------------------------------------|
| Core scripting language | PHP 8 |
| Database (dev & test) | MySQL |
| Unit testing | PHPUnit |
| Dependency management | composer |
## Project setup
PHP >= 8.3 is recommended. An instance of active MySQL database with permissions to create/manage databases/users is needed.
### Clone the repository
```sh
git clone https://github.com/erykmika/trim-it.git
```
### Install dependencies (composer)
```sh
cd ./trim-it/
composer install
```
This assumes the global installation of composer.
### Configure the database
Modify **include/db_config.php** and, optionally, **tests/test_db_config.php** to provide database credentials for a development and a testing database respectively. Run the **create_schema.sql** script for both databases to create a proper schema for the application:
```sh
mysql -u -p -d < ./sql/create_schema.sql
```
### Run the application
You can use the Apache HTTP Server to handle HTTP requests. You will have to expose the **public** directory in the configuration. An **.htaccess** file specifying rewriting rules is provided. For the sake of simplicity, you can also use the in-built PHP web development server:
```sh
cd ./public/
php -S localhost:8080
```
Now the application will respond to the API requests on the specified addresss.
## Endpoints
The application handles and serves JSON-encoded requests/responses.
| Method | URL | Description |
|-----------|---------------|-------------------|
| GET | /url/:hash | Get full URL |
| POST | /hash | Generate URL hash |
### POST JSON body structure
```json
{
"url": ""
}
```
### Response JSON body structure
```json
{
"status": "",
"": ""
}
```
## Unit testing
To launch tests, run the vendor-provided PHPUnit executable.
```sh
./vendor/bin/phpunit --testdox
```
The **--testdox** flag provides more user-friendly output of the tests.