Skip to content

Instantly share code, notes, and snippets.

@robap
robap / html_vs_json
Last active December 12, 2015 08:58
The difference between partial html ajax and json
//partial html can simply be inserted into the dom
$('.ajax-content').html(data);
//json needs to be mapped
$('#form_id').val(data['id']);
$('#form_firstName').val(data['firstName']);
$('#form_lastName').val(data['lastName']);
$('#form_phone').val(data['phone']);
$('#form_street').val(data['street']);
$('#form_city').val(data['city']);
@robap
robap / mydispatcher.php
Created March 23, 2012 13:01
alternative php-router dispatcher
<?php
class myDispatcher extends Dispatcher
{
protected function dispatchController($class, $method, $args, $context = null)
{
$obj = new $class($context);
return call_user_func(array($obj, $method), array_values($args));
}
}
@robap
robap / gist:1592498
Created January 11, 2012 01:57
Using psr-0 compliant autoloader
<?php
//File: test.php
//Create the file SplClassLoader.php and add the contents of this gist https://gist.github.com/221634
//Example:
//Class Foo\Bar located in 'src/Foo/Bar.php'
require 'SplClassLoader.php';
$autoloader = new SplClassLoader('Foo', 'src');
@robap
robap / app.php
Created January 8, 2012 23:25
My Symfony project web/app.php file
<?php
require_once __DIR__.'/../app/bootstrap.php.cache';
require_once __DIR__.'/../app/AppKernel.php';
use Symfony\Component\HttpFoundation\Request;
$mode = (getenv('APPLICATION_ENV')) ? getenv('APPLICATION_ENV') : 'dev';
umask(0000);
@robap
robap / gist:1578936
Created January 8, 2012 16:43
apache virtual host file for symfony2 project on dev system
<VirtualHost *:80>
ServerAdmin webmaster@localhost
ServerName {HOST}
DocumentRoot /usr/development/{projName}/web
<Directory />
Options FollowSymLinks
AllowOverride All
</Directory>
<Directory /usr/development/{ProjName}/web>
@robap
robap / config.nice
Created December 4, 2011 01:59
php configure
#! /bin/sh
#
# Created by configure
'./configure' \
'--with-curl' \
'--with-pdo-mysql=shared' \
'--enable-mbstring' \
"$@"
@robap
robap / object_id_php_traits.php
Created November 20, 2011 20:30
Adding Object ID to objects using php5.4 traits
<?php
trait ObjectId
{
public function getObjectId()
{
if(!isset($this->__objectId__)) {
$this->__objectId__ = uniqid();
}
return $this->__objectId__;
@robap
robap / autoloader_usage
Created November 6, 2011 14:39
Autoloader usage
<?php
//Unfortunately, AutoLoader can't auto load itself
require '/path/to/AutoLoader.php';
$auto_loader = new AutoLoader;
$auto_loader->registerDirectory('lib1/');
$auto_loader->registerDirectory('lib2/');
$foo = new Foo;
$bar = new Bar;
@robap
robap / php_class_autoloading.php
Created November 6, 2011 14:35
Better php class autoloading
<?php
function load_one( $class_name )
{
$file = 'lib1/' . $class_name . '.php';
if( file_exists($file) )
{
require_once $file;
}
@robap
robap / more_wrong_php_autoload.php
Created November 6, 2011 14:33
Even more wrong php class autoload
<?php
function __autoload( $class_name )
{
$file = $class_name . '.php';
$dirs = array( 'lib1/', 'lib2/');
foreach( $dirs as $dir )
{
if( file_exists($dir . $file) )
{