You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Sonata Admin Bundle自定义模板报错求助:ServiceNotFoundException问题

Fixing ServiceNotFoundException for Custom Sonata Admin Template

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 setTemplate call takes two arguments: the template type (list in this case) and the path to your Twig file (relative to your project's templates/ 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 SonataAdmin but the folder is named sonataadmin, Symfony won't find it.
  • Ensure there are no typos in the folder or file name (e.g., parametre vs paramètres, globaux vs globaux).

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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.19 07:53:00