Autonomous Machine

Fitzgerald update: before filters and sendDownload

Fitzgerald has been running at least two websites for a little while now, but I've had no time to do much with it for the last month. There were a few feature additions as a result of a client site I was working one, and now's as good a time as any to mention them. Especially seeing as the official docs are a couple blogs posts for now.

Before Filters

One feature of Fitzgerald I added to the repo a while ago but failed to document is the ability to add before filters to actions. Anyone that's used an MVC web framework will probably be familiar with this concept; for those who aren't, the idea is to assign one or more methods to be called before a given action. The most common use case for this is probably ensuring that a use is logged in before viewing a given page.

It should be noted that adding after filter would be trivial; however, I'm not sure there really is a need for them in a framework of this size. So I've left them out for now.

Fitzgerald's implementation of filters is a little different than most in order allow a filter to return text to the browser or redirect. Any time a filter returns anything, the filter chain is halted and the result is sent to the browser.

Filters are defined using a similar syntax to binding actions to routes, and are executed in the order they are defined:

class ApplicationWithAFilter extends Fitzgerald {

    protected function verify_user() {
        // redirect unauthorized users to the home page
        if(!$this->isAllowed()) {
            return $this->redirect('/');

    protected function get_protected() {
        // display some secrets

    protected function post_protected() {
        // make some secrets


$app = new ApplicationWithAFilter();

// Define a before filter to be executed before one or more actions
$app->before('get_protected|post_protected', 'verify_user');


Just add a method with the correct name to your Application definition, and you are all set. I've updated example.php in the repo to include usage of a before filter.


sendDownload is really just a brute force version of sendFile that does everything it can to persuade a browser to download a file instead of opening it inline. There are no guarantees this technique will work all of the time, but in my testing, sendDownload contained the correct black magic to force Internet Explorer to behave itself.

Since I haven't documented either of these methods yet, here's how to use sendFile and sendDownload:

class ApplicationWithDownloads extends Fitzgerald {
    public function get_pdf() {
        $this->sendFile('filename_to_send_as.pdf', 'application/pdf', $this->path('pdf/awesome.pdf'));
    public function get_pdf_for_dowload() {
        $this->sendDownload('filename_to_send_as.pdf', $this->path('pdf/awesome.pdf'));

sendFile takes three arguments: the filename to send the file as, the content type to set in the headers, and the path to the local file to send. Note the call to path, which simply appends its argument to the app root (which is defined as one level up from fitzgerald.php). Usage of sendDownload is the same, except it doesn't take a content type argument.