Check If Blade Template Exists in Laravel 10

Check If Blade Template Exists in Laravel 10

When dealing with a Laravel application dependent on dynamic Blade template names, it is crucial to verify the existence of the templates in the file system. Confirming the presence of a Blade template before attempting to render it can prevent runtime errors and enhance the overall stability of the user experience. This tutorial shows how to check if Blade template exists in Laravel 10.

The View facade provides a way to check the existence of a view. Utilizing the exists method will return a true value if the view exists.

app/Http/Controllers/TestController.php

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Response;
use Illuminate\Support\Facades\View;

class TestController
{
    public function index(): Response
    {
        if (!View::exists('test.index')) {
            return new Response('Blade template not exists');
        }

        return new Response();
    }
}

Leave a Comment

Cancel reply

Your email address will not be published.