Installation:
composer require laravel/lsp
Add the service provider to config/app.php:
'providers' => [
Laravel\Lsp\LspServiceProvider::class,
],
Configuration: Publish the config file:
php artisan vendor:publish --provider="Laravel\Lsp\LspServiceProvider" --tag="lsp-config"
Edit config/lsp.php to define your preferred language server settings (e.g., PHPStan, Psalm, Intelephense).
First Use Case: Run the language server locally for testing:
php artisan lsp:serve
Configure your IDE (e.g., VS Code) to connect to localhost:3200 via the Language Server Protocol (LSP) extension.
config/lsp.php: Central configuration for language servers, analysis tools, and IDE integrations.routes/lsp.php: Define custom LSP endpoints (if extending functionality).app/Providers/LspServiceProvider.php: Bootstrapping logic for the LSP server.Tool Integration:
config/lsp.php to define analysis tools (e.g., PHPStan, Psalm, Intelephense) with their respective configs:
'tools' => [
'phpstan' => [
'enabled' => true,
'config' => base_path('phpstan.neon'),
],
'psalm' => [
'enabled' => true,
'config' => base_path('psalm.xml'),
],
],
Custom Commands: Extend the LSP server with custom commands by publishing a command class and binding it in the service provider:
// In LspServiceProvider.php
$this->app->bind(LspCommand::class, function () {
return new CustomLspCommand();
});
IDE-Specific Configurations:
settings.json):
{
"laravel.lsp.server": "http://localhost:3200",
"laravel.lsp.tools": ["phpstan", "psalm"]
}
config/lsp.php to define IDE-specific overrides:
'ide' => [
'vscode' => [
'autocomplete' => true,
'hover' => ['phpdoc', 'source'],
],
],
Real-Time Feedback: Leverage the LSP server for real-time feedback during development:
Ctrl+Space).F12 (or equivalent) to jump to the definition of classes/methods.CI/CD Pipelines: Use the LSP server to validate code quality in CI by running:
php artisan lsp:validate
This triggers all enabled tools (e.g., PHPStan, Psalm) and fails the build on errors.
Monorepos:
Configure the LSP server to scan multiple projects by extending the LspServiceProvider:
public function boot()
{
$this->app['lsp']->addProject(base_path('../project-b'));
}
Custom Analysis:
Extend the LSP server to support custom analysis rules by creating a LspAnalyzer class and registering it:
$this->app->bind(Laravel\Lsp\Contracts\Analyzer::class, CustomAnalyzer::class);
Tool Conflicts:
config/lsp.php to prioritize one:
'tools' => [
'phpstan' => ['enabled' => true, 'priority' => 1],
'psalm' => ['enabled' => false], // Disable to avoid conflicts
],
Performance Overhead:
php artisan lsp:watch.--memory-limit in tool configs (e.g., phpstan.neon).IDE Connection Issues:
php artisan lsp:serve) and the IDE is configured to point to the correct URL (http://localhost:3200 by default).Caching:
php artisan lsp:clear-cache
Enable Verbose Logging:
Set the debug flag in config/lsp.php:
'debug' => env('APP_DEBUG', false),
Logs will appear in storage/logs/lsp.log.
Tool-Specific Debugging:
php artisan lsp:debug --tool=phpstan
storage/logs/phpstan.log).LSP Protocol Inspection:
Use the lsp:inspect command to dump raw LSP requests/responses:
php artisan lsp:inspect --request=textDocument/completion
Custom Language Servers:
Extend the LSP server to support additional languages (e.g., JavaScript, TypeScript) by implementing Laravel\Lsp\Contracts\LanguageServer.
Dynamic Tool Loading: Dynamically load tools based on environment variables or user input:
// In LspServiceProvider.php
$enabledTools = explode(',', env('LSP_ENABLED_TOOLS', 'phpstan,psalm'));
foreach ($enabledTools as $tool) {
$this->app['lsp']->enableTool($tool);
}
Webhook Integration: Trigger LSP analysis via webhooks (e.g., GitHub Actions) by exposing an API endpoint:
// In routes/lsp.php
Route::post('/webhook/analyze', [LspController::class, 'analyze']);
Custom LSP Features:
Add support for custom LSP features (e.g., code actions, document formatting) by extending the LspServer class:
class CustomLspServer extends Laravel\Lsp\LspServer
{
public function registerCustomFeatures()
{
$this->on('textDocument/codeAction', [$this, 'handleCodeAction']);
}
}
How can I help you explore Laravel packages today?