Blog posts by Geekyboy – Adam Culp

  • Zend Framework 2 XML Sitemap

    While tweaking the SunshinePHP site to be a bit more SEO friendly I realized we had neglected to create a sitemap for search engines to find.  I was pleasantly surprised to see the Navigation component of Zend Framework 2 includes a bunch of view helpers, including a Sitemap helper.  So now I have an xml sitemap created by Zend Framework 2 that works hand in hand with the site navigation.  However, the documentation was not complete as of this writing and caused me to do a bit of trial and error debugging to get it working.  Below I will post how I got it working, in hopes it will help others. (If the ZF2 folks like this post I will go in an update the documentation later.)  As with most things in Zend Framework 2 there may be more than one way to do things, but this is how I did it. (Until someone informs me of a better way.)

    Module Config

    In the Application module.config.php I created a factories node in the service_manager container where I pulled in the DefaultNavigationFactory.

    factory

    Then I also added a navigation container where I specified the sitemap for the site.

    NOTE: To add navigation specific for each module you would simply create this container in the specific module.config.php.

    navigation

    Next I added a route for the future sitemap to be viewed.  Notice how I simply added a sitemapAction to the Application IndexController.  You can add it wherever you desire if you want to create a separate controller or whatever, I just left it there.

    route

    Layout

    Because I just want the xml produced by the helper, I created a blank layout xml.phtml that does nothing more than output the content of the view.

    layout

    View

    The sitemap.phtml view is also pretty simple and outputs the xml sitemap created by the helper.

    view

    Controller

    In my controller I specified the layout to use, nothing more was needed.

    controller

    Verify

    By navigation to the URL specified in the route we should now be able to view the XML output.

    sitemap

    Future

    In this example someone would need to navigate to /sitemap to view the sitemap, but some automated tools would try to go to /sitemap.xml which would fail with this setup.  I will come back at some point in the future and enable the file extension to be ignored (after I figure out how).

    Conclusion

    The entire process is really pretty simple once the pieces are all in place, and the output was accepted by the various search engine webmaster tools…SCORE!

  • PHPUnit, Composer, PHPStorm, Oh my!

    Installing PHPUnit within a project via Composer, then running tests through PHPStorm is not an intuitive process. However, with the right steps it’s actually pretty simple. Here is my story:

    To launch the call for papers for the SunshinePHP Developer Conference it was a pleasure to use the OpenCFP project as a starting point rather than creating the entire thing from scratch.  While the project is still a “beta” with a few wrinkles to get ironed out, it’s still a pretty nice effort. (I have a pull requests pending, and another to submit.  Love open source!)

    For my CFP I wanted a few more fields of information than the “out of the box” setup, so I quickly added them to the app.  However, doing this meant the included unit tests would fail.  But wait, I hadn’t run the unit tests yet!  I realized immediately how spoiled I had become with today’s modern frameworks with a testing method built in.  This little project did not have that luxury, so I would need to run the tests the old fashioned way, or let an IDE do it for me.  I decided to configure PHPStorm do it. (I’ll do the same for Zend Studio in another post later.)

    The way OpenCFP was set up, using Composer, meant that PHPUnit was already placed in the /vendor directory as a requirement in the composer.json.  So rather than taking the lazy way out and using the PHPUnit already installed globally on my system, I wanted to use the latest PHPUnit within the project.  This requires 2 setup steps in PHPStorm.

    Step 1

    To start I needed to inform the IDE where to find the Composer autoload file and leverage the awesome PSR-0 goodness to autoload PHPUnit in the /vendor directory.  To do this I open the Settings via the icon on the toolbar, or by using the File->Settings menu item, or hitting the Ctrl+Alt+S keyboard shortcut.  Then in the Project Settings (top section) I expanded PHP to get the PHPUnit dialog.

    PHPUnit setup in PHPStorm

    Step 2

    Now I had to add a Run/Debug Configuration for the project.  I did this by clicking on the toolbar dropdown and selecting Edit Configurations.

    Edit Configuration

    Once the dialog opened I clicked the “+” to add a run configuration, and select the PHPUnit type.

    Choose configuration type

    Now it was just a matter of adding the directory where the tests reside. (Name the configuration to your taste.)

    Location of tests

    All done!!!  Now I was able to run the tests simply by selecting the new run configuration defined in the dropdown, and clicking the Run button in the toolbar.

    Good luck, and happy testing!

  • Copy and paste from virtual machines to host

    With the growth of using virtual machines as development environments, thanks to great projects like Vagrant and Puphpet.com, I have been using virtual environments for most of my development these days.  On some occasions, because I use Ubuntu as my primary operating system on my development laptop, I have the need for a Windows environment in order to work with certain clients. (Sad, I know.)

    Well, while using these Windows environments it is a royal pain if I cannot copy and paste back and forth between the host and virtual machine.  So I have always resorted to installing the Guest Additions (I use VirtualBox) to make this possible.  However, recently this stopped working for some reason.  I honestly didn’t have the time to investigate so just overlooked it and kept going. (I honestly don’t use a gui very often within a vm.)

    Today I finally got tired of looking the other way and investigated why I couldn’t copy/paste between host and vm.

    The Fix

    Turns out there is a very easy solution.  I guess at some point the copy/paste functionality setting is turned off by default.  So I simply needed to click on the VirtualBox “Devices” menu drill into the “Shared Clipboard” submenu and then select the option I desired.

    Now things are working as I expected.

  • XHProf PHP Profiling

    Today I set up my development environment so I can use XHProf to profile PHP scripts when needed, and it was pretty easy.

    For starters, I use Ubuntu as the operating system for my desktop environment. So while the information below may be helpful, I will not cover any other OS in my descriptions.

    XHProf is a PECL package, and can be easily installed by using standard PECL commands.  However, XHProf is still beta and the default settings of PECL will only install stable packages.  Don’t fear, there is a way within PECL to handle this by appending “-beta” to the end of the module name: (also note how I am using sudo to act as the admin user on Ubuntu)

    sudo pecl install xhprof-beta

    After issuing the command PECL will do its job of installing the PHP module but will not add it to the php.ini, so that must be done manually.  The default location of the php.ini in Ubuntu is at ‘/etc/php5/apache2/php.ini’, so we edit it there:

    sudo vi /etc/php5/apache2/php.ini

    We add a single line to the end of the php.ini to activate the module:

    extension=xhprof.so

    Then we restart Apache for the new setting to take affect:

    sudo service apache2 restart

    At this point we now have the XHProf module in PHP for Apache related calls to PHP.  If we add ‘phpinfo();’ to a PHP file and view it in a browser we see XHProf is now available for use.

    NOTE: This does not make XHProf available for CLI activity. (command line run PHP scripts)  We also need to add the extension to the ‘/etc/php5/cli/php.ini’ file as well to make it available via command line PHP.

    Now that XHProf is ready to be used I added it to the PHP script I wanted to profile.  Basically this is just a command to kick off the profiling at the beginning of the script, and some commands to save the results at the end of the script.

    In the beginning add the following to start recording at the beginning, and adds CPU and memory info to the output:

    xhprof_enable(XHPROF_FLAGS_CPU + XHPROF_FLAGS_MEMORY);

    Then at the end add the following to halt profiling and then using XHProf utilities create the output:

    $xhprof_data = xhprof_disable();
    $appNamespace = 'some_namespace_here';
    include_once '/usr/share/php/xhprof_lib/utils/xhprof_lib.php';
    include_once '/usr/share/php/xhprof_lib/utils/xhprof_runs.php';
    
    $xhprof_runs = new XHProfRuns_Default();
    $xhprof_runs->save_run($xhprof_data, $appNamespace);

    NOTE: XHProf will dump the results in the ‘/tmp’ location on the system named something like {run_id}.{app_namespace}.xhprof (Ex.- 51f384b8cb9f2.some_namespace_here.xhprof), but the contents are not truly human readable.  I recommend using the tools provided by XHProf to help make them viewable in HTML.

    How I made XHProf files pretty

    I am Lazy a HUGE fan of doing things simple, so to make the XHProf output readable and easier to use I simply created 3 files in my document root of Apache like so:

    index.php

    include_once '/usr/share/php/xhprof_html/index.php';

    callgraph.php

    include_once '/usr/share/php/xhprof_html/callgraph.php';

    typeahead.php

    include_once '/usr/share/php/xhprof_html/typeahead.php';

    No sense in reinventing the wheel.  These 3 files simply included the files already existing in the XHProf module, which call the output files directly from the ‘/tmp’ location on my system.

    By calling to http://localhost/index.php I am now greeted with a list of links representing the different files output from XHProf to the ‘/tmp’ location as I executed the PHP scripts calling to XHProf. (shown below)

    xhprof_files

    Now when I click on the individual links I can view the output from XHProf in a friendly HTML format, like below:

    xhprof_details

    So, there we go.  Enjoy!

  • Developer advice

    As the organizer of the SoFloPHP User Group I am often approached by entry to mid-level developers asking what they can do to advance in their career or become better developers.  Of course I am nowhere near perfect but have been around long enough to get a few bumps and bruises along the way, so below is what I usually share as some pointers:

    Note: While some of these items are kind of PHP specific, others may find useful items as well.

    • No self-respecting person should be up at 4:05am sending emails.  Get some sleep. :)  It is OK to stay up late once in awhile, but force yourself to get to bed at a decent time (10) each day.  And try to get up early each day also (6 or 7), which will help you get much more out of your days. 😉
      • The myths about developers working all night on caffeine are false.  Yes, it happens sometimes, but it is rare.  Well rested developers learn more, write better code, and get more work done…period!
    • Track your time, and get in the habit of knowing what you did with each hour.  I personally use Hamster religiously, and find that I get much more done each day as a result. (I have it set to nag me every 15 minutes if I have not set an activity.)  If you are not using Linux as your desktop environment I am sure there are time trackers for the other operating systems, find one.
    • Certifications will not actually carry much value on your resume, so I would not make them a main focus.  Sure they do carry some value, but perhaps not in the way you desire.  Achieving a certification is a great personal accomplishment and will make you feel better about yourself, as well as give you bragging rights among developers.  (Most developers tell you they don’t care about certifications, but deep inside they are simply envious.)  While many certifications are not a true gauge of actual knowledge, they do represent a certain amount of skills.  However, I have found that most employers do not even notice certifications.  I am not saying don’t get them. What I am saying is to be aware the actual accomplishment may be different than you perceive.  When I started getting certifications it reinforced, in my own mind, that I knew what I was doing.  That gave me more confidence overall in my jobs, and was still a big “win”.  But do them in your spare time, not as a focus item.
    • Pick an IDE to use and learn it FULLY. I will not recommend one in this post, so explore and find one that fits how you want to work.  Then learn it COMPLETELY, and use it ALWAYS.
      • If an IDE causes you pain, don’t use it any more.  Pick another one.  This tool will be where you spend most of your day, so you should not be forced to spend your time debugging and fixing your IDE.  It should not crash regularly.  You should not dread opening it, instead you should look forward to launching it.
      • Use all parts of your chosen IDE. (FTP, version control, testing, coding, debugging, issue tracking, etc.)
      • Learn the keyboard shortcuts, they will save you time.
      • Just because an IDE is free does not mean it is good.  You should base purchases on value provided, not $$$.
    • Pick a plain text editor, and learn it well.  There are times you just need to do a quick edit, and opening an IDE, creating a project, etc. is just overkill for this.  Again, there are many of these available so I will not recommend a certain one.  Pick what you like best.
      • The best ones come with syntax highlighting.
      • There are some free ones, but don’t be afraid to pay a few bucks for a good one.
    • Pick a pet “full stack” PHP framework to learn, FULLY.  I recommend either Zend Framework 2, Symfony2, or CakePHP 2.* since these 3 are the most common.  But as with an IDE you should learn one COMPLETELY, and use it most of the time.  Each framework has its strengths and weaknesses, so choose one that works best for you.
      • Good frameworks have mechanisms in place where you can add plugins, modules, or helpers in case the framework does not fully support what your trying to do.  But stick to the framework as much as possible.
      • Feel free to write your own framework, but ignore the urge to use it for employers.  As professional developers we owe it to our employers to use more widely available frameworks.  It is just smart business.  It means businesses can find other developers easier, onboard them faster, and train the group more.
    • Always strive to make yourself replaceable.  If you are replaceable you are also promotable, and you can go on vacation pain free.
    • Learn to use GIT for source control, and use it for EVERY project you do no matter how small.  Sure there are other source control products out there, but currently GIT is the way to go.  All it takes is the command ‘git init’ in a directory and you are of and running.  No excuses!
    • Do things publicly so others can see.  Such as github, BitBucket, etc.  I recommend having code in some sort of public place for others to see how you code.  Don’t be shy.  I’ve had other developers provide feedback on code I posted on github, in a constructive way, and it helped me advance my skills.
    • Your LinkedIn profile is your best career tool as a developer.  Tweak it, adjust it, get everyone you can to contribute to it.  Add projects to it, etc. (See “Build your brand” below.)  Don’t connect with everyone who pops up, and be stingy with what recruiters you allow to connect with you.  If someone is not going to help your career in some way, they do not belong in your connections on LinkedIn.
    • Pick up small projects here and there that are NOT urgent, and you can take your time on.  These little projects will afford you a way to learn new things.
    • Get active in the PHP community.  I mean really active.  Sure, it’s OK to be a member of other communities as well, but the PHP community (world-wide as well as local) is what will really “do it” for you. (If you are going to make a career doing PHP.)
    • Give talks at local user groups, blog about your experiences, follow other blogs of good people (phpdeveloper.org is a good place to see activity of PHP community members blogs. Chris Cornutt does a great job at filtering out relevant posts and adds the best of them on this site.)
    • Get somewhat active on Twitter, join IRC channels, travel to a couple of conferences each year and get to know people “doing things”.  Then eventually start submitting talks to the conferences so you can go talk, and have your expenses covered to go to it.
    • Build your “brand”.  By this I mean to say YOU are the product.  Everything you say and do is your offering.  Your name is your “brand”.  Build the reputation carefully, and before you do anything ask yourself, “Will my customers like/buy this?”  If the answer is “yes”, then go for it.  If the answer is “no” re-evaluate.
    • If you are a woman, be careful.  While women are becoming a larger part of the tech community there are still many men who are not used to it yet.  They are jerks, and your feelings will get hurt sometimes in the process.  Learn to ignore them and focus on the good parts as you grow.  KNOW you are going to do great things, and work toward that progress.
    • Learn Linux via command line.  No need to go crazy with this one, but since most web servers are on Linux it is a good idea to have some knowledge in this area.  You should at very least know:
      • Basic vim commands to edit files on the server.
      • Be able to navigate the OS files and directories.
      • Be able to manipulate files on the server. (cut, copy, paste)
    • Spend some time each day on Stackoverflow.  Try to pick a problem someone posted and help them.  “Doing” is the best way to learn, and there are plenty of problems posted to Stackoverflow daily.  This is addictive, so manage your time and limit yourself.  But do it!

    Of course there are many more tips, but I wanted to hit on some key items without writing a book on this blog post.  I hope you find this information helpful, and if you can think of some other hints and tips please feel free to share in the comments.

    Good luck!!!

  • CentOS networking not active after installation

    I needed to install a CentOS 6.4 server in a VM and after installing I was not able to hit anything on the Internet, or any network for that matter.  When I tried using the yum packages manager I received the error “Couldn’t resolve host ‘mirrorlist.centos.org…” and I was at a loss on what to do next.  I was not able to ping anything.

    This led me to look into the networking and and upon opening the /etc/resolv.conf I found it totally empty.  So I then opened /etc/sysconfig/network-scripts/ifcfg-eth0 and found the issue.  The line “ONBOOT” was set to “no”.

    The complete file /etc/sysconfig/network-scripts/ifcfg-eth0 after I altered it: (NOTE: the only thing I altered was the ONBOOT flag.)

    DEVICE=eth0
    HWADDR=09:00:27:D7:61:8D
    TYPE=Ethernet
    UUID=q93perfj-earf-ewrf-wqer-546877145475
    ONBOOT=yes
    NM_CONTROLLED=yes
    BOOTPROTO=dhcp

    After changing “ONBOOT” to “yes”, then rebooted for the configuration to take effect, all is good now.  I probably could have simply done a networking restart, but reboot on a fresh system was pretty fast.

  • PHP usage statistics

    Every once in awhile I stumble across someone who is trying to find their way and decide what they  will do in their career.  As the organizer of a PHP user group I see many new developers passing through.  Of course I always speak of how strong PHP is in the web markets, and encourage new web developers to pursue PHP as a tool in their box of goodies.  Because as a web developer it would be a career limiting move to not have any knowledge of PHP.  Here is why:

    Stats on website technologies

    http://w3techs.com/technologies/overview/programming_language/all

    In the link above we can see some enlightening metrics on PHP usage in websites:

    • PHP is used by 79.8% of all websites where the server technology is known.
    • The next closest competitor is ASP.Net at 19.8%.
    • Java is seriously trailing in web at 3.5%.
    • Ruby is hardly represented at 0.5%.

    NOTE: A website may use more than one technology, so the numbers are not a perfect 100%.

    I was a little shocked to see how high the numbers were when I saw them.  I mean, what about github, and Basecamp, and other sites using alternative technologies?  Well, after more thought I realized that while these popular sites do use non-PHP technologies, they are just single sites.  However when taken into account in the scope of the entire web, they only count as one.

    Note on limited career moves

    Now don’t take what I am saying totally wrong.  I am not so naive to think a web developers cannot survive without PHP in their toolbox.  I realize there are many developers using alternative technologies who have never touched PHP, and they are able to survive.  I am simply saying that if a web developer really wants to do well they will run into PHP more often than not, and it would be a shame if they were not able to play in that ballpark.

    The 20.2% of websites not using PHP is still a very large number.  According to Netcraft there are around 672 million active websites at last estimation.  Which means there are around 139 million websites not running PHP.

    PHP Versions

    To get a little more fine grain, lets look at what version of PHP is being used:

    http://w3techs.com/technologies/details/pl-php/all/all

    In that link we see:

    • PHP version 5 is used for 97.1% of all PHP websites.
    • PHP version 4 is used for 2.9%

    For those still using PHP 4, it is time to make solid plans to get updated.  It has been almost 9 years since PHP version 5 was released.  That is a very long time, and those old PHP 4 sites and applications need to be updated. (Aim for the current version 5.4)

    Now within PHP version 5 there are many sub-versions, so to gain more insight into how this breaks down we look at the following link:

    http://w3techs.com/technologies/details/pl-php/5/all

    So:

    • PHP 5.2 and 5.3 make up the largest share with 93.2% of the overall
    • PHP 5.3 is 49.6%, which I think should be higher
    • Newer 5.4 is very low at only 4.1%.

    I was sad to see version 5.3 was not significantly higher than 5.2, but it kinda makes sense.  Because 5.3 was such a drastic change in the object model introducing namespaces, late static bindings, and closure, many developers feel intimidated to update their servers from 5.2 to 5.3 even though the upgrade would most likely not break anything in their code.

    I would encourage anyone running their code on 5.2 to try out 5.3 or 5.4 on a “testing” server, then if all is well plan to migrate in production as well.  Then they can take advantage of more speed and include some of the new technologies that came with 5.3 and 5.4 in their code.

    We can clearly see how much PHP rules the web space.  But what about elsewhere?

    Overall programming language usage

    I thought I would share another stat about programming languages as a whole, regardless of where they are used.

    http://www.tiobe.com/index.php/content/paperinfo/tpci/index.html

    Here we can see PHP still ranks pretty high, and has been holding pretty steady for some time. While PHP rules where it was “intended” to be used (websites), it is versatile enough to still rank high overall.

    In closing

    So yes, there are many programming languages out there and I encourage all developers to learn as much as they can. But as professionals we cannot possibly learn them all and be good at all of them.  So I say pick one, and learn it very well while still dabbling with others.

    There is nothing wrong with building a career around PHP, and it can be quite profitable since PHP is so wide spread.

    Above all, live up to the title “Professional Developer” and always strive to learn more. And if you are a web programmer it would be a mistake in your career (in my opinion) not to know the most predominant technology in the space…PHP.

  • Skype sounds audio distorted in Ubuntu

    Now that I have Skype installed on Ubuntu 13.04 I discovered that the various Skype sounds were distorted.  It was almost like the sounds had some static mixed in with them.  I thought it was probably just a problem with the new version of Skype, or the new sound drivers.  However, I made a discovery that fixed the issue.

    I tried the AlsaMixer fix, but it didn’t work. (A reboot simply resets the PCM to 100 again, and the sound is still crackly.)  So it all came down to a simple file edit to get it fixed.

    Edit the file /etc/pulse/default.pa using the command:

    sudo gedit /etc/pulse/default.pa

    Add the following to the end of the line shown:

    pulse audio file

    So after adding “tsched=0” to the end of the line “load-module module-udev-detect” you will be all set.  After a reboot the sounds is still good.

    Enjoy!  Hope it works for you.

  • Skype for Ubuntu 13.04 Ringtail

    I am running the new version of Ubuntu 13.04 Raring Ringtail, and so far really like it.  However I’ve had a bit of trouble with Skype, because I could not get it to use the indicator area of the tray.  Other than that it seemed to work fine.

    When I installed Skype I did it from the Skype website, and didn’t realize there was a package at http://archive.canonical.com/ partners already carrying it because that repository is not turned on by default for Aptitude.

    The repository can be activated by either command line, or by using the Software & Updates which enable it for command line or Ubuntu Software Center, or Synaptic Package Manager.

    Via terminal:

    sudo add-apt-repository "deb http://archive.canonical.com/ $(lsb_release -sc) partner"
    sudo apt-get update

    Via GUI:

    Open the System Settings and click on the Software & Updates icon, or using the Dash you can simply type “Software & Updates”.  Once it opens you can select the “Other Software” tab and check the first box titled Canonical Partners.

    Software and Updates

    Now we are able to install Skype from the Canonical Partners repository no matter what method you wish to use.

    Install Methods:

    From terminal

    sudo apt-get install skype

    Or search for it through Ubuntu Software Center or Synaptic Package Manager and install nromally.

    It will install some other required packages with it, but after the install it now works as expected with the indicator and all.

    Enjoy!

  • Hack-a-thons are not “normal”

    In life I tend to do things a bit strange.  Not what most would consider “normal”.  For instance, I run thousands of miles each year and have been known to run up to 100 miles in a single week. (Yes, run.)  I am a black belt in Judo, and enjoy being thrown to the ground, only to bounce up and get my turn bouncing someone else.  I love scuba diving, and feel a great sense of relaxation while deep under water with only the sounds of my own breathing and bubbles around me.  I’m the organizer of a PHP user group, and the organizer of a PHP conference.  My family and I take vacations where we hike 30 or more miles over a few days, and come home feeling rested.  To top it all off, I love to refactor code!

    So, no, I do not live life in the “normal” zone.

    However, when it comes to coding PHP I do things pretty much as you would expect from a senior developer.  Most of what I do is pretty normal, with only a small dash of interesting here and there to satisfy some exotic needs.  Of course I spend most of my time these days refactoring other people’s code, but even then it is pretty normal and usually falls into a normal pattern.

    Then a couple weeks ago I had the great opportunity to organize my first hack-a-thon for the South Florida PHP User Group (SoFloPHP), and it sure was an eye opening experience for me.  Things kicked off pretty normal as most attendees split up into groups and started discussing their projects for the day, and the coding began.  I also had a small project I intended to work on, but ended up spending most of my day pulled between groups to help out in one way or another.  Questions on how to set up hosting, how to use Git version control and github, as well as how to use CakePHP.  I loved it, and really enjoyed helping others with their projects.

    Later in the day I was helping someone with a Git workflow when he said something that hit me squarely in the face.  He said, “I do not get to use Git in my normal job, so it is nice to do it here.”  Now this is not the first time I have heard such a thing, but for some reason it really sunk in this time as I realized that hack-a-thons are not for “normal” things we do every day.  Instead we enjoy hack-a-thons and other social coding activities because it affords us a chance to learn new things, use technologies we would not normally get to touch, and to go beyond our “normal” things.

    This opened up a whole new world for me, and from now on I have another way of looking at these social activities.  Attending activities like this are educational, enlightening, and door opening as well as presenting a social aspect that really helps developers advance their skills and networks.

    I can’t wait until the next one.