使用Ajax请求登录后的ZfcUser重定向

ZF2: ZfcUser redirect after login with Ajax request

本文关键字:ZfcUser 重定向 登录 Ajax 请求 使用      更新时间:2023-09-26

我在我的项目中使用ZfcUser。我现在有一个链接,能够书签广告。我使用jQuery Ajax来跳转到控制器的Action中。为了能够设置书签,我必须登录。如果用户没有登录,则会重定向到登录页面。

这一切都工作得很好,现在,但我想知道是否有可能重定向到我的广告页,用户登录后,最好的甚至是书签得到自动设置,因为这是最后的用户试图实现。现在我被重定向到定义在zfcuser.global.php("login_redirect_route")。是的,我设置了

'use_redirect_parameter_if_present' => true, 
在zfcuser.global.php文件中将

设置为true。但我现在该怎么用呢?我想这可能是个问题,因为它通过Ajax请求,然后我通过

实现重定向
window.location.replace(response.redirect);

有什么方法可以达到这个目标,还是我期望太高了?

 $(document).on("click", "#add-bookmark-link",function () {
            $.ajax({    
            dataType: 'json',
            data: { id: <?php echo $advertId;?>},
        url: '/add-bookmark',
        success: function(response) {
                    if (response.status == false)
                    {
                        window.location.replace(response.redirect);
                    }
                    else{
                        $('.add-bookmark').replaceWith('<p class="text-right remove-bookmark"><span class="danger glyphicon glyphicon-minus-sign text-danger"></span> <a href="#" id="remove-bookmark-link">Remove Bookmark</a>');
                    }
            }
        });
    });

});

AdvertController.php

 public function addBookmarkAction() {    
    $advertId =  $this->params()->fromQuery('id',null);
    if ($this->zfcUserAuthentication()->hasIdentity()) {
        $this->getServiceLocator()->get('BookmarkAdvertService')->saveAdvertBookmark($advertId);
        $result = new JsonModel(array(  
                'status' =>true,
        ));
    }
    else{
        $result = new JsonModel(array(
                'status'   => false,
                'redirect' => $this->url()->fromRoute('zfcuser/login')
               ));
    }
    return $result;
} 

收到以下回复后,我已经更新了我的源代码。我不确定我做得对不对。它现在直接转到下面的URL,而不显示登录。URL也是错误的,因为它显示我[对象对象]。为什么现在要进入登录?

http://mywebsite.com/ad/thenameoftheadvert/[object Object]

一般来说,我的URL应该是这样的

http://mywebsite.com/ad/thenameoftheadvert/articleId.htm

我做错了什么?我想我实施的解决方案是错误的…

public function addBookmarkAction() {    
    $advertId =  $this->params()->fromQuery('id',null);
    if ($this->zfcUserAuthentication()->hasIdentity()) {
            $this->getServiceLocator()->get('BookmarkAdvertService')->saveAdvertBookmark($advertId);
        $result = new JsonModel(array(  
                'status' =>true,
        ));
    }
    else{
        $name    = '/add-bookmark';
        $params  = array('advertId' => $advertId);
        $options = array();
        $bookmarkRedirect = $this->url($name, $params, $options);
        $name    = 'zfcuser/login';
        $params  = array(); 
        $options = array(
            'query' => array(
                'redirect' => $bookmarkRedirect
            ), 
        );   
        $loginRedirect = $this->url($name, $params, $options);
        $result = new JsonModel(array(
                'status'   => false,
                'redirect' => $loginRedirect
               ));
    }
    return $result;
}

通常情况下,您必须将重定向参数作为查询参数传递,然后在url中呈现如下:

http://www.example.com/login?redirect=http://www.example.com/object/1/add/bookmark

你可以这样做:

首先创建一个用于添加书签的重定向:

$name    = 'add/bookmark';
$params  = array('object_id' => $object_id);
$options = array();
$bookmarkRedirect = $this->url($name, $params, $options);

现在为登录创建一个重定向,并将重定向添加到书签中作为查询参数:

$name    = 'zfcuser/login';
$params  = array(); 
$options = array(
    'query' => array(
        'redirect' => $addBookmark
    ), 
);   
$loginRedirect = $this->url($name, $params, $options);

在JsonModel中返回:

$result = new JsonModel(array(
    'status'   => false,
    'redirect' => $loginRedirect
));