Search code examples
laravelphpstormopcache

Enable opcache for locally developed Laravel projects in PhpStorm


How would I locally enable opcache while developing in a Laravel project within PhpStorm?


Solution

    1. Open Run -> Edit Configurations
    2. Create a new configuration by expanding Defaults, then select PHP Built-in Web Server
    3. Set the following values:
      • Host: localhost
      • Port: 80
      • Document root: select the public folder in the project
      • Check Use router script: and select server.php in Laravel's project root directory
      • Interpreter options: -d zend_extension=php_opcache.dll -d opcache.enable_cli=1 -d opcache.memory_consumption=128 -d opcache.max_accelerated_files=10000 -d opcache.validate_timestamps=0 -d opcache.save_comments=0
    4. Ensure that you have entered an appropriate name for the configuration (ex. Local - with opcache)
    5. Click Ok

    This will cache the PHP scripts when the program is being executed. No invalidation for re-caching occurs with the current configuration. So, now file watchers are needed to detect when changes are made.

    1. Open File -> Settings
    2. Navigate to Tools -> File Watchers
    3. Add a new watcher by clicking on the green plus symbol (+) and select <custom>
    4. From the File type drop-down list, select PHP and set the following values:
      • Scope: Project Files
      • Program: php
        • This will already need to be set in your machine's path environment or you'll need to set the exact file path
      • Arguments: -d zend_extension=php_opcache.dll -d opcache.enable_cli=1 -r "opcache_reset();"
      • Expand Advanced Options and make sure all checkboxes are UNCHECKED
      • All other default values should sufficient
    5. Set a descriptive name for the watcher and click Ok

    This watcher will now detect any changes made to PHP files in your project when a manual save is invoked and invalidate the entire cache to be rebuilt.

    For blades, repeat the exact same steps immediately above. However, select Blade as the File type (obviously). This ensures those changes are also reflected while developing locally.

    Note

    • This directly replaces the usage of php artisan serve [--port=80]
    • This assumes that the php_opcache.dll file already exists in your PHP/ext folder. Otherwise, refer to the PHP documentation to get it installed.
    • These instructions were developed on a Windows environment. However, I'm sure they'd be easily ported over to other OS environments as well.