Happy to help.

The reason I included the plug-in was so your bootstrap could be a bit cleaner and easier to read, always a good thing(tm). Also it centralises setting up the encoding for different objects. So for example if you wanted to set the encoding for the database as well. Another example might be that you want to examine the response object and add the encoding for certain content types before the response is sent (like text/*). Additionally you could detect the Accept-Encoding header provided by the client in the request object. All could be done from the one plug-in.

Luke

Viper X wrote:
Thanks Luke, Matthew. Of course you are right. So when we have in the layout
template
<meta http-equiv="content-type" content="text/html;charset=utf-8" />

the
$response->setHeader('Content-Type', 'text/html; charset=UTF-8', true);

adds nothing more, but prevents us from sending other content if we need
this later. So I'll drop it. It was a bit of reinsurance from my side, not
well thought out.

But for setting the encoding to the View object - I think there is no need
of special plugin for this, when we can just make it with 3 lines in the
bootstrap:

$view = new Zend_View(array('encoding'=>'UTF-8'));
$viewRendered = new Zend_Controller_Action_Helper_ViewRenderer($view);
Zend_Controller_Action_HelperBroker::addHelper($viewRendered);


Regards,
Andrei



Luke Richards wrote:
Hi Viper,

I looked through 'Part 2' quickly. Something that did catch my eye was the part where you return the response from the front controller in your bootstrap and set the content type header to be text/html. I assume you've done this to set the UTF-8 encoding? What happens if you want to return JSON, XML or file downloads from part of your application as a response? The response object force ably overwrites any headers set to text/html.

Based on the following thread, it seems setting the encoding in the header isn't necessary as long as it's in the HTML.
http://www.nabble.com/Best-place-to-specify-encoding-tf4621717s16154.html#a13198947

As for the view perhaps you could use a plug-in like so:
class My_Plugin_SetEncoding extends Zend_Controller_Plugin_Abstract {
protected $_encoding; public function __construct($encoding = 'utf-8')
    {
        $this->_encoding = $encoding;
    }
public function preDispatch($request)
    {
Zend_Controller_Action_HelperBroker::getHelper('ViewRenderer')->view->encoding = $this->_encoding;
    }
}

and then in your bootstrap

Zend_Layout::startMvc(
    array <http://www.php.net/array>(
        'layoutPath' => $siteRootDir . '/application/layouts',
        'layout' => 'main'
    )
);

Zend_Controller_Front::getInstance()
  ->throwExceptions(true)
  ->registerPlugin(new My_Plugin_SetEncoding('utf-8'))
  ->addModuleDirectory($siteRootDir . '/application/modules')
  ->dispatch();

Just a suggestion (I've not actually tried the code so I'm not sure if it works, specifically ..._HelperBroker::getHelper part as the method isn't defined as static?? Although the method does seem to be able to operate as a static?? Can anyone explain?).

Luke

Viper X wrote:
Hi all there.

I'm writing simple web application to be used from me as "kick-start"
when
starting new application, which needs user registration and
authentication.
I think it can be interesting for someone to see my approach to the
problem
and eventually to use some of my code for his own purpose :) So i decided
to
put the code along with some descriptions to a blog series here:

http://zfsite.andreinikolov.com http://zfsite.andreinikolov.com
Now there is only the first part - setting up the MVC, later today I hope
I
will manage to write the next part - putting the session management in
the
database. I'll appriciate any comments on it.

Reply via email to