Skip to content

Instantly share code, notes, and snippets.

View coreymcmahon's full-sized avatar

Corey McMahon coreymcmahon

View GitHub Profile
@coreymcmahon
coreymcmahon / UserType.php
Created May 7, 2012 13:24
A UserType class used to implement a form for the User entity - http://www.symfonycentral.com/symfony-does-validation.html
<?php
namespace MyCompany\BlogBundle\Form;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilder;
class UserType extends AbstractType
{
public function buildForm(FormBuilder $builder, array $options)
<?php
namespace MyCompany\BlogBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/* Include the required validators */
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
use Symfony\Component\Validator\Constraints as Assert;
<h1>Users</h1>
{% if users|length == 0 %}
{# No users to display... #}
<p>Could not find any users :(</p>
{% else %}
<table>
<thead>
<td>Username</td>
<td>Email address</td>
<td>Password</td>
@coreymcmahon
coreymcmahon / create.html.twig
Created May 7, 2012 13:29
The view code for displaying the form fields, errors, etc. Yep! That easy! - http://www.symfonycentral.com/symfony-does-validation.html
<form action="{{ path('user_create') }}" method="post" {{ form_enctype(form) }}>
{{ form_widget(form) }}
<input type="submit" />
</form>
<?php
namespace MyCompany\BlogBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
// Add this so that we load the UserInterface class
use Symfony\Component\Security\Core\User\UserInterface;
class User implements UserInterface // Make sure we implement UserInterface
<?php
namespace MyCompany\BlogBundle\Entity;
use Doctrine\ORM\EntityRepository;
// Make sure we include UserProviderInterface
use Symfony\Component\Security\Core\User\UserProviderInterface;
use Symfony\Component\Security\Core\Exception\UsernameNotFoundException;
use Symfony\Component\Security\Core\Exception\UnsupportedUserException;
<?php
interface UserInterface
{
/**
* Returns an array of strings, where each string is a role that this user has.
*
* @return Role[] An array containing the roles this user has
*/
function getRoles();
<?php
interface UserProviderInterface
{
/**
* Loads the user object for the given username.
*
* @param string $username The username
* @return UserInterface
* @throws UsernameNotFoundException If the user is not found
*/
@coreymcmahon
coreymcmahon / composer.json
Created June 2, 2012 02:39
Example Composer manifest file for our framework, Simplex - http://www.symfonycentral.com
{
"require": {
"symfony/class-loader": "2.1.*"
}
}
@coreymcmahon
coreymcmahon / http-example.php
Created June 2, 2012 02:59
Using the Symfony 2 HTTP Foundation component - http://www.symfonycentral.com
<?php
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
/* Using the HTTP Foundation class */
$request = Request::createFromGlobals();
$input = $request->get('name', 'World');
$response = new Response('Hello ' . $input);
$response->send();