URL Shortener 1.0
A Laravel-based URL shortener application
Chargement...
Recherche...
Aucune correspondance
ShortUrlController.php
Aller à la documentation de ce fichier.
1<?php
2
4
6use Illuminate\Http\Request;
7use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
8use Illuminate\Support\Facades\Auth;
9use Illuminate\Support\Facades\Gate;
10use Random\RandomException;
11use Illuminate\Contracts\View\Factory;
12use Illuminate\Contracts\View\View;
13use Illuminate\Http\RedirectResponse;
14use Illuminate\Http\JsonResponse;
15
21{
22 use AuthorizesRequests;
29 protected ?\Illuminate\Contracts\Auth\Authenticatable $user;
30
35 public function __construct()
36 {
37 $this->user = Auth::user();
38 }
39
46 public function index(): Factory|View
47 {
48 $shortUrls = ShortUrl::where('user_id', $this->user->id)
49 ->orderByDesc('created_at')
50 ->paginate(10);
51
52 return view('shorturls.index', compact('shortUrls'));
53 }
54
60 public function create(): View
61 {
62 return view('shorturls.create');
63 }
64
73 public function store(Request $request): View|RedirectResponse
74 {
75 // Validate the original URL
76 $request->validate([
77 'original_url' => 'required|url',
78 ]);
79
80 // Generate a unique code using the Random library
81 do {
82 $code = $this->random();
83 } while (
84 ShortUrl::where('code',$code)->exists()
85 );
86
87 // Create a new shortened URL
88 ShortUrl::create([
89 'user_id' => $this->user->id,
90 'code' => $code,
91 'original_url' => $request->input('original_url'),
92 ]);
93
94 return redirect()->route('shorturls.index')->with('status','Shortened URL created successfully.');
95 }
96
103 public function random(): string
104 {
105 try {
106 $characters = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz';
107 $charactersLength = strlen($characters);
108 $randomCode = '';
109
110 for ($i = 0; $i < 6; $i++) {
111 $randomCode .= $characters[random_int(0, $charactersLength - 1)];
112 }
113
114 return $randomCode;
115 } catch (RandomException $e) {
116 throw new RandomException('Failed to generate random code: ' . $e->getMessage());
117 }
118 }
119
126 public function edit($id): Factory|View
127 {
128 $shortUrl = ShortUrl::findOrFail($id);
129 $this->authorize('update', $shortUrl);
130
131 return view('shorturls.edit', ['shortUrl' => $shortUrl]);
132 }
133
145 public function update(Request $request, $id): View|RedirectResponse
146 {
147 $shortUrl = ShortUrl::findOrFail($id);
148 $this->authorize('update', $shortUrl);
149
150 $request->validate([
151 'original_url' => ['required', 'url'],
152 ]);
153
154 $shortUrl->update([
155 'original_url' => $request->input('original_url'),
156 ]);
157
158 return redirect()->route('shorturls.index')->with('status','Shortened URL updated successfully.');
159 }
160
169 public function destroy($id): View|RedirectResponse
170 {
171 $shortUrl = ShortUrl::findOrFail($id);
172 $this->authorize('delete', $shortUrl);
173
174 $shortUrl->delete();
175 return redirect()->route('shorturls.index')->with('status', 'Shortened URL deleted successfully.');
176 }
177
186 public function incrementClicks(ShortUrl $shortUrl): JsonResponse
187 {
188 $shortUrl->increment('clicks');
189 $shortUrl->last_used_at = now();
190 $shortUrl->save();
191 $shortUrl->refresh();
192
193 return response()->json([
194 'success' => true,
195 'clicks' => $shortUrl->clicks // The new click count
196 ]);
197 }
198}
Class ShortUrlController Handles the logic for the shortened URLs.
random()
Generate a unique random code of 6 alphanumeric characters.
Illuminate Contracts Auth Authenticatable $user
incrementClicks(ShortUrl $shortUrl)
Increment the click count of a shortened URL.
index()
Display a paginated list of the user's shortened URLs.
store(Request $request)
Store a new shortened URL.
edit($id)
Display the form to edit a shortened URL.
create()
Display the form to create a new shortened URL.
update(Request $request, $id)
Update a shortened URL.
__construct()
Initialize a new instance of the controller.