Weave Code
Code Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message

Lessphp Laravel Package

leafo/lessphp

leafo/lessphp is a PHP compiler for the LESS CSS language. Compile .less files to CSS in your apps or build scripts, with support for variables, mixins, nesting, imports, and other core LESS features, plus caching and CLI usage.

View on GitHub
Deep Wiki
Context7

Getting Started

Minimal Steps

  1. Installation

    composer require leafo/lessphp
    

    Add to composer.json if not using autoloading:

    "autoload": {
        "psr-4": {
            "App\\": "app/"
        }
    }
    
  2. Basic Compilation

    use lessc;
    
    $less = new lessc();
    $css = $less->compile('body { color: #ff0000; }');
    // Outputs: "body { color: rgb(255, 0, 0); }"
    
  3. First Use Case: Compile a LESS File

    $less = new lessc();
    $css = $less->compileFile('path/to/styles.less');
    file_put_contents('path/to/styles.css', $css);
    
  4. Where to Look First


Implementation Patterns

Workflows

  1. Compile on Demand (Dynamic)

    $less = new lessc();
    $css = $less->compile($lessCode);
    return response($css, 200)->header('Content-Type', 'text/css');
    
  2. Compile and Cache (Static)

    $cacheKey = 'styles_' . md5($lessCode);
    if (cache()->has($cacheKey)) {
        return cache()->get($cacheKey);
    }
    $less = new lessc();
    $css = $less->compile($lessCode);
    cache()->put($cacheKey, $css, now()->addHours(1));
    return $css;
    
  3. Integrate with Laravel Mix

    // webpack.mix.js
    mix.less('resources/less/app.less', 'public/css/app.css');
    

    Use lessphp as a fallback or for server-side compilation.

  4. Middleware for LESS Requests

    // app/Http/Middleware/CompileLess.php
    public function handle($request, Closure $next) {
        if ($request->path() === 'compile-less') {
            $less = new lessc();
            $css = $less->compile($request->input('less'));
            return response($css, 200)->header('Content-Type', 'text/css');
        }
        return $next($request);
    }
    

Integration Tips

  • Blade Directives

    // Register in AppServiceProvider
    Blade::directive('less', function ($expression) {
        $less = new lessc();
        return "<?php echo \$less->compile($expression); ?>";
    });
    

    Usage:

    @less('$variable: #ff0000; body { color: $variable; }')
    
  • Laravel Views

    // In a controller
    $less = new lessc();
    $css = $less->compile(view('less.styles')->render());
    return response($css, 200)->header('Content-Type', 'text/css');
    
  • API Endpoint for Dynamic Theming

    Route::post('/theme/compile', function (Request $request) {
        $less = new lessc();
        $css = $less->compile($request->input('less'));
        return response($css, 200)->header('Content-Type', 'text/css');
    });
    

Gotchas and Tips

Pitfalls

  1. Performance Overhead

    • Compiling LESS on every request is slow. Cache aggressively or use client-side compilation (e.g., less.js).
    • Example: Use now()->addDays(7) for cache TTL in production.
  2. File Paths and Permissions

    • Ensure lessc->compileFile() has correct paths and read/write permissions.
    • Use absolute paths or Laravel's public_path() helper:
      $css = $less->compileFile(public_path('css/styles.less'));
      
  3. Imports and Relative Paths

    • LESS @import paths are relative to the compiled file. Use absolute paths or resolve them dynamically:
      $less->setImportDir(public_path('css/imports'));
      
  4. Memory Limits

    • Large LESS files may hit PHP's memory_limit. Increase it temporarily:
      ini_set('memory_limit', '512M');
      
  5. Deprecated Features

    • Some LESS features (e.g., mixins with arguments) may behave differently. Test thoroughly.

Debugging

  • Enable Debug Mode

    $less = new lessc();
    $less->setFormatter('compressed'); // or 'compressed', 'compact', 'expanded'
    $less->setLogLevel(lessc::LOG_DEBUG);
    
  • Check for Errors

    try {
        $css = $less->compile($lessCode);
    } catch (lesscException $e) {
        Log::error('LESS Compilation Error: ' . $e->getMessage());
        return response('Compilation failed.', 500);
    }
    

Config Quirks

  • Custom Functions Register custom LESS functions:

    $less->registerFunction('myfunc', function($arg) {
        return $arg * 2;
    });
    
  • Environment-Specific Settings

    if (app()->environment('production')) {
        $less->setFormatter('compressed');
    } else {
        $less->setFormatter('expanded');
    }
    

Extension Points

  1. Pre/Post-Processing Hook into compilation with events (if supported) or wrap the compiler:

    $less = new lessc();
    $lessCode = preg_replace('/@import.*;/', '', $lessCode); // Remove imports
    $css = $less->compile($lessCode);
    
  2. Custom Output Filters Modify output after compilation:

    $css = $less->compile($lessCode);
    $css = str_replace('color: rgb(255, 0, 0);', 'color: #f00;', $css);
    
  3. Extend with Traits Create a custom compiler class:

    class CustomLessCompiler extends lessc {
        public function compileWithCache($lessCode) {
            // Custom caching logic
        }
    }
    
Weaver

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
yandex/translate-api
voku/simple_html_dom
league/flysystem-vfs
bkwld/upchuck
filament/spatie-laravel-tags-plugin
capell-app/block-library
axium/identity
cetria/laravel-dummy-models
cetria/reflection-helper
agropredict/sso-auth-bundle
evolvestudio/spam-protection
datacore/hub-sdk
develia/commons
cuci/prototurk-sdk
cuci/prototurk-sdk-symfony
develia/geo-bundle
dreamzy/livewire-charts
touchestate-sdk/php-sdk
22h/doctrine-garbage-collection-bundle
agtp/agtp-php