orchestra/canvas-core
Core code generator builder for Laravel apps and packages. Provides a foundation for creating generators and scaffolding workflows, with test coverage and CI integration. Used by Orchestra Canvas ecosystem to streamline boilerplate creation.
Installation:
composer require orchestra/canvas-core
Publish the configuration file (if needed):
php artisan vendor:publish --provider="Orchestra\Canvas\Core\CanvasServiceProvider" --tag="config"
First Use Case: Generate a basic model with migrations, factory, and tests:
php artisan canvas:generate model User --with=migration,factory,test
config/canvas.php for available presets and options.Where to Look First:
config/canvas.php under the presets key for built-in generators.app/Canvas/ (auto-generated) for blueprints and generators.Standardized Generation: Use presets to enforce team conventions:
php artisan canvas:generate model Post --preset=api
api, admin) bundle related generators (e.g., model + controller + migration + API resource).Custom Blueprints: Extend existing generators by creating custom blueprints:
// app/Canvas/Blueprints/ModelBlueprint.php
namespace App\Canvas\Blueprints;
use Orchestra\Canvas\Core\Blueprints\ModelBlueprint as BaseModelBlueprint;
class ModelBlueprint extends BaseModelBlueprint
{
protected $customProperties = [
'softDeletes' => true,
'timestamps' => true,
];
}
Dynamic Generation:
Use actions (from orchestra/workbench) to conditionally generate code:
// app/Canvas/Actions/GenerateWithPolicy.php
namespace App\Canvas\Actions;
use Orchestra\Workbench\Actions\Action;
class GenerateWithPolicy extends Action
{
public function handle()
{
$this->call('canvas:generate policy', ['model' => $this->argument('model')]);
}
}
Integration with Artisan: Create custom commands that leverage Canvas:
// app/Console/Commands/GenerateModule.php
namespace App\Console\Commands;
use Illuminate\Console\GeneratorCommand;
use Orchestra\Canvas\Core\Canvas;
class GenerateModule extends GeneratorCommand
{
protected $signature = 'canvas:module {name}';
protected $description = 'Generate a full module with Canvas';
public function handle()
{
$canvas = app(Canvas::class);
$canvas->generate([
'model' => $this->argument('name'),
'with' => ['migration', 'factory', 'test', 'controller', 'policy'],
]);
}
}
app/Canvas/Blueprints/Auth/, app/Canvas/Blueprints/API/).config/canvas.php to standardize workflows across developers.Configuration Overrides:
generators is set to null in config/canvas.php, the package may fail silently. Ensure it’s an array:
'generators' => [
'model' => Orchestra\Canvas\Core\Generators\ModelGenerator::class,
// ...
],
Path Conflicts:
app/Canvas/Blueprints/ or be registered in the service provider. Misplaced blueprints will throw ClassNotFound errors.Deprecated Methods:
possibleModelsUsingCanvas() (soft-deprecated in v10.1.1). Use Laravel’s native possibleModels() instead.PHP Attributes:
composer.json:
"config": {
"platform-check": false,
"preferred-install": "dist"
}
Migration Generator:
Illuminate\Console\MigrationGeneratorCommand, ensure your custom generators extend Orchestra\Canvas\Core\Generators\MigrationGenerator for compatibility.Enable Debug Mode:
Set debug to true in config/canvas.php to log generator execution:
'debug' => env('CANVAS_DEBUG', false),
Check Generator Events:
Listen for generating and generated events to debug failures:
// app/Providers/CanvasServiceProvider.php
public function boot()
{
event(new \Orchestra\Canvas\Core\Events\Generating());
event(new \Orchestra\Canvas\Core\Events\Generated());
}
Validate Blueprints:
Use php artisan canvas:list to verify registered generators and blueprints.
Custom Generators:
Extend Orchestra\Canvas\Core\Generators\Generator to create domain-specific generators:
namespace App\Canvas\Generators;
use Orchestra\Canvas\Core\Generators\Generator;
class ApiResourceGenerator extends Generator
{
protected $type = 'api-resource';
protected $blueprint = 'api-resource';
}
Listeners:
Implement Orchestra\Canvas\Core\Contracts\GeneratesCodeListener to hook into the generation lifecycle:
namespace App\Canvas\Listeners;
use Orchestra\Canvas\Core\Contracts\GeneratesCodeListener;
class LogGeneration implements GeneratesCodeListener
{
public function afterCodeHasBeenGenerated($generator, $path)
{
\Log::info("Generated {$path} via {$generator}");
}
}
Preset Customization:
Override default presets in config/canvas.php:
'presets' => [
'api' => [
'model',
'migration',
'factory',
'test',
'controller',
'api-resource',
],
],
Template Overrides: Override default templates by publishing assets and modifying them:
php artisan vendor:publish --tag="canvas-views"
Edit files in resources/views/vendor/canvas/ to customize generated code.
How can I help you explore Laravel packages today?