Javascript重定向:symfony路由中的脚本不工作

Javascript redirect: script in symfony route not working

本文关键字:脚本 工作 路由 重定向 symfony Javascript      更新时间:2023-09-26

我重定向的页面I:

<a class="uk-overlay" href="{{ path("shot.view",{"id" : id})  }}">
    <img class="uk-border-rounded" src="{{ largeSquare(shot) }}">
    <div class="uk-overlay-area uk-border-rounded">
        <div class="uk-align-right uk-margin-small-top">
            {% if is_granted('ROLE_USER') %}
                {% if app.user.isOwnStar( id ) %}
                    <button class="uk-icon-button uk-icon-star uk-margin-small-right" style="background: #00a8e6;"></button>
                {% else %}
                    <button class="uk-icon-button uk-icon-star uk-margin-small-right" onclick="window.location='{{ path("shot.giveStar",{"id" : id})  }}'"></button>
                {% endif %}
                {% if app.user.isOwnFavorite( id ) %}
                    <button class="uk-icon-button uk-icon-heart uk-margin-small-right" style="background: #d32c46;"></button>
                {% else %}
                    <button class="uk-icon-button uk-icon-heart uk-margin-small-right" onclick="window.location='{{ path("favorite.add",{"id" : id}) }}'"></button>
                {% endif %}
            {% endif %}
        </div>
    </div>
</a>  

这是一个带有锚点和覆盖的图像,其中包含一些链接。因为不可能做嵌套锚,所以我设置了一个带有javascript重定向的链接按钮。

主要问题是,被调用的symfony路由(来自按钮)脚本没有执行:

public function giveStarAction(Application $app, Request $request, $id)
{
    $msg = $this->shotManager->giveStar($app['user']->getId(),$id);
    $app['session']->getFlashBag()->add('info', $msg );
    exit;
    return $app->redirect( $app->path('shot.view', array('id' => $id) ) );
}

例如path("shot.giveStar",{"id" : id}) 的路线

前三行被忽略,只执行重定向命令。。。

我需要改变的是,代码在重定向之前执行?

window.location.href = '' 
// or
window.location.replace("")