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

Modern Cloud DataOps Platforms for Reliable Data Pipelines

Introduction Modern organizations depend heavily on data. Every department, from finance and sales to healthcare, manufacturing, marketing, and customer support, needs reliable data to make better decisions….

Read More

Advanced DataOps Monitoring Tools for Enterprises: A Comprehensive Implementation Guide

Introduction Enterprise data environments are becoming more complex as organizations depend on cloud platforms, data lakes, data warehouses, real-time pipelines, analytics tools, and automated workflows. When one…

Read More

The Ultimate Share Market for Beginners Guide to Smart Returns

Entering the world of equity investing can feel like stepping into a foreign country where everyone speaks a different language. The flashing tickers, fast-moving financial news charts,…

Read More

Evaluating SEO Reporting Software: Must-Have Features for Modern Enterprise

Introduction Modern marketing teams, digital agencies, and e-commerce brands juggle multiple disjointed tools to manage their online footprint. Hopping between single-purpose tools for keyword tracking, asset storage,…

Read More

Platform Engineering and GitOps: Enterprise Guide to Modern Delivery

Introduction DevOps has evolved from a niche engineering practice into a boardroom priority that directly impacts customer experience, revenue, and competitiveness. Yet many enterprises still struggle to…

Read More

Platform Engineering vs DevOps: The New Cloud Architecture Shift.

Introduction Modern software engineering moves at breakneck speeds. Organizations must deploy features rapidly while maintaining total system availability. Transitioning away from legacy architectures toward modern cloud infrastructure…

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