K
K
kaxa32012019-01-05 03:17:16
Laravel
kaxa3201, 2019-01-05 03:17:16

How to check if a URL is valid in Laravel?

How can I check if the url is valid?

public function addResource(Request $request)
    {
        $url = $request->post('url');
        $result = $this->service->saveContent($url);

        if ($result !== null) {

            return response()->json(['status' => 'ok', 'message' => 'Url has been added', 'data' => $result]);
        }

        return response()->json(['status' => 'error', 'message' => 'Error', 'data' => ['url' => $url]]);
    }

Answer the question

In order to leave comments, you need to log in

2 answer(s)
P
prostoprofan, 2019-01-05
@prostoprofan

https://www.regextester.com/94502

V
Victor, 2019-01-07
@v_decadence

There is an active_url validation rule.
If you only need to check the format, then you can use the regex rule and specify the google regular expression for the URL there.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question