A simple and efficient URL shortener built with Laravel. This application allows users to shorten long URLs, track redirections, and view analytics for each shortened URL.
- Shorten URLs: Convert long URLs into short, easy-to-share links.
- Redirection: Redirect users from the short URL to the original URL.
- Analytics: Track the number of redirections and view detailed logs.
- Caching: Improve performance with Redis.
- Custom Aliases: Optionally provide a custom alias for the short URL.
- Laravel: PHP framework for web applications.
- MySQL: Relational database for storing URLs and logs.
- Redis: In-memory caching for faster lookups.
- PHPUnit: For testing the application.
- PHP V8.2.12
- Composer V2.8.6
- MySQL
- Redis for caching
- Clone the repository:
git clone https://github.com/your-username/url-shortener.git cd url-shortener - Install dependencies:
composer install
- Set up the environment:
- Copy the
.env.examplefile to.env:cp .env.example .env
- Update the
.envfile with your database and cache settings:DB_CONNECTION=mysql DB_HOST=127.0.0.1 DB_PORT=3306 DB_DATABASE=url_shortener DB_USERNAME=root DB_PASSWORD= CACHE_DRIVER=redis REDIS_HOST=127.0.0.1 REDIS_PASSWORD=null REDIS_PORT=6379
- Copy the
- Generate an application key:
php artisan key:generate
- Run migrations:
php artisan migrate
- Start the development server:
php artisan serve
- Access the application:
Open your browser and navigate to
http://localhost:8000.
Endpoint: POST /shorten
Request:
{
"original_url": "https://example.com"
}Response:
{
"short_url": "http://localhost/abc123"
}Endpoint: GET /{shortCode}
Example:
GET http://localhost/abc123
Response:
Redirects to https://example.com.
Endpoint: GET /analytics/{shortCode}
Example:
GET http://localhost/analytics/abc123
Response:
{
"original_url": "https://example.com",
"short_url": "http://localhost/abc123",
"redirect_count": 5,
"redirects": [
{
"id": 1,
"url_id": 1,
"ip_address": "127.0.0.1",
"user_agent": "PostmanRuntime/7.26.8",
"referrer": null,
"created_at": "2023-10-01T12:34:56.000000Z",
"updated_at": "2023-10-01T12:34:56.000000Z"
}
]
}