How to set charset to UTF-8 in a Zend application?

前端 未结 9 1492
失恋的感觉
失恋的感觉 2021-01-05 14:29

I am developping a Zend application. The data in my database is encoded in \"utf8_unicode_ci\". I declared in my application.ini :

resources.view.encoding =          


        
9条回答
  •  傲寒
    傲寒 (楼主)
    2021-01-05 15:09

    Have you tried also setting the headers to utf8? Usually in php i do it this way

     header ('Content-type: text/html; charset=utf-8');
    

    in your case i think you must use something different. i've taken this example from Zend Framework documentation maybe you should use something different, i'm no expert of Zend_Framework

    // Within an action controller action:
    // Set a header
    $this->getResponse()
        ->setHeader('Content-Type', 'text/html')
        ->appendBody($content);
    

    If you set headers, meta and encoding it should work (from your code it seems to me you are only setting meta and encoding)

    (look at this question to understand what i mean, the answer from Berry Langerak: PHP Display Special Characters)

    EDIT - i also found another example in this article where it sets the header for a controller, take a look at it,maybe this is what you are looking for : http://www.chris.lu/en/news/show/4d56d0ecb058c/

    This part might be what you are looking for:

    protected function _initFrontControllerOutput() {
    
        $this->bootstrap('FrontController');
        $frontController = $this->getResource('FrontController');
    
        $response = new Zend_Controller_Response_Http;
        $response->setHeader('Content-Type', 'text/html; charset=UTF-8', true);
        $frontController->setResponse($response);
    
        $frontController->setParam('useDefaultControllerAlways', false);
    
        return $frontController;
    
    }
    

提交回复
热议问题