Skip to content

Instantly share code, notes, and snippets.

View thewinterwind's full-sized avatar

Anthony Vipond thewinterwind

View GitHub Profile
@thewinterwind
thewinterwind / gzip-ajax-response.php
Last active July 26, 2019 09:50
How to gzip AJAX response with PHP (Laravel 4 flavor)
<?php
public function all()
{
if (Request::ajax()) {
$encoded_html = gzencode(Model::all(), 9);
header('Content-Length: ' . strlen($encoded_html));
header('Content-Encoding: gzip');
return $encoded_html;
}
@thewinterwind
thewinterwind / disable_multiple_tabs.js
Created April 29, 2017 03:49
Detect website being accessed in a new tab
<script>
// helper function to set cookies
function setCookie(cname, cvalue, seconds) {
var d = new Date();
d.setTime(d.getTime() + (seconds * 1000));
var expires = "expires="+ d.toUTCString();
document.cookie = cname + "=" + cvalue + ";" + expires + ";path=/";
}
// helper function to get a cookie
0xC27CF0B05c7468764e9f57e84B881821D145CE0d
@thewinterwind
thewinterwind / repo-proposal.md
Last active June 27, 2017 19:42
Proposal for service platform and repository pattern

Service Platform w/ Repository Pattern Proposal

This document will outline a proposal for a service platform (API) and a repository pattern as a business layer for IC.

Benefits

  • Have a data layer that is independent of models and controllers.
  • Have repo methods that can be consumed from other parts of the web application and from the command line.
  • Reduce logic in controllers and models, allowing controllers to focus on HTTP level work (request data, cookies, responses) and models to focus on creating relationships, casting columns, mutators, etc. The controllers should instead delegate work to the repos.
  • Repository methods can be reused by different parts of the web application and to perform work for an API request coming from outside (i.e. if we use a mobile platform like React Native it can call the API endpoints which in turn consume the repositories)
  • Have repo methods be easily unit tested as they will not be accessing session data, request data or external constants. They only
@thewinterwind
thewinterwind / insert_many_rows.php
Last active December 12, 2016 13:59
How to insert millions of rows to the database with PHP (YT Tutorial)
<?php
class Stock {
public function store_stock_history()
{
$date = date('Y-m-d');
$files = File::files(app_path() . '/resources/historical_lists/' . $date);
@thewinterwind
thewinterwind / validate_url.js
Created October 17, 2013 07:12
Validating a URL with Javascript
// I didn't write this function, only modified it slightly. It works well though!
function valid_url(str) {
var pattern = new RegExp('^(https?:\\/\\/)?'+ // protocol
'((([a-z\\d]([a-z\\d-]*[a-z\\d])*)\\.)+[a-z]{2,}|'+ // domain name
'((\\d{1,3}\\.){3}\\d{1,3}))'+ // OR ip (v4) address
'(\\:\\d+)?(\\/[-a-z\\d%_.~+]*)*'+ // port and path
'(\\?[;&a-z\\d%_.~+=-]*)?'+ // query string
'(\\#[-a-z\\d_]*)?$','i'); // fragment locator
@thewinterwind
thewinterwind / StoringController.php
Last active June 13, 2016 13:56
Code to count streaks in PHP (used with Yahoo API tutorial)
<?php
class StoringController extends BaseController {
public function store_streaks()
{
$date = date('Y-m-d');
$stocks = DB::table('stocks')->select('symbol')->orderBy('symbol', 'asc')->get();
@thewinterwind
thewinterwind / php-yahoo-finance.php
Created June 21, 2014 14:38
Lesson 8 - PHP & Yahoo Finance API
<?php namespace SS\Stock;
use DB, File, Cache;
class Stock {
public function __construct()
{
ini_set("memory_limit", "-1");
set_time_limit(0);
@thewinterwind
thewinterwind / Stock.php
Created June 23, 2014 11:02
Entire Stock class for tutorial on using PHP with the Yahoo Finance API
<?php namespace SS\Stock;
use DB, File, Cache, Input, Response;
class Stock {
public function __construct()
{
ini_set("memory_limit", "-1");
set_time_limit(0);
@thewinterwind
thewinterwind / phpunit.xml
Created January 28, 2014 05:14
Add colors to output for PHPUnit
<?xml version="1.0" encoding="UTF-8"?>
<phpunit colors="true"
processIsolation="true"
verbose="true"
debug="true">
</phpunit>