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;
22 use AuthorizesRequests;
29 protected ?\Illuminate\Contracts\Auth\Authenticatable
$user;
37 $this->user = Auth::user();
46 public function index(): Factory|View
48 $shortUrls = ShortUrl::where(
'user_id', $this->user->id)
49 ->orderByDesc(
'created_at')
52 return view(
'shorturls.index', compact(
'shortUrls'));
62 return view(
'shorturls.create');
73 public function store(Request $request): View|RedirectResponse
77 'original_url' =>
'required|url',
84 ShortUrl::where(
'code',$code)->exists()
89 'user_id' => $this->user->id,
91 'original_url' => $request->input(
'original_url'),
94 return redirect()->route(
'shorturls.index')->with(
'status',
'Shortened URL created successfully.');
106 $characters =
'0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz';
107 $charactersLength = strlen($characters);
110 for ($i = 0; $i < 6; $i++) {
111 $randomCode .= $characters[random_int(0, $charactersLength - 1)];
115 }
catch (RandomException $e) {
116 throw new RandomException(
'Failed to generate random code: ' . $e->getMessage());
126 public function edit($id): Factory|View
128 $shortUrl = ShortUrl::findOrFail($id);
129 $this->authorize(
'update', $shortUrl);
131 return view(
'shorturls.edit', [
'shortUrl' => $shortUrl]);
145 public function update(Request $request, $id): View|RedirectResponse
147 $shortUrl = ShortUrl::findOrFail($id);
148 $this->authorize(
'update', $shortUrl);
151 'original_url' => [
'required',
'url'],
155 'original_url' => $request->input(
'original_url'),
158 return redirect()->route(
'shorturls.index')->with(
'status',
'Shortened URL updated successfully.');
169 public function destroy($id): View|RedirectResponse
171 $shortUrl = ShortUrl::findOrFail($id);
172 $this->authorize(
'delete', $shortUrl);
175 return redirect()->route(
'shorturls.index')->with(
'status',
'Shortened URL deleted successfully.');
188 $shortUrl->increment(
'clicks');
189 $shortUrl->last_used_at = now();
191 $shortUrl->refresh();
193 return response()->json([
195 'clicks' => $shortUrl->clicks
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.
destroy($id)
Delete a shortened URL.