View Composer Paths in Laravel 4

November 2nd, 2013

I just ran into this, and thought I’d share a quick tip for avoiding unbound data errors when using view composers in Laravel 4.

Currently, my view hierarchy looks something like this

views
    layouts
        master.blade.php
    events
        index.blade.php 

Currently, I have my view in events/index.blade.php extending layouts/master.blade.php.

Because it’s in a folder, I need to traverse up to the master layout like so:

@extends('../layouts.master')

I’m also using a view composer to bind data to the master layout on every request. I’ve decided to simply put it in my app/filters.php file, but you can put it anywhere as long as it gets autoloaded. Here’s my view composer:

View::composer('../layouts.master', function($view) {
  $count = 0;
  if(Auth::check()){
    $count = Evnt::where('user_id', '=', Auth::user()->id)->count();
  }
  $view->with('event_count', $count);
});    

Pretty basic, we’re finding all the events that belong to the currently authenticated user and returning the count.

The gotcha, that I ran into was, I orignally passed the ../layouts.master portion without the ../, which resulted in the view composer not being fired.

The moral here, is if you are using a layout and writing views that extend the layout, you must pass the view path in your view composer just as your views extend it.

Fortunately, in case we have some crazy view directory structures going on we can pass multiple view paths through the composer.

View::composer(array('../layouts.master', 'layouts.master', '../../layouts.master'), function($view) {});    

Hopefully this helps anyone who runs in to the same problem I did. Further question? I’m @syropian on Twitter.

A Fix For Slow Sendmail on Ubuntu

April 15th, 2013 • 1 note

I currently host a majority of my websites on a DigitalOcean VPS. For the most part it’s been a great experience, and easy on my wallet. One thing I noticed however, was trying to use PHP’s sendmail function.

If you just install it via apt-get, you’ll likely notice that sending mail is painfully slow. There is a quick fix for this fortunately, and I’m going to show you how. First of all, if you don’t have sendmail installed, you’ll want to do that. Pretty easy, once you’re SSH’d into your server, execute the following command:

apt-get install sendmail

Great, we now have the sendmail module installed. Now, we need to configure our hosts file, just to make sure our entry is correct. If you don’t know your host name, just run the command hostname, and it will return it. Now we need to edit our hosts file. The quickest way is to use a command line text editor like nano, so let’s do that.

nano /etc/hosts

There should be an entry for your localhost. Edit the line so it resembles this, but using your own hostname.

127.0.0.1       localhost localhost.localdomain yourhostnamehere

Press Control+o, then enter, and finally Control+x. Now issue the following command and answer ‘Y’ to everything:

sendmail config

And for good measure, restart apache:

service apache2 restart

After that, your sendmail function should work much faster. Questions or comments? I’m @syropian on Twitter and App.Net

Hello Kirby →

February 15th, 2013 • 1 note

A short write up of my portfolio site’s new architecture.

Pay Collin!

February 10th, 2013 • 1 note

Pay Collin!

When Stripe finally came to Canada I was so very excited. Excited at the notion of dropping PayPal forever because they’re shady, ugly, and generally sucky.

I finally decided to build a tiny little web page to allow anyone to pay me an amount, powered by Stripe. It’s written in plain ‘ol PHP, HTML, CSS, and jQuery, and hosted on Heroku. Go ahead and check it out on your desktop or mobile device. Be warned, if you fill it out with your actual credit card info, it will charge you. You’ve been warned.

Laravel 4 Beta →

January 15th, 2013 • 2 notes

Laravel, my prefered PHP framework has hit v4 (in beta). Go check it out!