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.
Type | Technology used |
---|---|
Core scripting language | PHP 8 |
Database (dev & test) | MySQL |
Unit testing | PHPUnit |
Dependency management | composer |
PHP >= 8.3 is recommended. An instance of active MySQL database with permissions to create/manage databases/users is needed.
git clone https://github.com/erykmika/trim-it.git
cd ./trim-it/
composer install
This assumes the global installation of composer.
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:
mysql -u <user> -p -d <database> < ./sql/create_schema.sql
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:
cd ./public/
php -S localhost:8080
Now the application will respond to the API requests on the specified addresss.
The application handles and serves JSON-encoded requests/responses.
Method | URL | Description |
---|---|---|
GET | /url/:hash | Get full URL |
POST | /hash | Generate URL hash |
{
"url": "<url to be shortened>"
}
{
"status": "<success/failure>",
"<hash/url>": "<hash/url>"
}
To launch tests, run the vendor-provided PHPUnit executable.
./vendor/bin/phpunit --testdox
The --testdox flag provides more user-friendly output of the tests.