Introducing Clipboard

The more I looked at the code I had put together from the multi-step tutorial it made me realize that what I really needed to do was start over. It also occurred to me that the interesting problem to solve isn’t how to build a multi-step survey, but instead forms and surveys that are configured and stored in the database. In hindsight the multi-step piece is more of a feature you’d want to sit on top of that.

So I started from scratch, and what I built is this thing I call Clipboard. The underlying data model is (IMHO) much improved, and does a better job of abstracting out the components of a survey. You can check out a copy over on Github. I was probably looking at Google Forms a little too closely for inspiration when I started trying to pretty it up…

screenshot-clipboard-bicknoyle c9users io 2016-02-29 21-31-01

It’s been a while since I hacked it out and I haven’t touched it since; my last commit was Nov. 10, 2015. Slacker! I thought it might eventually make for an interesting Laravel package, though admittedly don’t have any current plans to do so. But, it’s a thing I had fun building, and wanted to just say a quick blurb about it.

 

 

 

Read More

Dynamic Virtual Hosts with VirtualDocumentRoot

If you work on an Apache web server that has a lot of Virtuals Hosts (or vhosts), you can simplify your configuration using the VirtualDocumentRoot directive. It works by dynamically mapping all of or part of a hostname to a folder.

I find this most useful for subdomains. Suppose you own the domain example.com, and you want to map each subdomain to its own folder. A config to do this might look like…

(more…)

Read More

Building a Multi-Step Survey with Laravel 5.1: Part 2

After step one of the multi-step survey tutorial, we had a working web app, but it had a few limitations. For example, to add additional questions or steps you would have to add and edit multiple files: SurveyController, Blade templates, etc. It would turn into an error prone task because of the likelihood of forgetting to make an edit somewhere. Not to mention you’d have to modify controller code each time, which is less than ideal.

In this step of the tutorial, we rectify this limitation by implementing a database driven form. We still store all aspects of the form in the database, including field name, type, validation rules, and the question text.

(more…)

Read More

Reading and Writing CSVs Using League\Csv

PHP has built in functions, fgetcsv() and fputcsv(), that provide basic read/write functionality for CSV files. But a better alternative, that has additional functionality you may find useful, is League\Csv by the League of Extraordinary Packages. Installation using Composer is simple:

$ composer require league/php

The Basics

Once installed, opening a CSV and extracting data is no problem:

use League\Csv\Reader;

$reader = Reader::createFromPath('/path/to/myfile.csv');

// outputs the 7th line as an array
$row = $reader->fetchOne(7);

foreach ($reader->query() as $index => $row) {
    // do something
}

Creating and writing to CSV files is also a breeze:

use League\Csv\Writer;

// create a file
$writer = Writer::createFromFileObject(new SplFileObject('/path/to/myoutput.csv', 'w'));

// add a row
$writer->insertOne(['1', [email protected]', 'John Doe']);

// output file contents as table with class="table table-striped"
echo $writer->toHtml('table table-striped');

// output file to browser for download
$writer->output();

Those are the basics, but here are a few cookbook-style examples for specific tasks you might find useful.

(more…)

Read More

Building a Multi-Step Survey with Laravel 5.1: Part 1

How would you make a multi-step survey or form in Laravel? This question came up on the /r/Laravel subreddit recently. In my own head I couldn’t immediately come up with a good answer. Should you store the data for each step in a session or persist it to a database? How do you handle users that leave midway through and return later? What would the controller look like?

In an attempt to try to answers those questions, here’s a tutorial on how you might approach the task.

(more…)

Read More

Testing a Form with reCAPTCHA

Note: Google added security settings to reCAPTCHA that must be configured in order for the noscript fallback to display. Unfortunately it doesn’t look like they’ve enable noscript for the test key yet, so the guide below is broken at the moment. I maintain a Laravel package that depends on this for testing, so as soon as I’m able to determine a workaround for it, I’ll post an update.

It can be tricky to test forms of your web application if they are protected by Google’s reCAPTCHA service, but it is possible. This is a quick guide on how to do it in PHP, but the high level concept should work for any language.

(more…)

Read More