Skip to content

Instantly share code, notes, and snippets.

@rlandas
rlandas / gist:4664243
Created January 29, 2013 13:30
ZF2: I need a universal method to initialize various elements (before action), without duplicating my code in every other controller action. Settings were just an example. Reference: http://zend-framework-community.634137.n4.nabble.com/ZF2-Doing-something-before-calling-the-controller-action-td4656922.html#a4656934
For universal solution you may want to try this one, it will work for all Controllers within single Module:
class Module
{
// Evan's changing layout for specific module only
public function init(ModuleManager $moduleManager) {
$sharedEvents = $moduleManager->getEventManager()->getSharedManager();
$sharedEvents->attach(__NAMESPACE__, 'dispatch', function($e) {
$controller = $e->getTarget();
// do your stuff with controller
@rlandas
rlandas / gist:4368879
Created December 24, 2012 11:23
GIT: Commit from one repository to another repository
git clone https://github.com/cakephp/cakephp
git push [email protected]:mg/cakephp.git master
@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}
(([^\s]+)?(\.(?i)(jpg|png|gif|bmp|git|phar|txt|json))$)
@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();
@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: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: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: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: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
*