Installation Add the bundle via Composer:
composer require coopers-peele/terms-bundle
Enable it in config/bundles.php:
return [
// ...
CoopersPeele\TermsBundle\CPTermsBundle::class => ['all' => true],
];
Database Setup
Run migrations (check Resources/migrations/ for schema):
php bin/console doctrine:migrations:diff
php bin/console doctrine:migrations:migrate
First Use Case: Basic Terms Display
Use the tos Twig extension to render terms:
{{ tos('latest') }}
Or fetch the latest terms programmatically:
$terms = $this->get('cp_terms.manager')->getLatestTerms();
Managing Terms Versions
/admin/terms) to create/update versions.$diff = $this->get('cp_terms.manager')->getVersionDiff(1, 2);
User Agreement Tracking
cp_terms.signup behavior (configure in config/packages/cp_terms.yaml):
cp_terms:
behaviors:
signup: true
$agreed = $this->get('cp_terms.manager')->userAgreed($user, $terms);
Frontend Integration
{% if tos('latest').hasChanges %}
<div class="changes-highlight">
{{ tos('latest').diffWithPrevious()|raw }}
</div>
{% endif %}
$this->get('cp_terms.manager')->recordAgreement($user, $terms);
Notifications
config/packages/cp_terms.yaml:
cp_terms:
notifications:
enabled: true
template: 'emails/terms_updated.html.twig'
Service Container Binding
Bind the manager to Laravel’s container in config/app.php:
'bindings' => [
// ...
CoopersPeele\TermsBundle\Manager\TermsManagerInterface::class => CoopersPeele\TermsBundle\Manager\TermsManager::class,
],
Twig Integration
Extend Laravel’s Twig environment in app/Providers/AppServiceProvider.php:
public function boot()
{
$this->app['twig']->addExtension(new \CoopersPeele\TermsBundle\Twig\TermsExtension());
}
Route Prefixes
Override admin routes in routes/web.php:
use CoopersPeele\TermsBundle\Resources\routing\admin_routes;
admin_routes()->prefix('admin')->name('admin.');
Event Listeners Listen for term updates in Laravel’s event system:
// app/Listeners/TermUpdatedListener.php
public function handle(TermsUpdatedEvent $event)
{
// Custom logic (e.g., log updates, trigger analytics)
}
Migration Conflicts
terms table, drop it before running migrations or manually merge schemas.Resources/migrations/ for the expected structure.Twig Extension Not Loading
AppServiceProvider).php artisan view:clear
User Agreement Logic
UserAgreement entity.auth:api, ensure the User model matches the bundle’s expected structure (e.g., id, email).Version Diff Rendering
diffWithPrevious() method returns raw HTML. Sanitize output if rendering in untrusted contexts:
{{ tos('latest').diffWithPrevious()|striptags }}
Notification Delays
terms.updated event:
event(new TermsUpdatedEvent($terms));
Enable Debug Mode
Set debug: true in config/packages/cp_terms.yaml to log version changes and agreement records.
Check Database Records Verify entries in:
terms (terms content and versions)user_agreements (user acceptance records)Override Templates
Customize Twig templates by copying from Resources/views/ to templates/cp_terms/.
Disable Behaviors Temporarily
Set cp_terms.behaviors.signup: false to bypass signup enforcement during development.
Custom Validation
Extend the Terms entity to add validation rules:
use CoopersPeele\TermsBundle\Entity\Terms as BaseTerms;
class CustomTerms extends BaseTerms
{
public function validate(\Symfony\Component\Validator\Constraint $constraint)
{
// Add custom rules
}
}
Additional Fields
Add columns to the terms table via a custom migration and update the Terms entity:
/**
* @ORM\Column(type="boolean")
*/
private $isActive;
Custom Notifications Override the notification service:
cp_terms:
notifications:
service: 'app.custom_notification_service'
API Integration Create a custom controller to expose terms via API:
use CoopersPeele\TermsBundle\Manager\TermsManagerInterface;
class TermsApiController extends Controller
{
public function __construct(private TermsManagerInterface $manager) {}
public function getLatestTerms()
{
return response()->json($this->manager->getLatestTerms());
}
}
How can I help you explore Laravel packages today?