URLShortly is a URL shortener service built using Laravel for the backend, which handles URL storage, slug generation, and redirection.
- PHP 8.2 or higher
- Laravel 11
- Laragon (Optional, for local development)
- Node.js
- Composer
- Postman (for API testing)
- Create short URLs: Users can provide a long URL and an optional custom slug.
- Redirect to the original URL: Users are redirected to the original long URL when accessing the short link.
- Error handling: Provides appropriate error messages when the URL creation fails or the slug doesn't exist.
- Laravel 11: The PHP framework used for routing, validation, and API responses.
- MySQL: Database used to store URLs and slugs.
git clone https://github.com/handikatriarlan/URLShortly-be.git
cd URLShortly-be
Run the following command to install the required PHP dependencies using Composer:
composer install
Copy the .env.example
file to .env
:
cp .env.example .env
Generate the application key:
php artisan key:generate
Run the migrations to set up the database tables:
php artisan migrate
If you encounter any database connection issues, edit your .env
file to match your local database credentials.
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=laravel
DB_USERNAME=root
DB_PASSWORD=
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=shortlink
DB_USERNAME=handikatriarlan
DB_PASSWORD=arlan123
You will also need to install the Node.js dependencies for asset compilation:
npm install
Run the application using the built-in Laravel server:
php artisan serve
The application will be available at http://127.0.0.1:8000
.
In a separate terminal, run the following command to compile your assets:
npm run dev
To test the API, you can use Postman or any other API testing tool. Be sure to add the Access-Token
header with the correct token value (abc
in this case) for routes protected by the VerifyAccessToken
middleware.
POST /api/short-link
Request body:
{
"url": "https://your-long-url.com",
"slug": "your-slug"
}
Response:
{
"message": "Link created successfully"
}
GET /your-slug
Redirects to the original long URL associated with the provided slug.
- 404 Not Found: When a non-existent slug is accessed.
- 500 Internal Server Error: For database or other server-side issues.