An inspired version of Laravel File Viewer.
Lara File Previewer is a Laravel package designed to offer a seamless and efficient solution for file previews within your applications. This package builds upon the concepts of the Laravel File Viewer (Packagist version 1.0.2) developed by Vishal Sharma, incorporating several custom modifications to enhance its functionality.
Lara File Previewer provides the same core features as Laravel File Viewer but with additional improvements and adaptations tailored to better fit your needs. Whether you need to preview documents, images, or other file types, this package streamlines the process, making it easier to integrate and use within your Laravel projects.
To use Lara File Previewer, your application must meet the following requirements:
- PHP: ^7.4|^8.0
- Laravel Framework: ^9.0|^10.0
To install the package, you need to update your composer.json
file to include the repository and then require the package.
-
Add the repository to your
composer.json
file:Open your
composer.json
file and add the following to therepositories
section:
"repositories": {
"khutachan/lara-file-previewer": {
"type": "git",
"url": "https://github.com/khutachan/lara-file-previewer"
}
}
If the `repositories` section does not exist, you can add it.
-
Require the package:
Add the package to the
require
section in yourcomposer.json
:
"require": {
"khutachan/lara-file-previewer": "dev-main"
}
-
Run Composer Update:
After adding the repository and require entries, run the following command to install the package:
composer update khutachan/lara-file-previewer
This will install the package from the specified repository and make it available for use in your Laravel application.
- Publish the package assets:
php artisan vendor:publish --provider="Khutachan\LaraFilePreviewer\LaraFilePreviewerServiceProvider" --tag=assets
-
(Optional) Publish the package views (for UI customization):
This step is optional and allows you to customize the UI of the file previewer by modifying the package's views.
Note: This has only been tested on Laravel. The compatibility with other frameworks has not been verified.
php artisan vendor:publish --provider="Khutachan\LaraFilePreviewer\LaraFilePreviewerServiceProvider" --tag=views
To ensure optimal performance, it is recommended to open each file preview in a new blank tab. This approach helps to avoid potential conflicts with existing scripts and CSS styles in the user's project, ensuring that the preview operates smoothly and without interference.
Currently, the package supports only one type of file per view. Opening multiple files of the same type within a single view may cause conflicts, such as duplicate element IDs when scripts are executed. To prevent such issues, it is advisable to use separate views or tabs for different files of the same type.
Add the alias to your Laravel configuration:
In case you’re unsure where to add the alias: To integrate the package with Laravel, you'll need to add the alias to your Laravel configuration. Specifically, you should include the alias within the
config/app.php
file under thealiases
key. Here’s how you can do it:
- Open
config/app.php
: This file contains your application's service providers and aliases configuration.- Locate the
aliases
Array: Find thealiases
key in theconfig/app.php
file.- Add the Alias: Include the following line within the
aliases
array to register theLaraFilePreviewer
facade:By following these steps, you will ensure that the
LaraFilePreviewer
facade is properly registered and can be easily accessed throughout your Laravel application.
'aliases' => Facade::defaultAliases()->merge([
'LaraFilePreviewer' => Khutachan\LaraFilePreviewer\LaraFilePreviewerFacade::class, //This line registers the LaraFilePreviewer facade. Ensure that you place it within the aliases array in config/app.php.
])->toArray(),
Register the alias in your OctoberCMS - WinterCMS plugin:
namespace Author\Vendor;
class Plugin extends PluginBase
{
public function boot(){
//Add this line inside boot() method.
$this->app->register(\Khutachan\LaraFilePreviewer\LaraFilePreviewerServiceProvider::class);
}
}
use LaraFilePreviewer;
/**
* Generate a file preview for the given file name.
*
* This method constructs a file path and URL based on the provided filename,
* then creates an array of file data to be used for the preview. It calls
* the `LaraFilePreviewer::show()` method, which handles generating and returning
* the preview view as a string. This method's primary purpose is to format
* the necessary file data and pass it to the `show()` method. The `show()` method
* internally returns a view, which is rendered as HTML.
*
* Note: The `LaraFilePreviewer::show()` method is responsible for returning the
* view, so this method itself does not directly handle view rendering.
*
* @param string $fileName The name of the file to preview. This should be the
* relative path or name of the file to generate a preview for.
* @return string The HTML content for the file preview, generated by the
* `LaraFilePreviewer::show()` method.
*/
public function filePreview($fileName) : string
{
$filePath = 'public/'.$fileName;
$fileUrl = asset('storage/'.$fileName);
$fileData = [
[
'label' => __('Label'),
'value' => "Value"
]
];
return LaraFilePreviewer::show($fileName, $filePath, $fileUrl, $fileData);
}
Please see CHANGELOG for more information what has changed recently.
This package builds upon the ideas and concepts introduced in the Laravel File Viewer. The goal is to enhance and extend the functionality of file previewing in Laravel, offering a refined experience for developers.
We would like to acknowledge the original creator, Vishal Sharma, for their work on the Laravel File Viewer. This project serves as a tribute and a continuation of their innovative ideas.
This package is licensed under the MIT License. See the LICENSE file for more details.
A demo from Laravel File Viewer