2014-12-01 07:43
浏览 31


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');
        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(){
    if($data instanceof \Zend\Http\PhpEnvironment\Response) return $data;
    # do rest of the staff of read action
public function deleteAction(){
    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';
    #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';
    }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 */ }
        ->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条回答 默认 最新

相关推荐 更多相似问题