Skip to content

Instantly share code, notes, and snippets.

@rlandas
rlandas / routes.php
Created October 11, 2012 12:03 — forked from robertbasic/routes.php
ZF2 Nice and clean routes with Part
<?php
$slugConstraints = '[\w_-]+';
$pagenumberChild = array(
'type' => 'segment',
'options' => array(
'route' => '/page/:page',
'constraints' => array(
'page' => '\d+'
@rlandas
rlandas / gist:4091376
Created November 16, 2012 22:15
GIT: Reset and delete all uncommitted files and pull the latest from the repository
git reset --hard HEAD;
git clean -f -d;
git pull;
@rlandas
rlandas / gist:4091394
Created November 16, 2012 22:18
ZF2: Extract (get) the module namespace from the requestedName in AbstractFactory class
namespace MyModule\Factory;
use Zend\ServiceManager\AbstractFactoryInterface;
class SomeClassFactory implements AbstractFactoryInterface {
// ... some other code
/**
* Determine if we can create a service with name
*
@rlandas
rlandas / gist:4108893
Created November 19, 2012 04:08
ZF2: how to check if a view template exists from within a controller
$template = 'non/existant/template';
$resolver = $this->getEvent()
->getApplication()
->getServiceManager()
->get('Zend\View\Resolver\TemplatePathStack');
if (false === $resolver->resolve($template)) {
// does not exist
}
@rlandas
rlandas / gist:4108895
Created November 19, 2012 04:09
ZF2: how to check if a view template exists, if in another view script
<?php if ($this->resolver('layouts/default')) : ?>
<?php $this->render('layouts/default'); ?>
<?php endif; ?>
@rlandas
rlandas / gist:4128250
Created November 21, 2012 22:22
ZF2: How to using DB transaction with zf2
public function add($data) {
try {
/**
* @var \Zend\Db\Adapter\Driver\Pdo\Connection
*/
$connection = $this->itemTable->adapter->getDriver()->getConnection();
$connection->beginTransaction();
$connection->commit();
@rlandas
rlandas / gist:4128254
Created November 21, 2012 22:23
ZF2: How to use Imagick with zf2
// @see http://giaule.com/2012/11/18/how-to-use-imagick-with-zf2/
public function uploadAction() {
$request = $this->getRequest();
if ($request->isPost()) {
$adapter = new \Zend\File\Transfer\Adapter\Http();
$adapter->addValidator('Size', false, array('min' => UPLOAD_IMAGE_MIN_SIZE, 'max' => UPLOAD_IMAGE_MAX_SIZE));
$adapter->addValidator('MimeType', true, array('image/jpeg'));
$adapter->addValidator('Count', false, array('min' =>1, 'max' => 1));
@rlandas
rlandas / gist:4191276
Created December 2, 2012 22:09
ZF2: Assigning variables to the controller using events
// @see http://samsonasik.wordpress.com/
public function onBootstrap(MvcEvent $e)
{
$sharedEvents = $e->getApplication()->getEventManager()->getSharedManager();
$sm = $e->getApplication()->getServiceManager();
$sharedEvents->attach('Zend\Mvc\Controller\AbstractActionController','dispatch',
function($e) use ($sm) {
$controller = $e->getTarget();
(([^\s]+)?(\.(?i)(jpg|png|gif|bmp|git|phar|txt|json))$)
@rlandas
rlandas / gist:4357674
Created December 22, 2012 05:47
ZF2: Create module directory structure
#!/bin/bash
read -e -p "Enter ZF2 module name: " moduleName;
moduleNameLowercase=$(echo $moduleName | tr '[A-Z]' '[a-z]');
mkdir -pv ${moduleName}/{config,src/${moduleName}/{Controller/Plugin,Entity,Filter,Form,Mapper,Service,Validator,View/Helper},view/${moduleNameLowercase}/index}