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

Symfony使用services.yml引入服务时遭遇语法解析错误求助

Fixing Symfony services.yml Parse Error: Unexpected 'security' (T_STRING)

Hey there! Let's break down this frustrating parse error you're hitting in your Symfony services.yml file. That Parse Error: syntax error, unexpected 'security' (T_STRING), expecting ']' message is a clear sign there's a tiny YAML syntax glitch somewhere—usually tied to array nesting, quotes, or indentation.

Here are the most common culprits and how to fix them:

Common Causes & Fixes

1. Unclosed Array Brackets

If you're using inline array syntax (with []) for service arguments, forgetting to close the bracket will trigger this error immediately. For example:

# ❌ Wrong - missing closing ]
services:
    app.my_service:
        class: App\Service\MyService
        arguments: ['@some_service', '@security.token_storage
# ✅ Correct - bracket properly closed
services:
    app.my_service:
        class: App\Service\MyService
        arguments: ['@some_service', '@security.token_storage']

2. Missing Quotes Around Service IDs

YAML is picky about special characters like @ or :. If you reference a service like @security.token_storage without quotes in an inline array, the parser might misinterpret it as a new key instead of a value. Always wrap service IDs in single quotes to avoid this:

# ❌ Risky - no quotes around service ID
arguments: [@some_service, @security.token_storage]
# ✅ Safe - quoted service IDs
arguments: ['@some_service', '@security.token_storage']

3. Indentation Mistakes

YAML relies entirely on consistent indentation (use spaces, not tabs!) to define hierarchy. If a line referencing security is indented incorrectly, the parser will think it's part of an array instead of a new configuration key. For example:

# ❌ Wrong - inconsistent indentation in arguments
services:
    app.my_service:
        class: App\Service\MyService
        arguments:
            - '@some_service'
          - '@security.token_storage'
# ✅ Correct - uniform indentation across all list items
services:
    app.my_service:
        class: App\Service\MyService
        arguments:
            - '@some_service'
            - '@security.token_storage'

4. Mixed Array Syntax

Avoid mixing inline ([]) and block-style arrays in the same configuration—this can confuse the parser and lead to unexpected errors:

# ❌ Confusing mixed syntax (missing closing ])
arguments: [
    '@some_service',
    '@security.token_storage'

Quick Debugging Hack

Save yourself time with Symfony's built-in YAML linter. Run this command in your project root:

bin/console lint:yaml config/services.yml

It will pinpoint the exact line number where the syntax error occurs—way faster than manually scanning the entire file!

Most of the time, this error is just a tiny oversight: a missing bracket, misplaced quote, or wonky indentation. The linter will get you straight to the problem, so you can get back to building your service.

内容的提问来源于stack exchange,提问作者Roddy P. Carbonell

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.19 08:03:08