mirror of
https://github.com/archtechx/tenancy.git
synced 2025-12-12 11:34:03 +00:00
[4.x] URL generation, request data identification improvements (#1357)
* UrlGenerator: set defaults based on config; request data: move config to config file+resolver * Claude code adjustments * improve request data tests, simplify complex test in UrlGeneratorBootstrapperTest * url generator test: test changing tenant parameter name * request data identification: add tenant_model_column configuration * defaultParameterNames -> passQueryParameter * move comment * minor refactor in PathIdentificationTest, expand CLAUDE.md to include early identification section * Fix COLOR_FLAG * improve test name Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> * TenancyUrlGenerator: add a check for queryParameterName being null Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> * Fix code style (php-cs-fixer) --------- Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
This commit is contained in:
parent
f4cc99b317
commit
5f7fd38e5a
13 changed files with 440 additions and 126 deletions
32
CLAUDE.md
32
CLAUDE.md
|
|
@ -6,7 +6,9 @@ This file provides guidance to Claude Code (claude.ai/code) when working with co
|
|||
|
||||
### Testing
|
||||
- `composer test` - Run tests without coverage using Docker
|
||||
- `./test tests/TestFile.php` - Run an entire test file
|
||||
- `./t 'test name'` - Run a specific test
|
||||
- You can append `-v` to get a full stack trace if a test fails due to an exception
|
||||
|
||||
### Code Quality
|
||||
- `composer phpstan` - Run PHPStan static analysis (level 8)
|
||||
|
|
@ -75,6 +77,36 @@ All of these work as flags, i.e. middleware groups that are empty arrays with a
|
|||
- `universal` - Routes working in both contexts
|
||||
- `clone` - Tells route cloning logic to clone the route
|
||||
|
||||
### Early Identification
|
||||
|
||||
**Early identification** ensures tenancy is initialized before controller instantiation, which is critical for certain scenarios.
|
||||
|
||||
**When needed:**
|
||||
- Controllers using constructor dependency injection
|
||||
- Integration with packages that inject dependencies in constructors
|
||||
|
||||
**The Problem:**
|
||||
Laravel executes controller constructors and route model binding before route-level middleware runs, causing services to use central context instead of tenant context.
|
||||
|
||||
**Solutions:**
|
||||
1. **Avoid Constructor Injection** - Use method injection instead
|
||||
2. **Laravel's Native Solution** - Use controllers that implement `HasMiddleware` interface
|
||||
3. **Kernel Identification** - Add middleware to HTTP Kernel's global stack:
|
||||
|
||||
```php
|
||||
// In HttpKernel.php
|
||||
protected $middleware = [
|
||||
\Stancl\Tenancy\Middleware\InitializeTenancyByDomain::class,
|
||||
// other middleware...
|
||||
];
|
||||
```
|
||||
|
||||
Note you also need to flag the route with the `'tenant'` middleware if default route mode (set in config) isn't set to TENANT.
|
||||
|
||||
**Benefits:**
|
||||
- Constructor dependency injection receives tenant-aware services
|
||||
- Seamless integration with existing Laravel applications
|
||||
|
||||
### Testing Environment
|
||||
|
||||
Tests use Docker with MySQL/PostgreSQL/Redis. The `./test` script runs Pest tests inside containers with proper database isolation.
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue