Skip to content

PHP

Speed up development by catching your mail locally

Have you ever been developing some kind of application that sends email? You need to test how the email looks, so you have to have access to an external SMTP server and you have to configure your application to use that. You can definitely set up sendgrid or another MTA to send email from your local computer and then use a real email address as your target. However, then to develop this portion of the application you need to be online.

Another option that I’ve found is the Mailcatcher gem. This is a small ruby program that you can easily configure as your SMTP endpoint. Then when your development environment sends mail, mailcatcher catches it. Then you can visit a URL on your local computer and view received emails. As soon as mailcatcher shuts down, the emails are lost, however.

Even though this is a ruby gem, you can use the app with different languages–as long as it you can configure the application to point to an SMTP server, you’re good (in the readme, there are examples for Django and PHP).

One note about it being a gem. Don’t put it in your Gemfile if you are building a rails app, because of possible conflicts. This means that if you manage your ruby environments via rvm you’ll need to re-install mailcatcher every time you change your ruby version.

Bonus: mailcatcher even has an API so you can use it in your integration test environment to verify that certain actions in your application caused certain emails to be sent.

Symfony2 Impressions

violin photo
Photo by mitch98000

Recently, I had a short engagement for a client who had an existing application written in Symfony2.  I haven’t really touched the modern PHP frameworks (the most recent experience was CakePHP 1.2, which was last released almost 3 years ago).  It was a pleasant surprise.

What was awesome about Symfony?

  • It forces you to think in terms of components (called bundles).  Even the core functionality is a bundle.
  • There are many bundles out there to let you get up to speed quickly.
  • It uses Composer, a dependency management tool much like Maven or NPM, to manage packages.
  • The documentation is extensive and versioned.
  • There is a clear product roadmap, including long term releases and clear deprecation dates
  • It has built in integration testing functionality, which lets you test clicks and form submissions and search the DOM for expected results.
  • There is clear configuration support for different environments.
  • It uses an ORM which seems capable–I didn’t get to dive into this too much.

Of course, the proof is in the pudding, and I didn’t get a chance to live with this solution for more than a few weeks.  I don’t know how active the community is, though the google group seems relatively active.  I’m sure there are warts in Symfony2–searching for ‘symfony2 sucks’ turned up a few rants.  But, for a greenfield webapp project, I’d happily use Symfony2.

CakePHP Rating System

I was thinking about adding a rating system (1-5 stars) to a cakephp site I run, and was looking forward to it.  But then I remembered the cardinal rule of software development, especially in the internet age: check and see if someone has done it first.

And someone had. Updated 6/2/2105: The previous link no longer works.  If I had to, I’d try this plugin instead (but I haven’t).

The biggest issue I ran into is making sure that I added the javascript helper to every controller that uses the default layout.  If I didn’t do that, I got a blank white screen.  Other than that, smooth sailing.

Musings on php development as a career path

I was at a TedXBoulder preparty last night.  Ran into some really interesting folks–the usual tech folks, but also Charles, a high flying audio engineer (we’re talking Wembley stadium), Emily, a money manager bizdev lady ($30 million, minimum, please) and Donna, an engineer on leave from a big aerospace firm who is interested in entrepreneurialism.  Really looking forward to the talks on Saturday (tickets apparently still available).  I also ran into an old friend, roommate and colleague.

We chatted about a wide variety of topics, but one stuck out in my mind.  His brother is getting back into software development, and is starting out doing a lot of php.  Fair enough–it’s a great language, I’ve done a fair bit of it, and one can write good, maintainable, fase code with it.  But last night, we agreed that if you don’t want to be competing against, how do I say this politely, the lowest common denominator, it is wise to develop your software dev skills elsewhere, into one of three paths.  I thought it’d make a good blog post.  As I see it, the three options are

  • a compiled language–C#, Java, c, erlang: these tend to be used by large companies
  • a sexy dynamic language–Ruby, javascript (especially server side), groovy, python, clojure, lisp: my feeling is that these are more used by startups
  • particular packages in php–magento, drupal: these are often more configuration than coding, but can be customized to produce astonishingly powerful applications

The end goal, to be clear, is not to avoid php, but just to avoid competing against developers who are likely to undercut you.  For example, I knew of someone, in the US, who was doing contract php work for $18/hr a few years ago.  I just don’t think that’s someone with whom you want to be competing for business (I certainly don’t!).  Following one of the above career development paths will help you avoid that.  I personally have followed the first and third paths, with some dabbling in the second.

A survey of CDNs for use with Drupal

I have spent some time researching Content Delivery Networks (CDNs) and how they can integrate with Drupal.  Note that I have not yet implemented a CDN solution, so my experiences and opinion may change….  I will try to do a second post or update when we’ve actually rolled something out live.

Here are some criteria I’d use in selecting a drupal module for CDN management:

  • Do you need a CDN?  This is the key question, as a CDN can speed up your site, but introduces a layer of managment and expense that might not be worth the hassle.
  • Do you mind patching drupal core?  This might be a maintenance issue going forward.
  • Do you want to have just images on your CDN, or javascript and CSS as well?  What about video?
  • How contained within the drupal interface do you need your interactions with a CDN to be?  Are you comfortable using a third party tool sometimes?
  • Do you have an existing CDN to work with, or are you selecting a CDN from scratch?  Obviously, you have more flexibility in the second case.
  • Do you mind coding? Some of these modules seem like they are 75% of the solution, but you might need to write some code to finish things up.

There are a number of modules that attempt to integrate a CDN into Drupal, or might help doing so.  All of these had a release for Drupal6.

  • CDN: this seems like a great fit.  Active development, good sized issue queue, support for multiple CDNs.  It also patches core. Here’s a list of CDNs used with this module.
  • media_mover: this module seems like it might be useful if you were needing to move image and or video files to a CDN.  That might require some coding, although I remember there being some S3 and FTP support.
  • creeper: this module is all about Amazon API integration, including CloudFront.  Plus, what a great name!
  • parallel: fairly new module that changes the source hostnames of images, css files and javascript html tags.  Therefore, they can be served off a CDN, or another web server, etc.
  • storage_api: this is a general storage service with a CDN focus, but doesn’t appear to be well documented or supported as of this time.
  • cloudfront: adds Amazon CloudFront support to the imagecache module

These all seem to be useful in their own ways.  The current project I’m working on is already invested in the Amazon infrastructure, mainly because of Project Mercury, so cloudfront is our current choice.

Did I miss any key modules?

[tags]drupal cms, cdns rock[/tags]

Using phpMyAdmin without the “Show Databases” privilege

phpMyAdmin is a pretty cool piece of software, and a very useful tool. If you haven’t used it before, it’s a full featured web-based interface to MySQL databases. You can use it to add data, update data, delete data, export data, and basically any other management of the database you might need.

I ran into an issue the other day. I was trying to allow phpMyAdmin to access a database on a remote host. The user that phpMyAdmin was connecting as didn’t have the “show databases” privilege (I imagine this is common in shared hosting environments, which is what this was). This, apparently, is what phpMyAdmin uses to populate the drop-down of databases on the left-hand side after you login. Since it didn’t display that drop-down, there is no way of selecting the database to which this user did have access.

I searched for a while, but couldn’t find anyone else with this problem. So I thought I would post the solution I found.

The solution is to hard code authentication data for the remote server in my config.inc.php file.  Then, you append the server and the database that you want to connect to the phpMyAdmin url.

In the config.inc.php file:
$cfg['Servers'][$i]['host'] = 'xxx.xxx.xx.xx';
$cfg['Servers'][$i]['user'] = 'user';
$cfg['Servers'][$i]['password'] = 'pass';

In the url that normal users use to access the database:
http://phpMyAdmin.example.com/phpMyAdmin/index.php?db=databasename&server=xxx.xxx.xxx.xxx

The left hand side still isn’t populated with any databases. But, this allows you to go directly to the database to which you do have access, and perform whatever tasks you want (and have permissions for). I tried adding the “db” parameter to the config.inc.php file, but that didn’t seem to work. I didn’t try using any other form of authentication than hardcoded, as I wanted to make this as simple as possible for the phpMyAdmin users.

I was running an older version of phpMyAdmin (2.11.9.5); I didn’t test this on newer versions.  If you do, please post a comment and let me know if this still works.

[tags]phpmyadmin,mysql privileges,remote database access[/tags]

The Drupal Experience

AKA, my time with the blue droplet.  I recently built a website for a client.  I initially recommended wordpress, as I often do but the client suggested the website would grow into an application.  You can certainly do webapps with wordpress, but it seemed worthwhile to look at alternatives.  Drupal 6 seemed to fit the bill:

  • flexible
  • lots of documentation (always important in any evaluation)
  • great community
  • tons of additional functionality (called plugins)
  • customizable UI
  • multiple languages supported

So, the site is launching soon and I thought I’d jot down my thoughts.  Keep in mind that I’m very much a drupal newbie.

The good:

  • I was astonished at the plethora of modules.  In fact, just about everything we needed to do functionally was done; it was a matter of getting the right modules installed and configured.  I don’t think I wrote a single line of code, though I did do plenty of code reading.
  • The look and feel were very customizable.  I ended up using a large number of blocks (isolated chunks of content that you can control and place).  The ‘clean’ theme was pretty easy to customize via CSS.  The Web Developer add on for Firefox was invaluable for this process–just hitting ‘control-shift-C’ and mousing over a component let me know what selector to use.
  • WYSIWYG support was pretty good, including image uploading (as a separate module), control of which of the TinyMCE buttons to display, and a dropdown to surround text with custom css class names.  I ended up using the HTML Purifier to scrub input from admin users.
  • The webform module allows non technical users to create complicated forms quite easily.  I didn’t really push this module, but it appears to have support for multi page wizards and other complex stuff.  But just using it for validation of required fields saved a lot of effort.
  • Drupal6 has a robust upgrade mechanism.  At least, it sure seems like it–I have upgraded modules and the core functionality a few times and haven’t had any issues.

My issues:

  • We started off on a medium strength server, and were not caching much, and performance was a big issue.  There are a number of resources out there, and some research is worth your while.  Drupal is a big, complicated system, and like all big complicated systems, tuning is a continual process.  Luckily, there’s a fair bit of built in caching that can be enabled with a checkbox.
  • Deploying from one server to another is an issue.  I talked to a friend who does a lot of Drupal development and he mentioned it was a real thorn in the side of drupal.  There’s just not that much available supporting the ability to move code from staging to production and content the other way.  To be fair, this is an issue for most CMSes, and there are some project (deploy is one of them) attacking this issue.  I don’t know of any open source CMSes that solve this problem entirely.
  • The SEO components seemed pretty good, but I was surprised that they weren’t bundled with the base Drupal installation.  You have to install a module to enable meta tags.  And another one for page titles.  Page titles!  (I looked at but was quickly overwhelmed by installation profiles, which might obviate some of the module installation.  There didn’t seem to be much support for Drupal 6 profiles.)
  • The complexity of Drupal, which allows it to do so much, means that there’s a lot to learn.  It can be a bit overwhelming at times.

The long and short of it:
If you’re looking to build a web application (not just a site), have some php expertise and some time to get up to speed, and need a lot of functionality, Drupal is worth looking at.

[tags]cms, php cms, drupal, blue droplets keep falling on my head[/tags]

Setting headers in a PHP include directive

I am currently working on a project that uses a PHP CMS for the bulk of the site. However, it pulls some content from a page generated by JSTL on a tomcat. I was using the fmt tag to do some pretty printing of numbers, and it looked fine when I viewed the content as served by tomcat. However, none of the formatting was carrying through when the PHP page included the jsp. Why? It appears that the fmt tag needs a locale. I confirmed that with wget.

Now, how to set the header with the PHP include. The include man page was no help, and mod_rewrite won’t let you modify headers, as far as I can tell. Once the request arrived to tomcat, that was too late.

Some digging around in the PHP documentation on fopen turned up this gem:

Custom headers may be sent with an HTTP request prior to version 5 by taking advantage of a side-effect in the handling of the user_agent INI setting. Set user_agent to any valid string (such as the default PHP/version setting) followed by a carriage-return/line-feed pair and any additional headers. This method works in PHP 4 and all later versions.

Click through for a code example. The aforementioned documentation talks about custom headers, but it worked for sending the Accept-Language header that the fmt tags needed.

PHP form generation

I just wanted to say: if you are building an application in PHP and you need to edit or search data from a relational database, HTML_QuickForm, DB_DataObject, and, occasionally, DB_DataObject_FormBuilder, can be very useful for prototyping and, depending on your client’s needs, building.  The tools are well worth a look if you’re planning to write any custom PHP database manipulation code.