Symfony Turbo 下载 html 文件而不是渲染它

问题描述 投票:0回答:1

我正在使用 Symfony 7 和 Turbo 制作一个网络应用程序。

我的所有控制器都可以与 Turbo 配合使用,遵循简单的步骤

    #[Route('/{platform}', name: 'app_platform_pick')]
    public function platformPick(Request $request, $platform){
        $request->setRequestFormat(TurboBundle::STREAM_FORMAT);
            return $this->render('platform/'.$platform.'.html.twig');
    }

但是对于我的流程的最后一条路线,它会在我的导航器上下载应该在我的页面上更新的 Turbo 模板文件。 这是我下载的 Turbo 模板,而不是在我的网页上更新:

<turbo-stream action="update" target="finalContent">
    <template>
        <p style="color:white">Playlist created!</p>
    </template>
</turbo-stream>

这是我的控制器,让我下载文件:

        #[Route('/authorize/spotify', name: 'app_auth_spotify')]
        public function authorizeSpotify(Request $request)
        {
            if ($request->query->get('code')) {
                $accountToken = $this->spotifyService->getUser($request->query->get('code'));
                $playlistId = $request->cookies->get('playlistId');
                $playlistPlatform = $request->cookies->get('playlistPlatform');
                try {
                    switch ($playlistPlatform) {
                        case 'spotify':
                           $this->spotifyService->createPlaylist($accountToken, $playlistId);
                            break;
                        case 'deezer':
                            break;
                        case 'applemusic':
                            break;
                    }
                } catch (\Exception $e) {
                    dd($e);
                }
            }
                $request->setRequestFormat(TurboBundle::STREAM_FORMAT);
                return $this->render('main/success.html.twig');
        }

我尝试创建其他路由,但问题是相同的,所以我重定向到“/”路由并且它起作用了,我猜问题来自请求对象,因为“/”路由是唯一没有请求参数如下:

    #[Route('/', name: 'app_main')]
    public function index(): Response
    {
        return $this->render('main/index.html.twig');
    }

你们有人遇到过这种情况吗? 谢谢

symfony turbo
1个回答
0
投票

问题可能与您的路线中使用的请求格式有关。您是否尝试过修改

app_auth_spotify
路由中定义请求格式的方式以确保 Turbo Stream 正确处理?

你可以试试这个:

#[Route('/authorize/spotify', name: 'app_auth_spotify')]
public function authorizeSpotify(Request $request)
{
    if ($request->query->get('code')) {
        $accountToken = $this->spotifyService->getUser($request->query->get('code'));
        $playlistId = $request->cookies->get('playlistId');
        $playlistPlatform = $request->cookies->get('playlistPlatform');
        try {
            switch ($playlistPlatform) {
                case 'spotify':
                    $this->spotifyService->createPlaylist($accountToken, $playlistId);
                    break;
                case 'deezer':
                    break;
                case 'applemusic':
                    break;
            }
        } catch (\Exception $e) {
            dd($e);
        }
    }
    $response = new Response();
    $response->headers->set('Content-Type', 'text/html');
    $response->setContent('<turbo-stream action="update" target="finalContent"><template><p style="color:white">Playlist created!</p></template></turbo-stream>');
    return $response;
}

在本例中,我将手动创建一个

Response
,其中包含您要发送以更新页面的 Turbo Stream 模板中的内容

© www.soinside.com 2019 - 2024. All rights reserved.