PHP

PHP Frameworks get weird

I was getting the itch to write a small web application and started looking around. I had played with Laravel a while back, but it's just a little too large. I wanted to deploy cheaply on my slow server.

The features I wanted were: REST API support, OAuth (client), Routes, User Auth

Phalcon was the most interesting. It's written in C as an extension, and is fast. It does mostly traditional, Laravel style MVC. It can do REST APIs as well, but the example isn't good. It needs some more code to do REST right. Being compiled it is fast. The features resemble Laravel and Twig.

WordPress Multisite Problems with Vagrant

I had some problems setting up Multisite on WP with the Vagrant environment.

The big issue is that the the domains and URLs need to be set just right. This is a little more complicated than a typical server setup because dev setups usually take shortcuts on DNS.

You need to fix the domain names in all the locations where they're resolved.

First, set up DNS. Set up domain names for all the domains you're going to develop, but use local names like "foobar.lo" instead of "foobar.com". They can all point to the IP address your dev server exposes via Vagrant, usually 192.168.33.10.

A Small Library to Change WordPress Options (useful for E2E WP Testing with Selenium)

I was messing with E2E testing of WordPress, and it was going okay, but I hit a wall when I needed to mess with one of the options, to turn of a CAPTCHA. You don't want to use deal with CAPTCHAs during a test, at least not every test. There may be some where you want to see the CAPTCHA.

The right way to do this is to flip options on and off.

The TDD/BDD Religion, Getting The

I must admit that I'm fully drunk on the Kool-Ade.

The tl;dr : testing isn't just writing tests, but also using mock objects and services to simplify testing, and using package managers to port code to new versions of modules. It takes days to learn how, but it's worth it.

Shorter code with functional-style programming in PHP.

So, I'm doing wordpress programming, and one of the headaches is that they store serialized objects in the database with the update_option() and related functions.

This makes querying some data difficult. Here's an example of the data structure I was dealing with (it stores sidebar settings).

Array
(
    [wp_inactive_widgets] => Array
        (
        )

    [sidebar-1] => Array
        (
            [0] => search-2
            [1] => recent-posts-2
            [2] => recent-comments-2

WordPress Unit Tests in the Main Directory

I'm hacking some of the original WP files. Not the smartest thing to do, but the options are to replace the functions, or alter them, because a plugin isn't going to work.

So, I needed to run tests in the main program. Here's the quck howto.

Install wp-cli and get it working.

Then, go into your wp root and type:

wp scaffold plugin-tests --dir=.

Then run the installer script:

bin/install-wp-tests.sh wordpress_test root '--password--'

WordPress wp-signup headaches, and refactoring.

I've been modifying the wp-signup script a lot, and figured out a better pattern for doing these screens. This technique works more like "views and controllers", so it's one step closer to MVC.

WP doesn't have routes, but the wp-signup script has a paramter named "stage" that specifies the next state. It's effectively a route.

There are two types of stages: showing a form, or validating a form.

When you validate a form, you see if there are errors. If there are, you show the form, with error messages. If there are no errors, you commit the changes, and then show the next form.

What is Dependency Injection?

I've answered this a couple times so I'm writing it down :)

DI is a way to separate services from the code that uses those services.

DI makes testing easier. It also allows the services to be replaced with alternatives - but this is less important than the testing.

DI is used in Angular, Zend Framework 2, Symfony and more frameworks.

DI is implemented as an array or dictionary of services with generic names. Services are created during startup, and registered.

CORS, Angular JS, and Parse.com, together, didn't let me login twice.

Things were going well with a re-architecting and re-factoring of a service to use Angular's awesome $request, and Django REST Frameworks' awesome ModelViewSet generics. As usual, when things are chugging along, you come across a weird bug that just sucks you in for a while. The bug I hit today involved CORS, AngularJS, and Parse (we're using Parse for part of our backend).

The symptom was that, if I logged in once, then logged out, I could not log in again. I could reach the server, but it wouldn't let me do the exact same thing I'd done just 30 seconds before.

Instant Cross-Domain Access for Everyone with CORS in PHP

Here's a snippet of code that will make your PHP REST API work cross-domain, through the magic of CORS:

/**
 * CORS is a way to allow scripts from other domains to post to this URL.
 */
if ($_SERVER['REQUEST_METHOD']=='OPTIONS') {
    header('Access-Control-Allow-Origin: *');
    header('Access-Control-Allow-Methods: POST, OPTIONS');
    header('Access-Control-Allow-Headers: Content-Type');
    exit();
}
header('Access-Control-Allow-Origin: *');

Model PHP Script Example: writing relatively safe web forms

I just whipped this script up to demonstrate some techniques for writing relatively safe web forms with PHP. This example doesn't strive to look nice or even be easy to understand. It should be a jumping off point for learning some PDO, some htmlspecialchars, some filter_var, and a functional style of composing pages. The last thing - not such a great idea, but it works for really short pages.

This script is written to try and avoid SQL injection attacks, and cross site scripting (XSS).

Code is attached, and below.

WordPress: Setting the META Description to part of the page's content

This code puts the first paragraph of the post into the description meta tag. It tries to strip out leading whitespace and any tags. If you insert an image, it should be stripped.

Class Autoloading, Namespaces, PSR-0 and Acting Like Java

One of the surprises of ZF2 is that the preamble to the code looks like this:

namespace Album\Model;

use Zend\InputFilter\Factory as InputFactory;
use Zend\InputFilter\InputFilter;
use Zend\InputFilter\InputFilterAwareInterface;
use Zend\InputFilter\InputFilterInterface;

That's an awful lot like Java:

package illustration;

import java.awt.*;

The slight difference is that "namespace" and "use" are more like Perl lingo. "Package" and "import" hint at the existence of a compiler and linker.

RIP Slaptech Framework

Slaptech framework was a PHP 4 based framework that never got released into the public, that we built to do projects.

How to use Tags or Ctags to Browse Code with Vim

This is a short tutorial about using ctags (or exuberant ctags) to work with the vim editor to give you a great code browser. Vi and it's successor Vim have a built-in feature to allow you to jump from a use of a function call to the definition of that function call. It's a good way to learn about a library of code: load up some example code, and then read it, jumping from the function calls to the definitions.

To use tags, you must first install ctags or exuberant-ctags. They are in the repositories. To run it, go to the root of your source tree and type:

ctags -R *
Syndicate content