Q&A about software engineering

 Posted by on August 23, 2012  General  11 Responses »
Aug 232012
 

This morning I got an e-mail from a 10th grade student asking me some questions about software engineering for a school assignment. I found his questions to be well-posed and thoughtful, so I figured I’d post my responses as a blog post. I’ll be sending this post to Jordan, so if anyone has additional comments or advice for the kid, please feel free to leave some wisdom in the comments!

Hello. My name is Jordan *******, and I’m a sophomore at ******* High School in Indiana. While browsing around Google, I saw your resume for being a software engineer. This intrigued me because I love to mess around with computers, mostly on the software level. I’ve been programming websites in PHP/JavaScript/HTML for about a year now, and more recently (approximately 6 months ago) began writing a video game with some friends using C#, JavaScript, and even a little C++. It’s been an enjoyable project. The real reason I’m contacting you though is this term for school we were assigned to research a field of engineering. I happily chose to research software engineering, and am seeking information from professionals like yourself to enhance my report. Attached to this email is a Word document with several questions about software engineering that I was hoping you could answer. I would greatly appreciate if you could email me back within the next week, because your answers would not only help my report, but also be very interesting.

Continue reading »

Disabling the layout in Zend Framework 2

 Posted by on August 16, 2012  General  6 Responses »  Tagged with: ,
Aug 162012
 

Sometimes you need to disable the layout for a specific action. To do this, you simply set the view model that your action returns as “terminal”. This tells ZF2 not to wrap the returned view model with a layout.

<?php
 
namespace Application\Controller;
 
use Zend\Mvc\Controller\ActionController;
use Zend\View\Model\ViewModel;
 
class IndexController extends ActionController
{
    public function nolayoutAction()
    {
        // Turn off the layout, i.e. only render the view script.
        $viewModel = new ViewModel();
        $viewModel->setTerminal(true);
        return $viewModel;
    }
}

More examples can be found in Rob Allen’s ZF2TestApp.

Aug 122012
 

This post will show you how to create a simple view helper in Zend Framework 2.

In this example, our view helper will simply return the full, absolute URL of the current page/request.

<?php
// ./module/Application/src/Application/View/Helper/AbsoluteUrl.php
namespace Application\View\Helper;
 
use Zend\Http\Request;
use Zend\View\Helper\AbstractHelper;
 
class AbsoluteUrl extends AbstractHelper
{
    protected $request;
 
    public function __construct(Request $request)
    {
        $this->request = $request;
    }
 
    public function __invoke()
    {
        return $this->request->getUri()->normalize();
    }
}

You’ll notice that this particular helper has a dependency — a Zend\Http\Request object. To inject this, we’ll need to set up a factory with the initialization logic for our view helper:

<?php
// ./module/Application/Module.php
namespace Application;
 
use Application\View\Helper\AbsoluteUrl;
 
class Module
{
    public function getViewHelperConfig()
    {
        return array(
            'factories' => array(
                // the array key here is the name you will call the view helper by in your view scripts
                'absoluteUrl' => function($sm) {
                    $locator = $sm->getServiceLocator(); // $sm is the view helper manager, so we need to fetch the main service manager
                    return new AbsoluteUrl($locator->get('Request'));
                },
            ),
        );
    }
 
     // If copy/pasting this example, you'll also need the getAutoloaderConfig() method; I've omitted it for the sake of brevity.
}

That’s it! Now you can call your helper in your view scripts:

The full URL to the current page is: <?php echo $this->absoluteUrl(); ?>

Note: I’ll be covering the details of the getViewHelperConfig() method and how ZF2 uses the ServiceManager to handle view helpers, controller plugins, etc in a later post.

Aug 102012
 

Sometimes I like to show someone my working/development copy of a project without having to commit/push or upload to a server.

Note: This post builds on my dynamic virtual hosts post, however the concept described here can be used to simply make port 80 on your local machine public from any connection, regardless of your virtual host configuration.

To accomplish this, I lease a static IP from my ISP (you could probably use dynamic DNS, too), and point *.ip.evan.pro to my static IP at home. I can then turn on and off port 80 forwarding when I want to show something to a friend (actually, I leave port 80 forwarded and toggle it wih iptables). In the Nginx, I simply added .ip.evan.pro to the server_name directive so it reads: server_name .dev .ip.evan.pro; (ServerAlias on Apache).

With port 80 open, I can point people to http://myproject.ip.evan.pro/ and it will load right up.

Note: Many broadband ISPs in the US such as Cox and Comcast tend to block port 80 on residential connections. I happen to be using a CenturyLink (formerly Qwest) VDSL connection, and they’re kind enough to let me use my ports however I’d like.

So using *.ip.evan.pro is great when I’m at home and have control over forwarding port 80, but I also wanted to be able to show people my working copy no matter where I was connected from (public wifi, friend’s house, conference, office network, etc).

Continue reading »

Aug 102012
 

I’ve been meaning to blog about this topic for quite a while, but like most other I kept putting it off. However, a recent exchange on Twitter has shown there might actually be some demand for this, so here it is.

Over the years, I’ve slowly evolved my web development environment for efficiency and convenience. A couple of years ago, I realized that I was sick and tired of having to create a new virtual host and edit my host file for every single project, and decided to come up with a solution. Two years later, and I couldn’t imagine working without my awesome dynamic virtual host setup. It’s well-worth the few minutes you’ll invest getting it working.

So just how cool is it? Well, let’s take a look at how simple it is for me to start a new project:

Continue reading »

Jul 182012
 

This post is intended to familiarize you with the various features of the new Zend Framework 2 ServiceManager component along with some simple examples.

So, what is the ServiceManager? Basically it’s a registry, or container (the proper term is service locator) to hold various objects needed by your application, allowing you to easily practice Inversion of Control. The service manager holds just the information needed to lazily instantiate these objects as they’re needed. So if you were thinking ‘services’ such as those composing a service layer, you might be better off thinking of the service manager more as an “object manager” or “instance manager”.

Continue reading »

Jul 182012
 

So you’re all excited to try out ZF2. You clone the skeleton, install some modules, maybe even follow Rob Allen’s excellent ZF2 tutorial, and finally, start building your application. Now, if you’re a former ZF1 user or refugee from another framework, you might be troubled at this point by the fact that, at first glance, ZF2 doesn’t appear to take into consideration environment-specific configuration values (e.g., development, testing, staging, production). Luckily, this is not the case!

Continue reading »

Jul 152012
 

This is meant to be a short, easy-to-follow tutorial to help you get started with Zend Framework 2.0 and add perhaps one of the most common modules, ZfcUser. By the end of this tutorial, you’ll have a simple ZF2 application with user registration and authentication capabilities.

Prerequisites

I’m going to assume you have the following installed:

  • PHP 5.3.3+ (with pdo-sqlite or pdo-mysql)
  • A web server and knowledge of how to set up a virtual host
  • Git

If you have PHP 5.4+ you don’t even technically need a web server to try out ZF2, as you can simply use PHP’s new built-in development web server.

A note about composer

While composer is a supported way of setting up the skeleton, I am choosing to simply use git for the sake of this tutorial. In my opinion, this keeps things simpler, and easier to debug if anyone has issues following my tutorial.

Continue reading »

Jun 292012
 

The new Zend\Db in Zend Framework 2 has a handy feature which allows you to specify your own entity/model class to represent rows in your database tables. This means you can tell Zend\Db to return each row as a populated instance of your own custom objects. Keep in mind that this is simply a convenience feature, and not meant to serve as a fully-featured ORM. If you’re looking for a full-blown ORM, have a look at Doctrine 2.

For this demonstration, let’s assume the following table:

CREATE TABLE `book` (
   `isbn10` VARCHAR(10) NOT NULL,
   `isbn13` VARCHAR(13) NOT NULL,
   `author` VARCHAR(50) NOT NULL,
   `title` VARCHAR(255) NOT NULL,
   `year` INT(4) NOT NULL
);

Continue reading »

Jun 192012
 

Today I gave a talk at Go Daddy’s annual TechFest conference about building modular web applications with Zend Framework 2.0. The talk went well, and the audience was surprisingly engaged and inquisitive! I would like to thank Go Daddy, and specifically Kurt Payne for allowing me to speak at this awesome conference.

If you attended my talk and have any comments or feedback, please feel free to comment on this post. Thanks!