Skip to main content

Black Friday 2025! Only until December 1st: coupon FRIDAY25 for 40% off Yearly/Lifetime membership!

Read more here

realodix/urlhub

532 stars
3 code files
View realodix/urlhub on GitHub

app/Rules/StrAlphaUnderscore.php

Open in GitHub
use Illuminate\Contracts\Validation\Rule;
 
class StrAlphaUnderscore implements Rule
{
public function passes($attribute, $value)
{
return preg_match('/^[\pL\pM\pN_]+$/u', $value) > 0;
}
 
public function message()
{
return 'The :attribute may only contain letters, numbers and underscores.';
}
}

app/Http/Requests/StoreUrl.php

Open in GitHub
use App\Rules\StrAlphaUnderscore;
 
class StoreUrl extends FormRequest
{
public function rules()
{
return [
'custom_key' => ['nullable', 'max:20', new StrAlphaUnderscore, 'unique:urls,keyword'],
];
}
}

app/Http/Controllers/UrlController.php

Open in GitHub
use App\Rules\StrAlphaUnderscore;
use App\Rules\StrLowercase;
use App\Rules\URL\KeywordBlacklist;
 
class UrlController extends Controller
{
public function customKeyValidation(Request $request)
{
$v = Validator::make($request->all(), [
'keyword' => [
'nullable',
'max:20',
'unique:urls',
new StrAlphaUnderscore,
new StrLowercase,
new KeywordBlacklist,
],
]);
 
if ($v->fails()) {
return response()->json(['errors' => $v->errors()->all()]);
}
 
return response()->json(['success' => 'Available']);
}
}

We'd Love Your Feedback

Tell us what you like or what we can improve

Feel free to share anything you like or dislike about this page or the platform in general.