orchestra/canvas
Orchestra Canvas brings Laravel’s artisan make code generators to apps and packages. Use familiar make:* commands outside a Laravel install or with customizable namespaces and stubs, speeding scaffolding for controllers, models, migrations, and more.
Installation:
composer require --dev "orchestra/canvas"
For Laravel projects, the package auto-registers via Package Discovery. For standalone use, run commands via:
vendor/bin/canvas make:model Post
Configure Presets:
Generate a canvas.yaml preset file tailored to your project:
vendor/bin/canvas preset laravel
vendor/bin/canvas preset package --namespace="Your\Package\Namespace"
This sets default namespaces, paths, and stubs (e.g., App\Models\ for Laravel, Your\Package\Models\ for packages).
First Use Case:
Generate a model with migration (replaces php artisan make:model Post -m):
vendor/bin/canvas make:model Post -m
Verify the files are created in app/Models/ (Laravel) or src/Models/ (package) with correct namespaces.
Laravel Integration:
make:* commands seamlessly. Use php artisan make:model as usual—it now uses Canvas’s stubs and presets.Package Development:
vendor/bin/canvas make:job ProcessPayment --namespace="Your\Package\Jobs"
canvas.yaml to define paths.src (e.g., src/) for non-standard directories.Team Standardization:
canvas.yaml in version control to enforce consistent naming (e.g., App\Services\ for service classes).resources/stubs/model.stub) for project-specific templates.CI/CD Pipelines:
vendor/bin/canvas make:factory Post --model=Post
--namespace flags to generate isolated components.Stub Customization:
Extend default stubs by copying them from vendor/orchestra/canvas/stubs/ to your project’s resources/stubs/ directory. Canvas will auto-detect and use your overrides.
Example: Modify model.stub to include soft deletes by default:
use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Database\Eloquent\Model;
class {{ class }} extends Model
{
use SoftDeletes;
// ...
Dynamic Namespaces:
Use the --namespace flag for one-off commands:
vendor/bin/canvas make:controller API\V1\PostController --namespace="App\Http\Controllers\API\V1"
Laravel Service Provider:
For Laravel projects, register the Orchestra\Canvas\LaravelServiceProvider in config/app.php to enable Artisan command overrides:
'providers' => [
// ...
Orchestra\Canvas\LaravelServiceProvider::class,
],
Testing:
Use canvas in PHPUnit tests to scaffold test doubles:
public function testExample()
{
Artisan::call('canvas make:model TestModel');
// Test logic...
}
Namespace Conflicts:
canvas.yaml is misconfigured.--namespace or verify canvas.yaml paths. Example:
model:
namespace: App\Models
--verbose to see resolved namespaces:
vendor/bin/canvas make:model Post --verbose
Stub Overrides Not Loading:
resources/stubs/ are ignored.model.stub for make:model). Clear Composer’s cache if needed:
composer clear-cache
Laravel Artisan vs. Canvas CLI:
php artisan and vendor/bin/canvas.composer exec canvas in Laravel projects to ensure consistency:
composer exec canvas make:migration create_posts_table --create
Migration Paths:
database/migrations/ vs. src/Migrations/).migration.path in canvas.yaml:
migration:
path: src/Migrations
PHP Version Mismatches:
composer require "orchestra/canvas:^10.1"
--dry-run to preview generated files without writing them:
vendor/bin/canvas make:model Post --dry-run
vendor/bin/canvas stub:dump model
vendor/bin/canvas make:model --help
Custom Commands:
Extend Canvas by creating new stubs and commands. Example: Add a make:resource-collection command by:
resources/stubs/resource-collection.stub.Preset Templates:
Share custom presets across projects by extending the preset command. Example:
vendor/bin/canvas preset custom --namespace="App" --paths.src="src"
Integration with IDEs: Use Canvas in IDE templates (e.g., PHPStorm Live Templates) to auto-generate code snippets. Example:
canvas make:model ${NAME} -m
Git Hooks:
Automate boilerplate generation in pre-commit hooks. Example .git/hooks/pre-commit:
#!/bin/sh
composer exec canvas make:factory "$(git diff --name-only | grep '\.php$' | head -1 | sed 's/\.php$//')"
--namespace to generate classes in shared namespaces:
vendor/bin/canvas make:service PaymentProcessor --namespace="Company\Shared\Services"
vendor/bin/canvas make:model Post -m && \
vendor/bin/canvas make:factory Post && \
vendor/bin/canvas make:seeder PostSeeder
orchestra/testbench for testing:
composer require --dev "orchestra/testbench"
vendor/bin/canvas make:test PostTest --namespace="Tests\Unit"
How can I help you explore Laravel packages today?