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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥15 stable diffusion
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿