Courtesy of Evan Coury (@EvanDotPro)
http://blog.evan.pro/keeping-a-clean-github-fork-part-1
loading...
Happenings of the man known as SpiffyJr
Courtesy of Evan Coury (@EvanDotPro)
http://blog.evan.pro/keeping-a-clean-github-fork-part-1
Matthew O’Phinney has summarized modules quite nicely on his blog at: http://weierophinney.net/matthew/archives/267-Getting-started-writing-ZF2-modules.html.
I have written a few modules that are available on http://www.github.com/SpiffyJr. Check them out!
So, it’s been a while since I’ve posted but I’ve been hard at work the last few evenings on a few very cool features.
Let’s get to the nitty gritty and build a complete registration form, controller, and entity. At the time of writing (7/15/2011) this post is accurate, however, the latest documentation is always available on my GitHub repository.
// My/Entity/User.php
namespace My\Entity;
use Spiffy\Entity;
/**
* @ORM\Table(name="user")
* @ORM\Entity
*/
class User extends Entity
{
/**
* @var integer $id
*
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @var string $username
*
* @ORM\Column(type="string")
*/
private $username;
/**
* @var string $email
*
* @Assert\EmailAddress()
* @ORM\Column(type="string")
*/
private $email;
/**
* @var string $password
*
* @ORM\Column(type="string")
*/
private $password;
public function __toString() {
return "{$this->username}";
}
public function getId() {
return $this->id;
}
public function setId($id) {
$this->id = $id;
}
public function getUsername() {
return $this->username;
}
public function setUsername($username) {
$this->username = $username;
}
public function getEmail() {
return $this->email;
}
public function setEmail($email) {
$this->email = $email;
}
public function getPassword() {
return $this->password;
}
public function setPassword($password) {
$this->password = $password;
}
}
// My/Forms/Register.php
namespace My\Forms\Register;
use Spiffy\Dojo\Form;
class Register extends Form
{
/**
* (non-PHPdoc)
* @see Zend_Form::init()
*/
public function init() {
$this->setName('register');
$this->add('username');
$this->add('email');
$this->add('password', 'PasswordTextBox');
$this->add('submit', 'SubmitButton');
}
/**
* (non-PHPdoc)
* @see Spiffy.Form::getDefaultOptions()
*/
public function getDefaultOptions() {
return array('entity' => 'Blitzaroo\Entity\User');
}
}
// application/controllers/RegisterController.php
use My\Forms\Register;
class RegisterController extends Zend_Controller_Action
{
public function indexAction() {
$form = new Register();
$request = $this->getRequest();
if ($request->isPost()) {
if ($form->isValid($request->getPost())) {
// success!
// user entity
$user = $form->getEntity();
// outputs the value of $form->username
echo $user->getUsername();
}
}
$this->view->form = $form;
}
}
Well, there’s a small example of what you can do with the new classes. This is not an exhaustive example nor have all the features (Filters) been implemented. Enjoy!