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

Symfony集成Pentatrion ViteBundle时renderTags()返回值类型错误

问题:Pentatrion ViteBundle集成Symfony时出现返回值类型错误

在Symfony项目中使用Pentatrion ViteBundle集成Vite,已按官方迁移文档操作,但仍触发TypeError:提示EntrypointRenderer::renderTags()必须返回Pentatrion\ViteBundle\Service\mixed实例,实际返回字符串。相关信息如下:

错误日志

[2024-06-20T22:39:48.201801+02:00] php.CRITICAL: Uncaught Error: Return value of Pentatrion\ViteBundle\Service\EntrypointRenderer::renderTags() must be an instance of Pentatrion\ViteBundle\Service\mixed, string returned {"exception":"[object] (TypeError(code: 0): Return value of Pentatrion\\\\ViteBundle\\\\Service\\\\EntrypointRenderer::renderTags() must be an instance of Pentatrion\\\\ViteBundle\\\\Service\\\\mixed, string returned at /.../myapp/vendor/pentatrion/vite-bundle/src/Service/EntrypointRenderer.php:289)"} []
[2024-06-20T22:39:48.303733+02:00] request.CRITICAL: Uncaught PHP Exception TypeError: "Return value of Pentatrion\ViteBundle\Service\EntrypointRenderer::renderTags() must be an instance of Pentatrion\ViteBundle\Service\mixed, string returned" at /.../myapp/vendor/pentatrion/vite-bundle/src/Service/EntrypointRenderer.php line 289 {"exception":"[object] (TypeError(code: 0): Return value of Pentatrion\\\\ViteBundle\\\\Service\\\\EntrypointRenderer::renderTags() must be an instance of Pentatrion\\\\ViteBundle\\\\Service\\\\mixed, string returned at /.../myapp/vendor/pentatrion/vite-bundle/src/Service/EntrypointRenderer.php:289)"} []

EntrypointRenderer.php相关函数

/**
 * @return string|array
 */
public function renderTags(array $tags, bool $isBuild, bool $toString): mixed
{
    if (null !== $this->eventDispatcher) {
        foreach ($tags as $tag) {
            $this->eventDispatcher->dispatch(new RenderAssetTagEvent($isBuild, $tag));
        }
    }

    if ('link-tag' !== $this->preload) {
        $tags = array_filter($tags, function (Tag $tag) {
            return !$tag->isModulePreload();
        });
    }

    return $toString
    ? implode('', array_map(function ($tagEvent) {
        return TagRenderer::generateTag($tagEvent);
    }, $tags))
    : $tags; // line 289
}

Bundle版本信息

"pentatrion/vite-bundle": "^6.3",
{"name": "pentatrion/vite-bundle","version": "v6.3.4",...}

Twig模板代码

<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8">
        <title>{% block title %}Welcome!{% endblock %}</title>
        <link rel="icon" href="data:image/svg+xml,&lt;svg xmlns=%22http://www.w3.org/2000/svg%22 viewBox=%220 0 128 128%22&gt;&lt;text y=%221.2em%22 font-size=%2296%22&gt;⚫️&lt;/text&gt;&lt;/svg&gt;">
        {% block stylesheets %}
            {{ vite_entry_link_tags('app') }}
        {% endblock %}
        ...
    </head>
    <body>
        {% block body %}{% endblock %}
        {% block javascripts %}
            {{ vite_entry_script_tags('app') }}
        {% endblock %}
    </body>
</html>
解决方案

这个错误由PHP版本不兼容导致:mixed类型声明是PHP 8.0及以上的特性,若项目运行在PHP 7.x环境中,就会触发该类型错误。

处理方法二选一:

  • 升级PHP版本到8.0或更高版本,满足Bundle的类型声明要求
  • 降级Pentatrion ViteBundle到兼容PHP 7.x的版本(如v5.x系列):
    1. 修改composer.json中的版本约束为"pentatrion/vite-bundle": "^5.0"
    2. 执行命令composer update pentatrion/vite-bundle --with-all-dependencies完成降级

另外,当前Twig模板中的vite_entry_link_tags和vite_entry_script_tags调用写法正确,无需调整。

内容的提问来源于stack exchange,提问作者nyaina

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.22 03:53:14