Skip to content

Instantly share code, notes, and snippets.

View weaverryan's full-sized avatar

Ryan Weaver weaverryan

View GitHub Profile
@weaverryan
weaverryan / array-rand-stuffs.php
Created December 7, 2011 15:37
array rand if statements
<?php
if (count($ids) == 0) {
return array();
}
if (count($ids) < $ct) {
// careful not to request more random numbers than we have in array_rand, it will throw up
$randomIndexes = array_keys($ids);
} else {
@weaverryan
weaverryan / config.yml
Created December 14, 2011 21:19
Propel with sqlite
dbal:
driver: sqlite
dsn: sqlite:/temp/abc.db
options: {}
attributes: {}

Redirecting to last visited page after registration.

Imagine this scenario: A new user on your awesome webpage finds an awesome link, but it's only open to registered users, so he's automatically redirected to login form, then he goes to a register page (or fills embedded form right there) and then gets redirected back to a homepage. Somewhat frustrating experience, don't you think?

Well, let's solve this little problem with some help from [symfony2 authentication listener] (https://github.com/symfony/symfony/blob/master/src/Symfony/Component/Security/Http/Firewall/AbstractAuthenticationListener.php). More specifically, by exploiting [already existing feature] (https://github.com/symfony/symfony/blob/master/src/Symfony/Component/Security/Http/Firewall/AbstractAuthenticationListener.php#L270) for login form. This feature is actually a simple session parameter that is set the moment

@weaverryan
weaverryan / function.php
Created January 14, 2012 16:59
Collecting FormView errors into an array
<?php
function aggregateFormErrors(FormView $form, $errors = array())
{
$errors = array_merge($errors, $form->get('errors'));
foreach ($form as $field) {
$errors = self::aggregateFormErrors($field, $errors);
}
@weaverryan
weaverryan / Category.php
Created January 17, 2012 21:21
Recursive function
public function getNameWithSpaces()
{
$category = $this;
$level = 0;
while ($category = $this->getParent()) {
$level++;
}
// now make spaces based on the level
}
@weaverryan
weaverryan / security.yml
Created February 10, 2012 16:24
A helpful "works-for-most" security.yml file
security:
encoders:
# unless you just use the few hard-coded users below, you'll likely have your own User class
# if you do, this will change to the fully-qualified namespace to your class and you should use sha512
Symfony\Component\Security\Core\User\User: plaintext
# Acme\UserBundle\Entity\User: sha512
role_hierarchy:
ROLE_ADMIN: ROLE_USER
ROLE_SUPER_ADMIN: [ROLE_USER, ROLE_ADMIN, ROLE_ALLOWED_TO_SWITCH]
@weaverryan
weaverryan / forms.html.twig
Created April 13, 2012 11:26
Some basic Twitter Bootstrap Symfony2 form customizations
{# for twitter bootstrap specifically #}
{% block field_row %}
{% spaceless %}
<div class="control-group {% if errors|length > 0 %}error{% endif %}">
{{ form_label(form, label|default(null)) }}
<div class="controls">
{{ form_widget(form) }}
{{ form_errors(form)}}
</div>
</div>
@weaverryan
weaverryan / FeatureContext.php
Created June 19, 2012 21:05
Behat Definition to help click generic links on different rows of a table
<?php
// ...
class FeatureContext extends MinkContext
{
/**
* Looks for a table, then looks for a row that contains the given text.
* Once it finds the right row, it clicks a link in that row.
*
@weaverryan
weaverryan / FeatureContext.php
Created January 9, 2013 20:01
Using KernelAwareInterface
<?php
// ..
use Behat\Symfony2Extension\Context\KernelAwareInterface;
use Symfony\Component\HttpKernel\KernelInterface;
class FeatureContext extends MinkContext implements KernelAwareInterface
{
private $kernel;
@weaverryan
weaverryan / README.md
Last active December 15, 2015 08:09
Welcome to Symfony Hack Day March 30th, 2013

It's a Doc Hack Day!

Welcome! No matter what you're level of Symfony or how you feel about your English (I'm sure it's just fine), we have plenty of things we need help with! This document will guide you through everything:

Where are we meeting?

We're meeting on Freenode in the #symfony-docs channel. Come hang out!

What can I work on?