How to add a WYSIWYG editor to Laravel: a practical integration plan
A WYSIWYG editor in a Laravel application is more than a form field with a toolbar. You have to install it, configure it, put it in a view and – above all – save what the editor sends correctly. This article walks through the whole process using the veditor/laravel package as an example – from co…
This post is also available in: Polski
A WYSIWYG editor in a Laravel application is more than a form field with a toolbar. You have to install it, configure it, put it in a view and – above all – save what the editor sends correctly. This article walks through the whole process using the veditor/laravel package as an example – from composer require to the tests worth having before your first deployment.
TL;DR
- The
veditor/laravelpackage supports Laravel 11–13 and PHP 8.2+; the service provider registers itself. - Configuration goes into
config/veditor.php, keys (licence, AI) into.env. - The editor field in a form is a single directive:
@veditor('content', old('content', $article->body)). - Before saving, content goes through
VEditorService::sanitize()– that is the security boundary, not the editor in the browser. - Keep the AI endpoint behind the
authmiddleware, because it calls a paid API.
What a Laravel form should handle
Before you add an editor, decide what you expect from the form. In a typical Laravel CMS the list is short:
- an HTML content field that comes back with what the editor typed after a validation error (
old()), - CSRF protection, as in any form (
@csrf), - authorisation – only a permitted user can edit and save,
- HTML cleaning on the server before saving to the database,
- optionally: image uploads to your endpoint, your own file manager window and AI features.
The first four points are mandatory whichever editor you use. You switch on the rest with keys in the package configuration – without them the editor works as a plain rich text field.
VEditor requirements and installation
The veditor/laravel package requires PHP 8.2+ and works with Laravel 11, 12 and 13 (tested with Laravel 12). You install it with Composer from VEditor's private repository, where the password is your licence key – the installation documentation explains how to add the repository. Then:
composer require veditor/laravel
php artisan vendor:publish --tag=veditor-config # config/veditor.php
The service provider is discovered automatically (extra.laravel.providers), so you do not add it to bootstrap/providers.php by hand. You do not need npm or Vite either – the editor files are served by a package route, not built together with your frontend assets.
Add the keys to .env:
VEDITOR_LICENSE_KEY="VED1.…"
OPENAI_API_KEY="…" # only for the AI Edition
On local and test addresses no licence key is needed, so you can skip it during integration.
Configuring config/veditor.php
The published file contains several groups of settings:
routes– the route prefix (veditorby default), the AI endpoint middleware (['web', 'auth']by default) and the editor file middleware (none by default, because the files are the same for everyone),licenseKeyandlanguage– the key fromVEDITOR_LICENSE_KEYand the editor language;nullmeansapp()->getLocale(),styles– classes from your CSS that the editor shows in the “Style” list and the sanitizer keeps on save,editor– other editor options for all fields, e.g. height or the toolbar,filePickerUrl,uploadUrl,uploadField– the file manager window and the endpoint for dropped images,aiProvider,aiApiKey,aiTextModeland related keys – AI features; withoutaiApiKeythe editor shows no AI buttons.
The package registers two routes. GET /veditor/assets/{version}/{file} serves only .js and .css files from the package directory, cached for a year. The version number in the URL changes with every update, and the controller returns 404 for any version other than the current one – browsers will not be left with an old script. The second route, POST /veditor/ai, handles AI features and requires a logged-in user by default. Keep auth or replace it with your own middleware that lets in only people who edit content.
If you enable AI, in AppServiceProvider::boot() you can restrict features by role (aiFeaturesUsing()) and add a daily request limit (beforeAiRequestUsing() with RateLimiter). Details and an example are in the Laravel integration documentation.
Adding the editor field to a Blade view
In the edit form, put the directive where your <textarea> used to be:
<form method="post" action="{{ route('articles.update', $article) }}">
@csrf
@method('PUT')
@veditor('content', old('content', $article->body), ['height' => '400px'])
<button>Save</button>
</form>
The first argument is the field name the content will arrive under in the request, the second is the initial value, the third – optional editor options. The directive outputs the styles, a <textarea> with the content and the script that creates the editor. That is the minimum for the integration; why it is a directive rather than a component, how to display the finished article and how to configure a CSP nonce are covered separately in How to integrate a WYSIWYG editor with Blade templates.
Receiving and validating content
In the controller the order is fixed: authorisation, validation, sanitisation, saving.
use VEditor\Laravel\VEditorService;
public function update(Request $request, Article $article, VEditorService $veditor)
{
$this->authorize('update', $article);
$request->validate(['content' => ['required', 'string']]);
try {
$article->body = $veditor->sanitize($request->input('content'));
} catch (\LengthException) {
return back()->withInput()->withErrors(['content' => 'The content is too long.']);
}
$article->save();
return redirect()->route('articles.edit', $article);
}
VEditorService is a singleton in the container, so you inject it into the method like any other dependency. Validation here only checks that the field exists and is a string – it does not judge whether the HTML is safe. The try block is needed because the sanitizer rejects documents larger than the limit (1 MB by default) with a LengthException. Without handling it, an editor who pastes a huge document sees a 500 error instead of a message next to the field.
Sanitising HTML before saving
The sanitize() call in the controller above is the most important line of the whole integration. The method uses VEditor\HtmlSanitizer with the classes configured in styles – the same ones the editor shows in the “Style” list. Content produced by the editor passes through unchanged; what the editor would not produce is removed – for example HTML sent by bypassing the form.
Remember that this is not the only way content gets in. If the application has an API, an import from an old CMS or a bulk editing panel, each of them must pass HTML through the same sanitizer. The content model, XSS test scenarios and extending the list of allowed elements are covered in Laravel: a rich text editor with server-side HTML sanitisation.
Integration tests and deployment
A few PHPUnit feature tests will catch the most common integration mistakes:
public function test_update_strips_script(): void
{
$user = User::factory()->create(); // a user the policy allows to edit
$article = Article::factory()->create();
$this->actingAs($user)
->put(route('articles.update', $article), ['content' => '<p>Text</p><script>alert(1)</script>'])
->assertRedirect();
$this->assertStringNotContainsString('<script', $article->fresh()->body);
}
It is also worth checking:
- that content saved in the editor comes back from the sanitizer identical – VEditor's test application compares the output of
sanitize()with the submitted content after normalising line endings (\r\n→\n), - that a document over the limit ends with a validation message, not a server error,
- that
POST /veditor/aidoes not let in an anonymous user, - that the editor file route returns 200 for the current version.
Remember two things when deploying. After a package update the editor file URLs change by themselves, so you do not need to clear browser or CDN caches. If you use AI features behind nginx, the controller sends the X-Accel-Buffering: no header with streamed responses, and in Apache it disables mod_deflate for them – you do not have to configure this by hand, but no proxy along the way may buffer the response.
A practical pre-launch checklist
- Package installed,
config/veditor.phppublished, licence key in the production.env. - The form has
@csrf, and the field value comes fromold('content', …). - The controller authorises the user and calls
sanitize()before every save. LengthExceptionreturns a validation error instead of a 500 error.- The AI endpoint is protected by
author your own middleware, and request limits are set. - Classes from your theme are listed in
styles. - Feature tests for saving pass in CI.
Where does VEditor fit in this process?
VEditor runs entirely on your server: the editor files are served by a package route, with no npm and no external CDN. In Laravel the whole integration comes down to three things – the configuration file, the @veditor directive in the form and a call to VEditorService::sanitize() in the controller. The PHP sanitizer uses the same model of allowed HTML as the JavaScript editor, so valid content is not changed on save. Authorisation, secure file uploads and application tests remain your responsibility.
See VEditor features, the integration documentation and the editor demo.
The same pattern – assets from your own server, sanitisation on save, CSRF, CSP and the AI endpoint – applies to all VEditor integrations. A comparison of Laravel, Symfony, Yii2, plain PHP, React and Vue is in the guide to integrating a WYSIWYG editor with PHP and JavaScript.
FAQ
Does VEditor need npm or Vite to build the editor?
The VEditor documentation describes an integration that does not build the editor itself with npm. Deployment details and limitations still depend on your application and its infrastructure.
Does a staging environment need a licence?
No. On local and test addresses (e.g. localhost, .test domains, private addresses, subdomains such as staging.example.com) the editor works without a licence key; AI features there still need an AI Edition key, e.g. a free trial key. Details are in the licensing documentation.
Where should image upload handling go?
File handling has to be designed in your application: authentication, limits, MIME types, permissions and safe URLs. VEditor can send dropped images to your upload endpoint (uploadUrl in config/veditor.php) and work with your own file manager (filePickerUrl).
How do I display saved HTML safely?
Clean the content on the server first, then render it only in the intended HTML context. Do not output unverified user input directly.
Does the integration work with Laravel 11?
Yes, according to the documentation VEditor supports Laravel 11–13. Before going to production, test PHP versions, extensions, CSP, forms and authorisation.
Try VEditor in practice
The best way to judge the integration is on your own project: install the package, connect one form and run the save tests.