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

Discover How Tool Selection Impacts DataOps Success and Team Productivity

Modern businesses run on data. Every click, sale, and customer interaction generates information that companies use to make decisions. However, raw data is like crude oil; it…

Read More

Strategies to Monitor Multi-Cloud Data Pipelines and Prevent Failures

Introduction Today, data drives almost every major business decision. To keep up with massive amounts of information, organizations no longer rely on just one cloud provider. Instead,…

Read More

Improving Enterprise Data Governance Strategy Through Modern DataOps Platform Tools

Introduction Modern organizations run on data. Every transaction, customer click, and supply chain adjustment generates valuable information. However, managing this information at scale presents significant challenges. If…

Read More

Streamlining Enterprise DataOps Strategies With AI-Based Analytics Tools Safely

Introduction Data has become the lifeblood of modern businesses. However, managing vast amounts of information can quickly overwhelm traditional data teams. DataOps—the practice of bringing agility, continuous…

Read More

Navigating Your Ultimate Goa Itinerary: Best Places to Visit Safely

Introduction Planning a trip to India’s most iconic coastal paradise can feel overwhelming, but finding the absolute best places to visit in Goa doesn’t have to be…

Read More

The Ultimate Checklist for Planning Cosmetic Surgery Abroad Safely

Introduction The quest for self-improvement and aesthetic refinement has evolved from a localized luxury into a highly accessible global phenomenon. Today, patients seeking transformative treatments are no…

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