Skip to content

Instantly share code, notes, and snippets.

View coreymcmahon's full-sized avatar

Corey McMahon coreymcmahon

View GitHub Profile
@coreymcmahon
coreymcmahon / routing.php
Created June 2, 2012 03:43
Using the Symfony 2 Routing component with an in-line Controller - http://www.symfonycentral.com
<?php
// ...
$routes->add('hello', new Routing\Route('/hello/{name}', array(
'name' => 'World',
'_controller' => function ($request) {
return render_template($request);
}
)));
@coreymcmahon
coreymcmahon / routing.php
Created June 2, 2012 03:28
Using the Symfony 2 Routing component to match URLs - http://www.symfonycentral.com
<?php
// ...
/* Define the routes for our application */
$routes = new Routing\RouteCollection();
$routes->add('hello', new Routing\Route('/hello/{name}', array('name' => 'World')));
$routes->add('bye', new Routing\Route('/bye'));
/* Set up the context and establish whether this request matches a route */
@coreymcmahon
coreymcmahon / front.php
Created June 2, 2012 03:19
The first version of our front controller for Simplex. - http://www.symfonycentral.com
<?php
/* Import the autoloader and classes from external namespaces */
require_once __DIR__.'/../src/autoload.php';
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
/* Build the request and response objects using HttpFoundation */
$request = Request::createFromGlobals();
$response = new Response();
@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();
@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.*"
}
}
<?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
*/
<?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
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
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
@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>