How to get list of all views file in laravel

Updated at 13-Apr-2021, By samar

How to get list of all views file in laravel

Hello everyone, in this post we will examine how to solve the "How to get list of all views file in laravel" programming puzzle.

  • Get all files of views directory in array

    //config\filesystems.php
    'disks' => [
        
        'views' => [
            'driver' => 'local',
            'root' => base_path('resources/views'),
        ],
    ],
    
    //Code to get the list of all view files.
    $files = Storage::disk('views')->files('');
    echo "<pre>";
    print_r($files);
    
    //Import storage facade before calling the disk method on Storage facade.
    use Illuminate\Support\Facades\Storage;
    

    You can get a list of all views files in laravel using this code snippet. This code snippet returns the files name in an array using the disk(‘views’)->files() method on Storage Facades. Use use Illuminate\Support\Facades\Storage;  to call Storage fasade in your controller file. It only returns the files name which are directly inside of the views directory.

     

    Array
    (
        [0] => bsform.blade.php
        [1] => get-ajax.blade.php
        [2] => home.blade.php
        [3] => posts.blade.php
        [4] => welcome.blade.php
    )
  • Get all files within a views directory including all sub-directories

    //config\filesystems.php
    'disks' => [
    
        // ...
    
        'views' => [
            'driver' => 'local',
            'root' => base_path('resources/views'),
        ],
    ],
    
    // app\Http\Controllers\<YourController>.php
    $files = Storage::disk('views')->allFiles('');
    echo "<pre>";
    print_r($files);
    

    This code snippet returns an array of all files within a given directory (views) including all sub-directories. Basically this method returns all the files which are directly inside of the views directory or in its sub-directory  in laravel. Here index.blade.php is the file under image directory which is a sub-directory of views directory.

     

    Array
    (
        [0] => bsform.blade.php
        [1] => get-ajax.blade.php
        [2] => home.blade.php
        [3] => image/index.blade.php
        [4] => posts.blade.php
        [5] => welcome.blade.php
    )

Back to code snippet queries related laravel