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

Exploring Financial Operations Workflows in Modern Cloud Environments

Introduction The Certified FinOps Professional is the definitive benchmark for experts looking to master the intersection of finance, engineering, and business. As organizations transition from traditional data…

Read More

Strategic Certified FinOps Engineer integrates governance with cloud operations

Introduction The shift to cloud computing has fundamentally altered how businesses manage infrastructure, but it has also introduced significant financial complexities that many engineering teams struggle to…

Read More

Certified FinOps Manager Knowledge for Cloud Financial Governance

Introduction The shift toward cloud-native infrastructure has brought undeniable speed, but it has also introduced significant financial complexity. The Certified FinOps Manager is a professional designation designed…

Read More

Smart Career Growth Through Certified FinOps Architect Learning Journey

Introduction The Certified FinOps Architect is a professional certification designed to help engineers, cloud professionals, and managers optimize cloud financial operations and cost efficiency. This guide is…

Read More

CDOM – Certified DataOps Manager Learning Path for Modern Data Professionals

Introduction The CDOM – Certified DataOps Manager is a professional designation designed to bridge the gap between data engineering and operational excellence. This guide is written for…

Read More

Professional development journey using CDOA – Certified DataOps Architect

Introduction The CDOA – Certified DataOps Architect is a professional designation designed to address the unique challenges of managing and scaling data delivery in cloud-native environments. This…

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