Working in a Vacuum

While reading Indi’s blog post about the Colombo Hackathon, I was really surprised about the comments some people were making. They were bordering on the xenophobic and some were outright against the concept simply because there was commercial backing behind it. I don’t understand why commercial backing or presenting to an open audience is a bad thing.  I find that one of the unique things about startups is that it fosters a sense of community. Yes, this may sound a very ‘hippie’ thing to say but startups are unlikely to work if you do not have that and you work in a vacuum. It’s not about stealing ideas, it’s about helping other people and finding people who can help you. Paul Graham wrote a great essay about this, and it’s well worth a read.

If you have a game changing idea and you plan to make this happen, how will you do it without opening up? How do you even know whether the idea is stupid or would only work for 100 people out of the billions out there? You need to talk to other people. If you have the resources, technical skills, business acumen and laser focus and belief to push through alone, then you’re one of the lucky few. Yes, ideas can be stolen, but it’s about the execution. Why did MySpace die out while Facebook is thriving? They are both about connecting people. It’s easy enough to say “I’m going to build a photo sharing site” and there’s nothing revolutionary about it but Instagram went from having 1 million accounts in January 2011 to 15 million in January 2012 to 25 million in March 2012. And they are growing faster than Flickr.

The point I’m trying to make is that all of these startups who are currently connecting with millions of people have a vision that is very much larger than what is immediately visible (be it a photo uploading service or a way to share your location with friends). This is why they lead. Even if someone clones the idea, they are thinking and working on the next step. Just look at Pinterest for example, they have many clones but they aren’t even slowing down. It’s really important that you have a vision beyond that simple idea.

And to people who argue against showing your idea to investors and so forth, I don’t really see the point. It’s up to you to choose the right investors for your startup. You can treat it as a business transaction or you can look at it as mentoring as well as financial backing, that’s up to you. But without investors, it’s going to be really hard to get things going. They are there to back winners, prove to them your idea is awesome, you have the vision and you’re the right team to push it through and you’re set! Why would they want to steal your idea and implement it themselves when they can simply have you execute it? Funding is a very necessary part of any startup. If you don’t have enough capitol to see you through building the product, then it doesn’t matter how good you idea is. If you’re leery to go with private funding, you could always use avenues like Kickstarter or YCombinator.

At the end, if you think you have a great idea, share it with people, get some feedback and execute it. That’s all that matters.

 

Dynamic Module Switching in Zend Framework

Recently I ran into the problem of needing to switch the module part of a request depending on the hostname. I needed to do this because I plugged in a whitelabeling solution to our existing web application. When the request comes in, I needed to switch from the default module to the whitelabel module so that app routing would work. I’m not 100% sure that this is the best way to go for this scenario, but this is the only way I could think of doing it. I’d be interested in finding out any alternatives to this method.

I solved this problem by creating an action helper with a preDispatch() hook that would switch the module depending on the hostname. If you want the hooks to work you need to instantiate the helper in the bootstrap, otherwise it won’t work since helpers are lazy loaded.. This article has more details about action helpers, but it’s a bit dated - http://devzone.zend.com/1218/action-helpers-in-zend-framework/

Create the Action Helper

class My_Controller_Action_Helper_Whitelabel extends Zend_Controller_Action_Helper_Abstract {

    public function preDispatch() {
        /**
         * Here we want to check whether this is a whitelabel url, and if so switch the module.
         */
        if ($this->isWhitelabelUrl() && $this->getRequest()->getModuleName() == 'default') {
            $this->getRequest()->setModuleName('whitelabel');
            $this->getRequest()->setDispatched(false);
        }
    }

    /**
     * Checks whether the current host is a whitelabel or not
     * @return boolean
     */
    private function isWhitelabelUrl() {
        //code to check condition
    }
}

Note that ‘My’ is just a namespace I use. You can even put this inside your application folder and not in a separate library folder. You can read this question on SO for more information - http://stackoverflow.com/questions/4701177/zend-action-helper.

$this->getRequest()->setModuleName('whitelabel');
$this->getRequest()->setDispatched(false);

Switching modules only takes two lines,  the first line resets the module and the second line sets the dispatched flag for the request. If you don’t set the dispatched flag to false, Zend will just serve the default module. Keep in mind that if you don’t have the conditional, Zend will simply get into a loop because it will keep trying to dispatch the request over and over again.

Now that you have the action helper created, you need to hook it into the bootstrap file so that an instance of the helper is loaded when your application is loaded. You need to do this because you need the preDispatch() hook to be fired during the normal routing process.

Initialize in Bootstrap

In my Bootstrap.php file, I have a _initApplication() method which is fired when the application is loaded. I’ve put the instantiation code for the helper in that method.

public function _initApplication() {
    //code
    Zend_Controller_Action_HelperBroker::addHelper(
        new My_Controller_Action_Helper_Whitelabel()
    );
}

This creates an instance of our whitelabel helper and Zend will execute the code in the hooks at the proper time.

Something I have to look into is, how routing is affected when I set ‘dispatched’ to false. I haven’t found enough information on this yet, but will update this space if and when I find more.

Debugging Tips for Beginners

A common type of question I find on Stack Overflow are beginners having trouble with figuring out why their AJAX application / functionality isn’t working. They usually go something like this…

I have two input boxes which accept the username and password and sends it to the PHP login page via AJAX. But when I type in my credentials, I don’t get logged in. Why isn’t it working?

When starting out to fix a problem, you need to first find out what the problem is. To do that you need to isolate where the problem lies, so break it down into its individual components. In no particular order, these would be

  • The Javascript and AJAX
  • The PHP and server side code
  • The DOM and HTML
Let’s look at each of them in a bit more detail

The Javascript

I do my development on Firefox because I’ve found it has the best addons for the job. If you haven’t already, install Firebug for Firefox. In the page that has the AJAX functionality, open the Firebug console by hitting F12. Make sure the console is enabled (a reload of the page may be required) and carry out the action that triggers the AJAX response. If your JS code works, you should see something like the following in the console.
Firebug console in action

Firebug console in action

If you see any error messages or ‘not founds’ for any of your requests, then you have a problem. Clicking on the request allows you to confirm that the correct request and response is happening in your application. If the requests and responses are correct, then your problem lies in either the HTML or your JS callback.
If you’re getting 404 messages for the request, that mean the PHP file you’re pointing to doesn’t exist. Update your code to fix the problem. Do the same if you’re seeing JS error messages. Make good use of the console (which can run live JS that affects your page) and the debugger (where you can walk through your JS files) .
If on the other hand, your request is sent properly but the response is incorrect then you need to look at your PHP page.

The PHP and Server Side Code

When you work with AJAX, it helps if you make sure that your application works without AJAX initially. This serves the double purpose of making sure your core functionality works and having a fallback when Javascript is disabled. Assuming you’re working with GET and not POST, either copy the request URL from Firebug or craft your own and access it in the browser. If you get an error then you’ve isolated (one of) your problem(s)! Find out what’s wrong and fix it and then see if the AJAX functionality works.

Using an IDE when coding gives you many benefits. Although you may think that notepad might be enough for your needs, something like Eclipse (which I use) or NetBeans (which I’ve used) can come in very handy to catch some problems as you code them.

And don’t forget to have a debugger setup. I mainly use Xdebug, but you can just as easily use ZendDebug.

Once you make sure you have the JS async call working properly with the serverside PHP script, you can look at the callback and the DOM.

The DOM and HTML

The most common reason for AJAX updates not working on the client side is usually because the element you are trying to update doesn’t exist. Confirm that the element exists first. I usually use the .length property or the toggle() method to make sure I have the correct reference.

And for any event issues, make sure that the DOM element exists before you bind any events to it. A related cause is when you bind an event with an element but then update the  element in thee DOM so you lose the reference. An easy solution to avoid this problem is to use something like the .delegate() method if you’re using jQuery.

To sum it up, whenever you run into a problem carry out the following steps.

1. Isolate the problem

2. Focus on the problem area

3. Fix the bug (easy part!)

A Blog is Born. Again.

I used to have a work / technical blog. But then I got bored of it and took it down after a while. It’s been a couple of years since that happened and now I find myself constantly wanting to write down the things that are going through my mind. Mostly for my own reference (what better way to learn than to teach something) but also to showcase the soft skills that are much needed in the field I work in. Let’s jump right in with an introduction.

I’m John Pereira and I’m a software developer. My chosen field is the Web and I mostly work with PHP, Javascript and NodeJS. I’m also familiar with Java to a certain degree. I’m in the process of deciding to learn a new language but it’s currently an even split between Python, Ruby and the Android SDK (Java). Hoping to add the final candidate into the 2012 new year resolutions.

I’ve been coding since I was in school, and I’ve been doing it professionally for about 4 years now. I started out with QBasic and then moved on to VB5 and then a bunch of different languages when I was studying for my bachelors. Most of my work has revolved around large business process oriented systems.

I code for fun and for profit but I haven’t yet contributed to an open source project. This is something I hope to rectify soon, suggestions are welcome. My current toolbox is made up of Zend Framework, CakePHP, WordPress, a bunch of NodeJS libs, MongoDB and jQuery so it would be nice to extend my knowledge in those areas. The only community I have any level of activity is on StackOverflow.

profile for JohnP on Stack Exchange, a network of free, community-driven Q&A sites

It’s not all fun and games though, as a dutiful gamer I put in whatever time I can grinding through levels (where applicable). I’ve already planned out my December – January playlist like so – Battlefield 3, Modern Warfare 3, Arkham City and Skyrim. I also read a lot of fantasy, sci fi and Terry Pratchett (Yes he rates his own mention because he’s just that good).

And in case I didn’t make this clear earlier, I really enjoy what I do.