webapp/app/Http/Controllers/LinksController.php

32 lines
894 B
PHP

<?php
namespace App\Http\Controllers;
use App\User;
use Illuminate\Http\Request;
use Illuminate\Validation\ValidationException;
use Laravel\Lumen\Routing\Controller as BaseController;
class LinksController extends BaseController
{
public function add(Request $request)
{
/** @var User $user */
$user = $request->user()->first();
$email = $user->email;
$link = $request->input('link');
$link = filter_var($link, FILTER_VALIDATE_URL);
if ($link === false) {
return response()->json(['error' => 'Invalid link.'], 422);
}
$email_sent = mail($email, 'New link', $link);
if ($email_sent) {
return response()->json(['info' => 'Email sent.']);
}
return response()->json(['error' => 'An error occured while sending email'], 500);
}
}