Disabling The Zend Framework ViewRenderer

There are times when using the Zend Framework that you don’t want a view to render.

For example, you may be generating a feed programaticaly, or you may be using a templating engine such as Twig for some or all paths.

By default, the front controller enables the ViewRenderer action helper. It is the job of the ViewRenderer to inject the view object into the controller and to automatically render the view. The idea is that you don’t need to worry about manually instantiating view objects in your controller, the helper does it for you.

It’s actually very simple to disable this behaviour, and there are several ways to do it.

Firstly, if you want to disable this functionality across your application, you could use the following in the application.ini file…

resources.frontController.noViewRenderer = true

This can also be set programatically…

Zend_Controller_Front::getInstance()->setParam('noViewRenderer', true);

A better approach would be to simply remove the ViewRenderer helper.

$this->_helper->removeHelper('viewRenderer');

If you don’t want the ViewRenderer disabled across your entire application, you can set it locally in a controller…

$this->_helper->viewRenderer->setNoRender(true);

Using PHP To Send A UDP Message

Previously I’ve written about sending UDP datagrams using Perl and JavaScript with node.js.

As I’ve been using a lot of PHP recently, I thought I’d show how to send data over UDP using PHP.

If you’ve not come across UDP before, it’s a simple protocol for sending messages over the internet that aren’t guaranteed to arrive. You just fire them off and hope they arrive.

The previous examples showed sending a simple heartbeat message, a string simply saying “PyHB”, to a server listening on port 43278. I’ll do the same in this example.

Firstly we’ll setup a few variables saying where we’ll be sending the the message to, along with how often we want to send the message and what it should say.

$server_ip = '127.0.0.1';
$server_port = 43278;
$beat_period = 5;
$message = 'PyHB';

Now we have to create the socket, using the socket_create function. We need the domain to be of type AF_INET as we are using an IP4 address for our server, the type needs to be SOCK_DGRAM and the protocol needs to be SOL_UDPas we’re sending a UDP datagram.

$socket = socket_create(AF_INET, SOCK_DGRAM, SOL_UDP))

It’s time to send the message now, and to do this we can use PHP’s socket_sendto function. This function takes the parameters of the socket to send the message over, the message itself, the length of the message, some flags, the destination address of the message and the port to send it to. We’re not setting any flags so we can just set this field to 0. The following code will do the trick.

socket_sendto($socket, $message, strlen($message), 0, $server_ip, $server_port);

Let’s wrap this up in a loop that sends the message every $beat_period seconds.

<php
$server_ip   = '127.0.0.1';
$server_port = 43278;
$beat_period = 5;
$message     = 'PyHB';
print "Sending heartbeat to IP $server_ip, port $server_portn";
print "press Ctrl-C to stopn";
if ($socket = socket_create(AF_INET, SOCK_DGRAM, SOL_UDP)) {
  while (1) {
    socket_sendto($socket, $message, strlen($message), 0, $server_ip, $server_port);
    print "Time: " . date("%r") . "n";
    sleep($beat_period);
  }
} else {
  print("can't create socketn");
}
?>

This code is run on the command line, and not via a web server.