Skip to content

Instantly share code, notes, and snippets.

View coreymcmahon's full-sized avatar

Corey McMahon coreymcmahon

View GitHub Profile
@coreymcmahon
coreymcmahon / start.php
Created May 22, 2014 06:20
Documented approach for defining environments in Laravel - http://www.slashnode.com
<?php
// ... etc
// in bootstrap/start.php
$env = $app->detectEnvironment(array(
'local' => array('localhost'),
'staging' => array('staging.domain.org'),
'production' => array('domain.org'),
));
@coreymcmahon
coreymcmahon / routes.php
Created March 25, 2014 03:07
Using macros to implement custom response types in Laravel - www.slashnode.com
<?php
Response::macro('csv', function($data, $filename = 'data.csv', $status = 200, $delimiter = "|", $linebreak = "\n", $headers = array())
{
return Response::stream(function () use ($data, $delimiter, $linebreak) {
foreach ($data as $row) {
$keys = array(); $values = array();
$i = (isset($i)) ? $i+1 : 0;
foreach ($row as $k => $v) {
if (!$i) $keys[] = is_string($k) ? '"' . str_replace('"', '""', $k) . '"' : $k;
@coreymcmahon
coreymcmahon / UsersController.php
Created March 25, 2014 03:03
Using the custom response facade - www.slashnode.com
<?php namespace Acme\Controllers;
use Acme\Repositories\UserRepositoryInterface as UserRepository;
class UsersController extends \BaseController
{
protected $userRepository;
public function __construct(UserRepository $userRepository)
{
@coreymcmahon
coreymcmahon / app.php
Created March 25, 2014 02:55
Registering a custom Response facade - www.slashnode.com
<?php
// ...etc
/* comment out the standard response registration */
//'Response' => 'Illuminate\Support\Facades\Response',
/* add our new one...*/
'Response' => 'Acme\Extensions\Facades\Response',
@coreymcmahon
coreymcmahon / responses.php
Created March 25, 2014 02:46
Examples of different response types that can be sent back using the Response facade in Laravel 4 - www.slashnode.com
<?php
// send back some HTML, rendered from a template
return Response::view('users.index', array(
'name' => 'Steve Brule',
'title' => 'Brule\'s Rules',
));
// send back some json
return Response::json(array(
@coreymcmahon
coreymcmahon / Response.php
Created March 25, 2014 02:36
Custom Response facade for additional response types in Laravel 4
<?php namespace Acme\Extensions\Facades;
class Response extends \Illuminate\Support\Facades\Response
{
public static function csv($data, $filename = 'data.csv', $status = 200, $delimiter = "|", $linebreak = "\n", $headers = array())
{
return static::stream(function () use ($data, $delimiter, $linebreak) {
foreach ($data as $row) {
$keys = array(); $values = array();
<?php
/**
* function to check whether a date is within the last 30 days
*/
function withinOneMonth ($checkDate) {
$now = new DateTime();
$thirtyDayInterval = new DateInterval('P30D');
@coreymcmahon
coreymcmahon / DateTimeExample.php
Created March 7, 2014 07:03
DateTime example
<?php
/* 00:00am (midnight) in Vietnam */
$datetimeOne = new DateTime('2014-03-07 00:00:00+07:00');
/* 04:00am in Australia */
$datetimeTwo = new DateTime('2014-03-07 04:00:00+11:00');
echo ($datetimeOne == $datetimeTwo) ? 'equal' : 'not equal';
// prints 'equal'
<?php
// etc...
// find the payment if it already exists
$payment = Payment::where('txn_id', '=', Input::get('txn_id'))->get()->first();
// if it doesn't exist, create the payment using all the fields in the POST
if (empty($payment)) $payment = Payment::create(Input::all());
// if in debug mode, add the raw body of the request and also save it
@coreymcmahon
coreymcmahon / example.php
Created February 20, 2014 08:09
Use a repository to update an object
<?php
// resolve the dependency
$repo = App::make('Acme\Repositories\ObjectRepository');
// update the entity
$repo->update($object->id, array(
'attribute' => 'new value',
));