doulei6330
2014-12-01 07:43
浏览 31
已采纳

如何在zf2中恢复重定向命令

I'm looking proper way to revert any given redirect command in ZF2 controller

For example in one of my function i use - $this->redirect()->toRoute('my/desire/path/to/redirect'); But after getting result I want to cancel that command and show something else to my user.

Here is my code to illustrate more precisely -

considering following method inside controller that will retrieve data for one id

public function getMyData(){
    $idX = $this->params()->fromRoute('id');
    $rtrn=$this->myTableGateway->getData($idX);
    if($rtrn!==false){
        $mnfctr['myData']=$rtrn;
        $mnfctr['readMode']='yes';
        return $mnfctr;
    }else return $this->redirect()->toRoute('product-loader/sub-categories', array('cid'=>$this->mcep->id));
}

now in a real action inside controller

public function readAction(){
    $data=$this->getMyData();
    if($data instanceof \Zend\Http\PhpEnvironment\Response) return $data;
    # do rest of the staff of read action
}
public function deleteAction(){
    $data=$this->getMyData();
    if($data instanceof \Zend\Http\PhpEnvironment\Response) return $data;
    # do rest of the staff of delete action
}

now i want to show a logo of my company through the same controller problem is if i simply redirect the logo should not draw properly so i design my image action method as follows

public function logoAction(){
    $logoFlName='Unknown Company';
    $data=$this->getMyData();
    #echo '$mnfctr: '.get_class($mnfctr); exit();
    $response = $this->getResponse();
    if($mnfctr instanceof \Zend\Http\PhpEnvironment\Response){
        # in this situation i need to cancel my previous response command
        $logoFlName='Invalid Company';
        if($response->isRedirect()){
            $this->flashMessenger()->clearCurrentMessages();
            $response->getHeaders()->clearHeaders();
            $response->setStatusCode(Response::STATUS_CODE_200);
        }
    }elseif(is_array($data) && array_key_exists('myData', $data) && $data['myData'] instanceof MyData){
        # code to draw the real logo
        $logoFlName='Logo of XYZ Company';
    }else{ /* something wrong and can't be happen. but we really can't find logo */ }
    $newImage=PLImageHelper::getLogoFromString($string2draw__not_found_image__or__logo);
    $response->setContent($newImage->get('png'));
    $response->getHeaders()
        ->addHeaderLine('Content-Transfer-Encoding', 'binary')
        ->addHeaderLine('Content-Type', 'image/png')
        ->addHeaderLine('Content-Disposition', 'filename="'.$logoFlName.'.png"')
        #->addHeaderLine('Content-Length', mb_strlen($imageContent))
        ;
    return $response;
}

I know there has a lot of way, suppose its possible to redirect to a real no-image-found.png file to show. but i want it programmatically

  • 写回答
  • 好问题 提建议
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • douhan8892 2014-12-01 07:43
    已采纳

    Best possible way I get -

    $response = $this->getResponse();
    if($response->isRedirect()){
        $response->getHeaders()->clearHeaders();
        $response->setStatusCode(Response::STATUS_CODE_200);
    }
    

    Is there any other way to do it. I mean more efficient way that I can use.

    已采纳该答案
    评论
    解决 无用
    打赏 举报
  • dongyou6768 2014-12-05 14:26

    If you want to redirect the user to where the request is coming from you can use the following:

    /** @var \Zend\Http\Request $request */
    $request = $this->getRequest()
    return $this->redirect()->toUrl($request->getHeaders()->get('referer')->getUri());
    

    getUri() will return an url, for example: http://http://stackoverflow.com/posts/27318052/edit. Hope this helps.

    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题