Installation:
composer require dcylabs/symfony-twig-helper dev-master
Add the bundle to config/bundles.php (Laravel 5.5+) or AppKernel.php (older Symfony/Laravel):
Dcylabs\TwigBundle\DcylabsTwigBundle::class,
First Use Case:
Use the checkRoles Twig tag in a template to verify if the current user has access to a route/path.
Example:
{% checkRoles '/admin/dashboard' %}
<div class="access-granted">
Welcome, Admin! {{ check_url }}
</div>
{% else %}
<div class="access-denied">
You don't have permission to view this page.
</div>
{% endcheckRoles %}
Where to Look First:
checkRoles for role-based path validation.Role-Based UI Conditionals: Dynamically show/hide UI elements based on path access:
{% checkRoles '/admin/users', '/admin/settings' %}
{{ renderAdminPanel() }}
{% endcheckRoles %}
Multi-Path Validation: Validate access to multiple paths at once:
{% checkRoles '/user/profile', '/user/settings' %}
<div class="user-actions">
{{ check_urls | join(', ') }} accessible!
</div>
{% endcheckRoles %}
Integration with Laravel’s Auth:
Since Laravel uses Symfony’s security components, leverage the bundle with Laravel’s Auth::user() and gates/policies:
{% if app.user and app.user.can('access', '/admin') %}
{% checkRoles '/admin' %}
{{ renderAdminContent() }}
{% endcheckRoles %}
{% endif %}
Custom Logic Extension: Extend the bundle’s behavior by overriding its Twig environment or security logic (see Gotchas for details).
Symfony Security Integration:
Ensure your Laravel app’s security layer (e.g., auth.php gates) aligns with the bundle’s path-based checks.
Example:
// app/Providers/AuthServiceProvider.php
Gate::define('access-path', function ($user, $path) {
return $user->canAccessPath($path); // Custom logic
});
Twig Global Variables:
Expose check_url/check_urls as globals in your Twig environment for reusable logic:
// config/view.php
'globals' => [
'check_url' => null,
'check_urls' => [],
],
Testing:
Mock the checkRoles tag in PHPUnit:
$twig = new \Twig\Environment($loader);
$twig->addExtension(new \Dcylabs\TwigBundle\Twig\Extension\CheckRolesExtension());
$this->assertTemplateString('{% checkRoles "/test" %}OK{% endcheckRoles %}', 'OK');
Laravel-Symfony Mismatch:
Route::get() paths, not URIs).Security component is available (Laravel 5.5+ includes it via symfony/security-bundle).symfony/security-core if missing:
composer require symfony/security-core
Path Matching Strictness:
The bundle may use exact path matching. For Laravel’s route model binding or dynamic segments (e.g., /user/{id}), pre-process paths:
{% checkRoles app.path('/admin') %} {# Use Laravel's path() helper #}
No Laravel-Specific Features:
can() method or policies.// app/Extensions/CustomCheckRolesExtension.php
class CustomCheckRolesExtension extends \Twig\Extension\AbstractExtension
{
public function getFunctions()
{
return [
new \Twig\TwigFunction('laravelCheckRoles', [$this, 'checkLaravelRoles']),
];
}
public function checkLaravelRoles($paths)
{
// Implement Laravel-specific logic
}
}
Dev Dependency Limitation:
The package is installed as dev-master. Ensure your composer.json locks the version to avoid runtime issues:
"require": {
"dcylabs/symfony-twig-helper": "dev-master as 1.0.0"
}
Check Security Context: Dump the current user’s roles/permissions in Twig:
{{ dump(app.user) if app.user is defined }}
Override Twig Extension:
Debug the checkRoles logic by extending the bundle’s Twig extension:
// app/Providers/AppServiceProvider.php
public function boot()
{
$twig = $this->app['view']->getEngine();
$twig->addExtension(new class extends \Dcylabs\TwigBundle\Twig\Extension\CheckRolesExtension {
public function checkRoles($paths)
{
// Add debug logs
\Log::debug('Checking paths:', $paths);
return parent::checkRoles($paths);
}
});
}
Route Paths: Verify paths match Laravel’s route generation:
// In a test or Tinker
route('admin.dashboard'); // Outputs: "/admin/dashboard"
Custom Role Provider: Override the security context provider:
// config/services.php
'dcylabs.twig.helper.security.context' => function () {
return new \YourApp\CustomSecurityContext();
};
Add New Twig Tags: Extend the bundle’s Twig extension to add custom tags:
// app/Extensions/MyTwigExtension.php
class MyTwigExtension extends \Twig\Extension\AbstractExtension
{
public function getTokenParsers()
{
return [new \YourApp\Twig\TokenParser\CustomCheckParser()];
}
}
Path Normalization: Pre-process paths in a middleware to ensure consistency:
// app/Http/Middleware/NormalizePaths.php
public function handle($request, Closure $next)
{
$request->merge([
'normalized_path' => Str::of($request->path())->lower()->trim('/'),
]);
return $next($request);
}
How can I help you explore Laravel packages today?