20steps/bricks-custom-acme-demo-angular-bundle
Installation
composer require 20steps/bricks-custom-acme-demo-angular-bundle
Ensure your composer.json includes "20steps/bricks-custom-acme-demo-angular-bundle": "^1.0" under require.
Publish Assets Run the bundle’s asset publisher (if applicable):
php artisan vendor:publish --provider="20steps\BricksCustomAcmeDemoBundle\BricksCustomAcmeDemoBundle" --tag=public
Check resources/views/vendor/bricks-demo/ for Angular templates or directives.
First Use Case: Embedding Angular in Laravel Blade Add the bundle’s Angular entry point to your Blade template:
@include('bricks-demo::angular-entry')
This typically loads the Angular module and initializes it with Laravel’s API endpoints (e.g., /api/auth).
Laravel-Angular API Integration
routes/api.php) to expose endpoints consumed by Angular services.Route::get('/api/data', [DataController::class, 'index']);
DataService) call these endpoints via $http.get('/api/data').Blade + Angular Hybrid Views
@verbatim or @once directives to embed Angular components in Blade:
@verbatim
<app-dashboard></app-dashboard>
@endverbatim
app-dashboard) in its entryComponents.Dynamic Configuration
window.Laravel (global JS object):
// In Laravel (Blade)
<script>
window.Laravel = {!! json_encode(['csrfToken' => csrf_token()]) !!};
</script>
const csrfToken = window.Laravel.csrfToken;
Service Communication
AuthService):
// Angular
@Injectable()
export class AuthService {
login(credentials: any) {
return this.http.post('/api/login', credentials);
}
}
providers array.webpack.mix.js to bundle Angular assets:
mix.js('resources/js/bricks-demo/app.module.ts', 'public/js')
.angular()
.version();
this.http.post('/api/login', { email, password }, { withCredentials: true });
Asset Loading Order
$ is not defined errors.@stack directives to manage script ordering:
@stack('scripts')
// In a layout file
@push('scripts')
<script src="{{ asset('js/bricks-demo.bundle.js') }}"></script>
@endpush
CSRF Token Mismatch
$http requests must include the CSRF token. Use a request interceptor:
// Angular HTTP Interceptor
intercept(req: HttpRequest<any>, next: HttpHandler) {
const token = window.Laravel.csrfToken;
req = req.clone({ setHeaders: { 'X-CSRF-TOKEN': token } });
return next.handle(req);
}
Route Conflicts
/dashboard) that conflict with Angular routes. Use Laravel’s prefix or Angular’s pathLocationStrategy.Dependency Hell
package.json matches:
"dependencies": {
"@angular/core": "^12.0.0",
"@angular/common": "^12.0.0"
}
ERROR Error: ... messages. Common causes:
ModuleNotFoundError).window.Laravel is undefined).200 OK and CORS headers (Access-Control-Allow-Origin).Custom Directives Extend the bundle’s Angular directives by creating a child module:
@NgModule({
declarations: [CustomDirective],
imports: [BricksDemoModule],
exports: [CustomDirective]
})
export class CustomBricksModule {}
Register it in app.module.ts.
Laravel Service Providers Bind Angular-specific services to Laravel’s container:
// In a ServiceProvider
$this->app->singleton('angular.auth', function () {
return new AngularAuthService();
});
Access via app('angular.auth') in controllers.
Configuration Overrides Publish the bundle’s config and override defaults:
php artisan vendor:publish --tag=bricks-demo-config
Modify config/bricks-demo.php (e.g., API base URL).
Testing
Http facade to test API endpoints:
$response = $this->getJson('/api/data');
$response->assertStatus(200);
TestBed in a separate test suite.How can I help you explore Laravel packages today?