Installation
composer require c975l/pageedit-bundle
Add to bundles.php (Laravel 5.5+ uses config/app.php):
return [
// ...
\C975L\PageEditBundle\C975LPageEditBundle::class,
];
Publish Assets Run:
php artisan vendor:publish --tag=pageedit-assets
This copies TinyMCE, jQuery, and Bootstrap assets to public/bundles/c975lpageedit/.
Configure Routes
Add routing in routes/web.php:
use C975L\PageEditBundle\Controller\PageEditController;
$router->mount('/_pageedit', PageEditController::class)
->methods(['GET', 'POST'])
->bind('pageedit');
First Use Case
Edit a Twig template (e.g., resources/views/homepage.html.twig) and access it via:
http://your-app.test/_pageedit/path/to/homepage
The page will render with an inline TinyMCE editor for editable regions (wrapped in {% block editable %}).
Mark Editable Regions Use Twig blocks to define editable content:
{% block editable %}
<h1>{{ block.content|default('Default Title') }}</h1>
<p>{{ block.content|default('Default paragraph.') }}</p>
{% endblock %}
Template Protection
Place templates in templates/protected/ to prevent edits via the UI. These remain editable via code.
Versioning
The bundle auto-archives old versions of edited files in var/pageedit_archive/. Retrieve past versions via:
$archive = $this->get('c975l_pageedit.archive');
$oldContent = $archive->getVersion('path/to/file', 1); // Version 1
Sitemap Integration
Generate a sitemap.xml dynamically in a controller:
use C975L\PageEditBundle\Service\SitemapService;
$sitemap = $this->get(SitemapService::class);
$xml = $sitemap->generate([
'/home' => ['changefreq' => 'daily', 'priority' => 0.9],
'/about' => ['changefreq' => 'weekly', 'priority' => 0.5],
]);
PDF Generation Convert a page to PDF in a controller:
use C975L\PageEditBundle\Service\PdfService;
$pdfService = $this->get(PdfService::class);
$pdfContent = $pdfService->generate('path/to/page');
file_put_contents('path/to/output.pdf', $pdfContent);
Asset Management
Override default assets by publishing and extending the pageedit template in your theme:
php artisan vendor:publish --tag=pageedit-assets --force
Then extend resources/views/bundles/c975lpageedit/base.html.twig.
Authentication
Secure the _pageedit route with middleware (e.g., auth or custom roles):
$router->mount('/_pageedit', PageEditController::class)
->methods(['GET', 'POST'])
->bind('pageedit')
->middleware('auth');
Custom Editors
Extend TinyMCE configuration by overriding the tinymce_config.json asset:
// public/bundles/c975lpageedit/tinymce_config.json
{
"plugins": ["advlist autolink lists link charmap print preview anchor"],
"toolbar": "undo redo | bold italic bullist numlist"
}
Event Listeners Hook into page save events to trigger custom logic (e.g., notifications):
// src/EventListener/PageEditListener.php
namespace App\EventListener;
use C975L\PageEditBundle\Event\PageEditEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
class PageEditListener implements EventSubscriberInterface
{
public static function getSubscribedEvents()
{
return [
'pageedit.save' => 'onPageSave',
];
}
public function onPageSave(PageEditEvent $event)
{
// Custom logic (e.g., log changes, send email)
}
}
Register in services.yaml:
services:
App\EventListener\PageEditListener:
tags:
- { name: kernel.event_subscriber }
Deprecation Warning
The bundle is deprecated in favor of c975L/SiteBundle. Migrate to the newer bundle for long-term support.
Twig Auto-Reloading Edits to Twig templates via the UI do not trigger Laravel’s template cache. Clear manually:
php artisan view:clear
File Permissions
Ensure var/pageedit_archive/ is writable by the web server:
mkdir -p var/pageedit_archive
chmod -R 775 var/pageedit_archive
TinyMCE Conflicts If TinyMCE fails to load, check for duplicate jQuery/Bootstrap includes. Use the bundle’s published assets exclusively.
PDF Generation Limits
PDF generation relies on external tools (e.g., wkhtmltopdf). Ensure dependencies are installed:
sudo apt-get install wkhtmltopdf # Debian/Ubuntu
Protected Templates
Templates in templates/protected/ cannot be edited via the UI, but they must still exist in the filesystem. Missing files will break the app.
Log Archive Issues Enable debug mode to log archive operations:
// config/packages/c975l_pageedit.yaml
archive:
debug: true
Check Route Conflicts
Verify _pageedit routes don’t clash with existing routes. Use:
php artisan route:list | grep pageedit
Twig Syntax Errors Validate Twig templates for syntax errors before editing via the UI. Use:
php artisan twig:lint resources/views/
Custom Archive Storage Override the archive service to use a database or cloud storage:
// config/services.yaml
services:
C975L\PageEditBundle\Service\ArchiveService:
arguments:
$storage: '@custom.archive.storage'
Dynamic Editable Regions Use Twig functions to conditionally enable editing:
{% if app.user.isAdmin %}
{% block editable %}
{{ block.content|default('Admin-only content') }}
{% endblock %}
{% endif %}
SEO Metadata Extend the sitemap with custom metadata:
$sitemap->addUrl('/blog/post', [
'changefreq' => 'monthly',
'priority' => 0.7,
'lastmod' => '2023-10-01',
'images' => ['/images/post-thumbnail.jpg'],
]);
Multi-Language Support
Use the bundle’s pageedit.translation service to manage translations:
$translations = $this->get('pageedit.translation');
$translations->set('path/to/page', 'en', 'English content');
$translations->set('path/to/page', 'fr', 'Contenu français');
Custom Validation Add validation rules for editable content via a subscriber:
public function onPageSave(PageEditEvent $event)
{
$content = $event->getContent();
if (strpos($content, 'malicious') !== false) {
throw new \RuntimeException('Invalid content detected.');
}
}
How can I help you explore Laravel packages today?