Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
8 views

Write a PHP program to create a url shortener laravel (1)

Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views

Write a PHP program to create a url shortener laravel (1)

Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

Write a PHP program to create a url shortener

laravel.

First, make sure you have Laravel installed on your system. If not, you can install it using Composer.

Create a new Laravel project using Composer:

composer create-project --prefer-dist laravel/laravel url-shortener

Navigate into your project directory:

cd url-shortener

Set up your database in the .env file:

DB_CONNECTION=mysql

DB_HOST=127.0.0.1

DB_PORT=3306

DB_DATABASE=your_database_name

DB_USERNAME=your_database_username

DB_PASSWORD=your_database_password

Generate the URL Shortener controller:

php artisan make:controller UrlShortenerController

Define routes in routes/web.php:

use App\Http\Controllers\UrlShortenerController;

Route::get('/', [UrlShortenerController::class, 'index']);

Route::post('/shorten', [UrlShortenerController::class, 'shorten']);

Route::get('/{code}', [UrlShortenerController::class, 'redirect']);

Implement the controller methods in


app/Http/Controllers/UrlShortenerController.php:

<?php
namespace App\Http\Controllers;

use Illuminate\Http\Request;

use App\Models\Url;

class UrlShortenerController extends Controller

public function index()

return view('index');

public function shorten(Request $request)

$request->validate([

'url' => 'required|url'

]);

$inputUrl = $request->input('url');

$code = Url::generateUniqueShortCode();

Url::create([

'original_url' => $inputUrl,

'short_code' => $code

]);

return redirect('/')->with('shortened', url('/'.$code));

public function redirect($code)


{

$url = Url::where('short_code', $code)->firstOrFail();

return redirect($url->original_url);

Create the Url model:

php artisan make:model Url

Define the Url model in app/Models/Url.php:

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;

use Illuminate\Database\Eloquent\Model;

use Illuminate\Support\Str;

class Url extends Model

use HasFactory;

protected $fillable = ['original_url', 'short_code'];

public static function generateUniqueShortCode()

$code = Str::random(6);

while (self::where('short_code', $code)->exists()) {

$code = Str::random(6);

}
return $code;

Create views in resources/views:

Create index.blade.php:

<!DOCTYPE html>

<html>

<head>

<title>URL Shortener</title>

</head>

<body>

@if (session('shortened'))

<p>Shortened URL: <a href="{{ session('shortened') }}">{{ session('shortened') }}</a></p>

@endif

<form method="post" action="/shorten">

@csrf

<input type="text" name="url" placeholder="Enter URL">

<button type="submit">Shorten</button>

</form>

</body>

</html>

Create a new PHP file with an appropriate name for your migration. For example,
you could name it something like 2024_02_21_123456_create_urls_table.php to
follow Laravel's migration naming convention.

<?php

use Illuminate\Database\Migrations\Migration;

use Illuminate\Database\Schema\Blueprint;

use Illuminate\Support\Facades\Schema;

class CreateUrlsTable extends Migration


{

/**

* Run the migrations.

* @return void

*/

public function up()

Schema::create('urls', function (Blueprint $table) {

$table->id();

$table->string('original_url');

$table->string('short_code')->unique();

$table->timestamps();

});

/**

* Reverse the migrations.

* @return void

*/

public function down()

Schema::dropIfExists('urls');

Migrate the database:

php artisan migrate

That's it! You've created a basic URL shortener in Laravel. You can now run your
Laravel application using:

php artisan serve

You might also like