Skip to content

Instantly share code, notes, and snippets.

View martinbean's full-sized avatar

Martin Bean martinbean

View GitHub Profile
@martinbean
martinbean / macros.php
Created July 30, 2016 10:48
Laravel Request startsWith and endsWith macros
<?php
use Illuminate\Support\Facades\Request;
use Illuminate\Support\Str;
Request::macro('startsWith', function () {
return Str::startsWith(func_get_args());
});
Request::macro('endsWith', function () {
@martinbean
martinbean / VerifySnsMessageCameFromAmazon.php
Created July 8, 2016 21:01
Laravel 5.x middleware to verify an SNS request came from Amazon.
<?php
namespace App\Http\Middleware;
use Aws\Sns\Message;
use Aws\Sns\MessageValidator;
use Closure;
class VerifySnsMessageCameFromAmazon
{
@martinbean
martinbean / function.js
Created June 8, 2016 09:36
Lambda function to send Slack notification on deployment
'use strict';
const https = require('https');
const querystring = require('querystring');
String.prototype.capitalizeFirstLetter = function () {
return this.charAt(0).toUpperCase() + this.slice(1);
}
exports.handler = function (event, context) {
@martinbean
martinbean / replace-dots.js
Created June 1, 2016 14:37
Converts dotted notation (`some.dotted.string`) to HTML format (i.e. `some[dotted][string]`).
key.replace(/\.(\w+)/g, '[$1]');
@martinbean
martinbean / after.sh
Created January 22, 2016 11:29
Per-project Homestead after.sh and aliases
#!/bin/sh
cd 'project-dir'
composer install
php artisan migrate
php artisan db:seed
npm install --loglevel error
export DISABLE_NOTIFIER=true
gulp
@martinbean
martinbean / RepositoryMakeCommand.php
Last active January 19, 2016 14:58
Laravel repository make command.
<?php
namespace App\Console\Commands;
use Illuminate\Console\GeneratorCommand;
class RepositoryMakeCommand extends GeneratorCommand
{
/**
* The console command name.
@martinbean
martinbean / filtering.php
Last active December 28, 2015 05:29
Very crude way of building up raw SQL clauses for filtering and sorting.
<?php
// whitelist columns to filter by
$filterable = array(
'colour',
'size',
'price'
);
// build up string for WHERE clause
$where = array();
@martinbean
martinbean / NewsArticle.php
Last active December 10, 2015 07:28
Fetching records from one table based on existence of records in a HABTM relation.
<?php
class NewsArticle extends AppModel {
public $name = 'NewsArticle';
public $hasAndBelongsToMany = array(
'Image' => array(
'joinTable' => 'news_articles_images'
)
);
@martinbean
martinbean / gist:4162031
Created November 28, 2012 15:36
Martin’s fantastic naming convention document

Martin’s fantastic naming convention document

Controllers: plural

  • For example: UsersControllers

Models: singular

  • A model representes one item, i.e. a record from a database. So: User or UserModel.
@martinbean
martinbean / composer.json
Created November 23, 2012 22:38
Code samples for “Uploading files with Amazon’s new PHP SDK” blog post.
{
"require": {
"aws/aws-php-sdk": "dev-master"
}
}