Sonata Admin Bundle自定义模板报错求助:ServiceNotFoundException问题
Hey there! Let's break down why you're hitting this ServiceNotFoundException when trying to customize your Sonata Admin Bundle template. The error says your admin.globaux service depends on a non-existent service called SonataAdmin/CRUD/Special/list_parametre_globaux.html.twig—which makes sense, because Twig templates aren't Symfony services, so you can't reference them as if they are. Here's how to fix this:
1. Correct Your service.yml Template Configuration
First, double-check how you're setting the custom template in your service definition. You should use the setTemplate method call to tell Sonata Admin to use your custom list template, not inject it as a service dependency.
Your service.yml should look something like this:
services: admin.globaux: class: App\Admin\ParametreGlobauxAdmin arguments: [~, App\Entity\ParametreGlobaux, ~] # Third argument is for controller service (use ~ for default) tags: - { name: sonata.admin, manager_type: orm, label: "Paramètres Globaux" } calls: - [setTemplate, ['list', 'SonataAdmin/CRUD/Special/list_parametre_globaux.html.twig']]
- The
setTemplatecall takes two arguments: the template type (listin this case) and the path to your Twig file (relative to your project'stemplates/directory). - Avoid adding
@before the template path—this tells Symfony to look for a service, which isn't what you want here.
2. Verify Your Template File Location
Make sure your list_parametre_globaux.html.twig file exists exactly at this path in your project:
templates/SonataAdmin/CRUD/Special/list_parametre_globaux.html.twig
- Case sensitivity matters (especially on Linux/macOS): If your config uses
SonataAdminbut the folder is namedsonataadmin, Symfony won't find it. - Ensure there are no typos in the folder or file name (e.g.,
parametrevsparamètres,globauxvsglobaux).
3. Try Defining Templates Directly in Your Admin Class
An alternative (often more straightforward) approach is to set the custom template directly in your Admin class instead of service.yml. Here's how:
// src/Admin/ParametreGlobauxAdmin.php namespace App\Admin; use Sonata\AdminBundle\Admin\AbstractAdmin; class ParametreGlobauxAdmin extends AbstractAdmin { // Define custom templates here protected $templates = [ 'list' => 'SonataAdmin/CRUD/Special/list_parametre_globaux.html.twig' ]; // ... rest of your Admin class code }
This bypasses any potential misconfiguration in service.yml and keeps template settings close to your admin logic.
4. Clear Your Symfony Cache
Sometimes old cache can cause Symfony to look for outdated paths. Run this command to clear cache:
php bin/console cache:clear # For production environment: php bin/console cache:clear --env=prod
5. Check for Accidental Service Injection
If you still get the error, make sure you didn't accidentally add the template path as a service argument in service.yml. For example, this is wrong:
# ❌ Wrong! Don't do this arguments: [~, App\Entity\ParametreGlobaux, '@SonataAdmin/CRUD/Special/list_parametre_globaux.html.twig']
The third argument in the admin service should be either ~ (to use the default controller) or a valid controller service ID—not a template path.
内容的提问来源于stack exchange,提问作者zbeb

