carone/laravel-content

A light-weight CMS for Laravel projects

Installs: 2

Dependents: 0

Suggesters: 0

Security: 0

Stars: 0

Watchers: 0

Forks: 0

Open Issues: 0

pkg:composer/carone/laravel-content

1.0 2025-12-31 14:25 UTC

This package is auto-updated.

Last update: 2025-12-31 14:27:47 UTC


README

A lightweight, developer-friendly content management package for Laravel applications. This is not a full-fledged CMS, but rather a simple solution for managing editable text and images directly in your views, perfect for quick content updates and typo fixes without deploying new code.

Features

  • ๐Ÿš€ Simple Integration - Drop-in Blade components for editable content
  • ๐Ÿ“ Text, Image & File Support - Manage text content, image paths, and file downloads
  • ๐ŸŽจ Built-in Visual Editor - Clean, modern interface for managing all your content
  • ๐Ÿ”’ Authentication Aware - Only show edit indicators to authenticated users
  • โšก Performance Optimized - Built-in caching support for fast content retrieval
  • ๐ŸŽฏ Customizable - Extensive configuration options
  • ๐Ÿ”ง Developer-Friendly - Minimal setup, maximum flexibility

Installation

Install the package via Composer:

composer require carone/laravel-content

Publish Configuration

Publish the configuration file to customize the package settings:

php artisan vendor:publish --provider="Carone\Content\CaroneContentServiceProvider" --tag="config"

This creates a config/content.php file where you can configure routes, middleware, caching, and default values.

Run Migrations

Publish and run the migrations to create the page_contents table:

php artisan vendor:publish --provider="Carone\Content\CaroneContentServiceProvider" --tag="migrations"
php artisan migrate

Publish Views (Optional)

If you need to customize the component views:

php artisan vendor:publish --provider="Carone\Content\CaroneContentServiceProvider" --tag="views"

Usage

Basic Text Content

Use the editable-text component to create editable paragraphs:

<x-editable-text element="about-me-text" />

With custom classes and attributes:

<x-editable-text element="hero-title" class="text-4xl font-bold text-gray-900" />

Image Content

Use the editable-image component for editable images:

<x-editable-image element="company-logo" />

With custom attributes:

<x-editable-image element="hero-banner" class="w-full h-64 object-cover" alt="Hero Banner" />

File Downloads

Use the editable-file component for downloadable files:

<x-editable-file element="terms-and-conditions" />

With custom link text:

<x-editable-file element="user-manual" text="Download User Manual" class="btn btn-primary" />

By default, the component displays the filename as link text. Use the text attribute to customize it.

Content Editor

The package includes a beautiful, built-in visual editor for managing all your content.

Accessing the Editor

Navigate to the editor at:

https://yoursite.com/admin/content

(The prefix is configurable via CONTENT_ROUTE_PREFIX)

Editor Features

  • ๐Ÿ“‹ Page List - View all pages with accurate content counts in the sidebar
  • โœ๏ธ Inline Editing - Edit content values directly with instant save
  • โž• Add Content - Add new content with dynamic component code snippets
  • ๐Ÿ“‹ Copy to Clipboard - Copy component code with one click
  • ๐Ÿ—‘๏ธ Delete Content - Remove individual content items
  • ๐Ÿ—‘๏ธ Delete Pages - Remove entire pages with all their content
  • ๐Ÿ” Route Explorer - Discover and quick-add content for any application route
  • ๐Ÿ’พ Auto-save - Changes are saved immediately with visual feedback

Dynamic Component Snippets

When adding new content, the editor shows you the exact Blade component code to add to your view, updated in real-time based on your inputs:

  • Updates automatically as you type the element ID
  • Changes when you select a different content type
  • One-click copy to clipboard
  • Example: <x-editable-text element="hero-title" />

Delete Pages

Each page view includes a "Delete Page" button that removes the entire page and all its content items. This feature is also available when page loading fails, allowing you to clean up corrupt or invalid page entries.

Route Explorer

The editor includes a powerful "Route Explorer" feature that helps you quickly start managing content for any page in your application:

  1. Click "Discover Routes" at the bottom of the sidebar
  2. Browse all your application's web routes
  3. Click "Quick Add" next to any route to:
    • Add the page to your sidebar
    • Automatically open the "Add Content" modal
    • Start creating content immediately

Pages added via Route Explorer are tracked in the frontend until you save your first content item, making it easy to explore without cluttering your database.

API Routes

The editor uses the following API endpoints (all under /api/admin/content by default):

  • GET /page/{pageId} - Fetch content for a specific page
  • POST /content - Create or update content
  • DELETE /content/{id} - Delete a content item
  • DELETE /page/{pageId} - Delete an entire page with all content
  • GET /routes - Get all application routes

These routes automatically include the api middleware and can be further protected via your config.

Editor Access Control

By default, the editor is protected by the middleware defined in your config:

'middleware' => [
    'web',
    'auth',
],

Add additional middleware for fine-grained control:

'middleware' => [
    'web',
    'auth',
    'can:manage-content',  // Require specific permission
],

How It Works

  1. Define Content Areas - Add <x-editable-text>, <x-editable-image>, or <x-editable-file> components in your views with unique element identifiers
  2. View Your Pages - Content displays with default values when not yet defined
  3. Edit Content - Use the visual editor to update content or authenticated users see edit indicators on the frontend
  4. Store in Database - Content is stored per page and element combination

Content Retrieval

The package provides a get_content() helper function that retrieves all content for the current route:

$content = get_content();
$value = $content->get('about-me-text');

Content is automatically cached based on your configuration settings for optimal performance.

Console Commands

The package provides convenient Artisan commands for managing content:

Create Content

Interactively create new page content:

php artisan content:create

With options for automation:

php artisan content:create --page=home --element=hero-title --type=text --value="Welcome to our site"

Features:

  • โœ… Page ID validation (prevents invalid routes like /)
  • โœ… Duplicate detection with update option
  • โœ… Context-aware prompts based on content type
  • โœ… Automatic cache clearing
  • โœ… Shows component code to add to your view

List Content

Display all page content in a clean table:

php artisan content:list

Filter by page or type:

php artisan content:list --page=home
php artisan content:list --type=image
php artisan content:list --page=about --full  # Show full values

Features:

  • ๐Ÿ“‹ Grouped by page when showing all content
  • ๐ŸŽจ Color-coded content types
  • โœ‚๏ธ Automatic value truncation (use --full to disable)

Clear Content

Clear content from the database:

# Clear all content (with confirmation)
php artisan content:clear

# Clear specific page
php artisan content:clear --page=home

# Skip confirmation
php artisan content:clear --force

Features:

  • โš ๏ธ Confirmation prompts for safety
  • ๐Ÿ—‘๏ธ Shows what will be deleted before clearing
  • ๐Ÿงน Automatic cache clearing

Configuration

The config/content.php file provides extensive customization options:

Table Name

Customize the database table name:

'table_name' => env('CONTENT_TABLE_NAME', 'page_contents'),

Route Configuration

Configure the admin routes prefix:

'route_prefix' => env('CONTENT_ROUTE_PREFIX', 'admin/content'),

Middleware Protection

Protect your content management routes with middleware:

'middleware' => [
    'web',
    'auth',
    // Add custom middleware like 'can:manage-content'
],

Default Values

Set default text, images, and files when content is not yet defined:

'defaults' => [
    'text' => env('CONTENT_DEFAULT_TEXT', '-- No content available --'),
    'image' => env('CONTENT_DEFAULT_IMAGE', 'images/placeholder.png'),
    'file' => env('CONTENT_DEFAULT_FILE', 'files/placeholder.pdf'),
],

Caching

Enable or disable caching and configure cache duration:

'cache' => [
    'enabled' => env('CONTENT_CACHE_ENABLED', true),
    'ttl' => env('CONTENT_CACHE_TTL', 3600), // 1 hour
    'key_prefix' => env('CONTENT_CACHE_PREFIX', 'laravel_content_'),
],

Environment Variables

You can set these in your .env file:

CONTENT_TABLE_NAME=page_contents
CONTENT_ROUTE_PREFIX=admin/content
CONTENT_DEFAULT_TEXT="Content coming soon..."
CONTENT_DEFAULT_IMAGE=images/default.png
CONTENT_DEFAULT_FILE=files/default.pdf
CONTENT_CACHE_ENABLED=true
CONTENT_CACHE_TTL=7200

Database Structure

The package creates a page_contents table with the following structure:

Column Type Description
id bigint Primary key
page_id string Route name or page identifier
element_id string Unique element identifier
type enum Content type ('text', 'image', or 'file')

Page ID Validation

Page IDs must follow these rules to ensure valid URLs:

  • โœ… Valid: home, about, contact, blog/post-1, products/category
  • โŒ Invalid: / (root route), paths with // (double slashes)
  • Must start and end with alphanumeric characters
  • Can contain: letters, numbers, hyphens (-), underscores (_), dots (.), forward slashes (/)

Note: The root route / cannot be used as a page ID. Use home or another identifier instead.

The editor's Route Explorer will automatically:

  • Display a warning for routes with invalid page IDs
  • Suggest alternatives (e.g., home for /)
  • Disable the "Quick Add" button for invalid routes

API Routes

The package registers the following routes (under your configured prefix):

Method URI Description
GET /pages List all pages with content
POST /pages Create/update page content
GET /pages/{page} Show specific page content
PUT/PATCH /pages/{page} Update page content
DELETE /pages/{page} Delete page content

Use Cases

Perfect for:

  • โœ… Landing pages with editable hero sections
  • โœ… About pages with team member bios and photos
  • โœ… Contact information that needs occasional updates
  • โœ… Feature descriptions and marketing copy
  • โœ… Footer content and legal text
  • โœ… Downloadable files (PDFs, documents, etc.)
  • โœ… Quick typo fixes without redeployment

Not ideal for:

  • โŒ Complex content hierarchies
  • โŒ Multi-language content (no built-in i18n)
  • โŒ Rich text editing with formatting
  • โŒ Actual file uploads (only stores paths/URLs)
  • โŒ Content versioning and workflows

Requirements

  • PHP 8.2 or higher
  • Laravel 12.0 or higher

Security

The package is designed to work with Laravel's built-in authentication. The editable indicators only show to authenticated users via auth()->check().

Important: Ensure you protect your content management routes with appropriate middleware to prevent unauthorized access.

Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

License

This package is open-sourced software licensed under the MIT license.

Author

Caron Emile
Email: emile.caron@constructiv.be

Support

For issues, questions, or suggestions, please open an issue on GitHub.

Note: This package provides the foundation for content management but does not include an admin UI for editing content. You'll need to implement your own editor interface or integrate with your existing admin panel.