cortezvini97/symfony-hybrid-views-bundle
Installation
Add the package via Composer in your Laravel project (Symfony-based, so use a bridge like fruitcake/laravel-symfony if needed):
composer require cortezvini97/symfony-hybrid-views-bundle
Register the bundle in config/app.php (Symfony) or bridge it via Laravel’s service provider.
First Use Case Render a hybrid view (e.g., mix PHP + Twig) in a Symfony controller:
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Response;
class HomeController extends AbstractController
{
public function index(): Response
{
return $this->render('home.html.twig', [
'title' => 'Hybrid View Demo',
'data' => ['key' => 'value']
]);
}
}
templates/home.html.twig (or .php for PHP views).php bin/console server:run (Symfony) or Laravel’s routing.Hybrid Templating
home.html.twig with embedded PHP:
<h1>{{ title }}</h1>
{% for item in data %}
{{ include('partials/_item.php', {'item': item}) }}
{% endfor %}
Laravel Integration
HybridViewsBundle via Laravel’s AppServiceProvider:
public function register()
{
$this->app->singleton('twig', function ($app) {
return HybridViewsBundle::getTwigEnvironment();
});
}
AbstractController in Laravel routes:
Route::get('/', [HomeController::class, 'index']);
Partial Reuse
templates/partials/_header.twig (Twig)resources/views/partials/header.php (PHP){% include %} or {{ include }} in templates.{{ app.environment }} in Twig to conditionally load assets.twig/extra-bundle for Blade-like syntax in Twig:
{{ include('layouts.app', {'content': include('home.twig')}) }}
Namespace Collisions
twig_ or php_:
{% include('twig_header.twig') %}
Caching Quirks
var/cache/dev) may not auto-update in dev. Clear it after template changes:
php bin/console cache:clear
php artisan cache:clear if needed.Dependency Conflicts
symfony/twig-bundle and twig/twig versions align with Laravel’s Symfony bridge.Template Errors: Twig errors show line numbers in .twig files; PHP errors may be ambiguous. Use:
$this->get('twig')->addRuntimeLoader(new \Twig\RuntimeLoader\RuntimeLoader());
For better error context.
Variable Dumping: Use Twig’s dump() or Laravel’s dd() in PHP templates:
{{ dump(data) }}
Custom Filters
Add Twig extensions in Symfony’s config/packages/twig.yaml:
twig:
extensions:
- App\Twig\AppExtension
Or in Laravel’s AppServiceProvider:
$twig->addExtension(new \App\Twig\AppExtension());
PHP Template Overrides Override Twig’s loader to prioritize PHP templates:
$loader = new \Twig\Loader\FilesystemLoader([__DIR__.'/templates']);
$twig->setLoader($loader);
Hybrid Forms
Use Symfony’s FormComponent in Twig:
{{ form_start(form) }}
{{ form_widget(form.name) }}
{{ form_end(form) }}
Render PHP forms via {{ include('form.php', {'form': form}) }}.
How can I help you explore Laravel packages today?