How do check current dynamic request URL is broken in Laravel

Introduction

Have you ever encountered a broken URL while working with Laravel? It can be frustrating, especially when you’re trying to build a dynamic web application. In this article, we will explore how to check if the current dynamic request URL is broken in Laravel, and we’ll provide a live example to help you understand the concept better.

Understanding the Problem

Before we dive into the solution, let’s first understand what a broken URL is. A broken URL is a URL that leads to a webpage or resource that does not exist or cannot be accessed. This can happen due to various reasons such as incorrect routing, missing files, or database errors.

Step 1:- Create a route

Route::post('/guest_post_store', [App\Http\Controllers\Admin\GuestPostAdminController::class, 'guest_post'])->name('guest_post_store');

Step 2:- Create a controller

public function guest_post(Request $request)
    {
        log::info("guest_post id me guest_post aa rha hai.");
        $adminId = auth()->user()->id;
        log::info('Request Data:', $request->all());
        $url = $request->input('url');
        $title = $request->input('title');
        $admin_id = $request->input('admin_id');
        $admin_email = $request->input('admin_email');
        $u_org_slugname = $request->input('u_org_slugname');
        $u_org_organization_id = $request->input('u_org_organization_id');
        $u_org_role_id = $request->input('u_org_role_id');
        
        
        // Perform any validation or processing on the URL if needed

        // Check if the URL is working (you may need to customize this logic)
        $isUrlWorking = $this->checkIfUrlIsWorkingWithGuzzle($url);

        // Update the database based on the status
        $newGuestPost = Guest_post::updateOrCreate(
            ['title' => $title], // Use the actual column name in your table
            [
                'url' => $url, 
                'admin_id' => $adminId, 
                'admin_email' => $admin_email,
                'u_org_slugname' => $u_org_slugname,
                'u_org_organization_id' => $u_org_organization_id,
                'u_org_role_id' => $u_org_role_id,
                'status' => $isUrlWorking
            ]
        );
        $guestPosts = Guest_post::all();
        return response()->json(['success' => true, 'message' => 'URL status updated successfully', 'data' => $guestPosts]);
      
    }

    private function checkIfUrlIsWorkingWithGuzzle($url)
    {
        $client = new Client();
    
        try {
            $response = $client->get($url);
            return $response->getStatusCode() === 200;
        } catch (\Exception $e) {
            return false;
        }
    }

Step 3:- Create a blade page

<form method="post" id="sample_form" class="form-horizontal" enctype="multipart/form-data">
                                            @csrf
                                            <div class="form-group" id="name_form">
                                                <label class="control-label col-md-4">Title :- </label>
                                                <div class="col-md-12">
                                                    <input type="text" name="title" id="title" class="form-control" autocomplete="off" />
                                                    <!-- <span id="username" class="text-danger"></span> -->
                                                </div>
                                            </div>
                                            <div class="form-group" id="name_form">
                                                <label class="control-label col-md-4">URL :- </label>
                                                <div class="col-md-12">
                                                    <input type="url" name="url" id="url" class="form-control" autocomplete="off" />
                                                    <!-- <span id="username" class="text-danger"></span> -->
                                                </div>
                                            </div>

Step 4:- Create a model

Step 5:- Create a table

Output:-

As per your requirement change in code.

Related Posts

Advanced Certified MLOps Professional Program for Scalable AI Model Deployment Systems

Introduction The Certified MLOps Professional program from AIOpsSchool has emerged as a vital benchmark for engineers looking to bridge the gap between data science and production engineering….

Read More

Powerful Certified MLOps Engineer Program to Build Reliable ML Infrastructure

Introduction The integration of Machine Learning into production environments has created a significant gap between data science and traditional software engineering. The Certified MLOps Engineer program is…

Read More

Professional Skill Alignment Around MLOps Foundation Certification in Modern Workplaces

Introduction The MLOps Foundation Certification has emerged as a critical benchmark for professionals looking to bridge the gap between data science and production engineering. This guide is…

Read More

Certified AIOps Manager: Strategic Framework for Intelligent IT Operations

Introduction The Certified AIOps Manager program is a specialized training designed to help professionals lead the next wave of IT operations. This guide is for engineers and…

Read More

Advanced AIOps Architect Certification Roadmap for DevOps Engineers

Introduction The Certified AIOps Architect is a comprehensive professional program designed for engineers and architects who want to master the intersection of Artificial Intelligence and IT Operations….

Read More

Advanced Certified AIOps Professional Guide for Mastering AI Driven Operations Skills

Introduction Artificial Intelligence for IT Operations is the future of managing complex systems and large scale digital environments. The Certified AIOps Professional program is designed for those…

Read More
Subscribe
Notify of
guest
0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x