http://framework.zend.com

 


Fri 16 Oct 2015

What is Zend Framework?

Open source framework for developing web applications and services using PHP.
In the beginning...
<?php /* login.php */
$conn = new mysqli('localhost', 'username', 'password'); // Database connection
if (!$conn) { die('Connection failed: ' . mysqli_connect_error()); }

if (isset($_POST['submit'])) { // Check form submit
    $user = $_POST['user'];
    $pass = $_POST['pass'];
    $result = $conn->query(
        "SELECT * FROM user WHERE username='{$user}' AND password='{$pass}'"
    );
    header('Location:' . (mysqli_num_rows($result) > 0 ? 'ok.php' : 'fail.php'));
    exit;
}
?>
<html>
  <head><title>My Website</title></head>
  <body>
    <form>
      Username: <input type="text" name="user"><br />
      Password: <input type="password" name="pass"><br />
      <input type="submit" />
    </form>
  </body>
</html>
Then, common include files...
<?php /* login.php */
include 'connection.php';
include 'functions.php';

if (isset($_POST['submit'])) { // Check form submit
    $user = $_POST['user'];
    $pass = $_POST['pass'];
    header('Location: ' . (isLoginValid($user, $pass) ? 'ok.php' : 'fail.php'));
    exit;
}
?>
<html>
  <?php include 'head.php'; ?>
  <body>
    <form>
      Username: <input type="text" name="user"><br />
      Password: <input type="password" name="pass"><br />
      <input type="submit" />
    </form>
  </body>
</html>
Years later...Model-View-Controller (MVC) design pattern
/* module/App/src/App/UserMapper.php */
class UserMapper extends \Zend\Db\TableGateway\AbstractTableGateway { }

/* module/App/src/App/User.php */
class User { }

/* module/App/src/App/LoginForm.php */
class LoginForm extends \Zend\Form
{
    public function init()
    {
        $this->add(array(
            'name' => 'username',
            'type' => 'Zend\Form\Element\Text',
            'options' => array('label' => 'Username'),
        ));

        $this->add(array(
            'name' => 'password',
            'type' => 'Zend\Form\Element\Password',
            'options' => array('label' => 'Password'),
        ));
    }
}
Model
/* module/App/view/layout/layout.phtml */
<html>
  <head>
    <title>My Website</title>
  </head>
  <body>
    <?php echo $this->content; ?>
  </body>
</html>

/* module/App/view/app/auth/login.phtml */
<?php
$form->prepare();
echo $form;
?>
View
namespace App\Controller; /* module/App/src/App/Controller/AuthController.php */
class AuthController extends \Zend\Mvc\Controller\AbstractActionController
{
    public function loginAction
    {
        $form = new LoginForm();
        $request = $this->getRequest();
        if ($this->getRequest()->isPost()) {
            $form->setData($request->getPost());
            if ($form->isValid()) {
                $data = $form->getData();
                $mapper = new UserMapper();
                $user = $mapper->fetchByCredentials($data['user'], $data['pass']);
                if ($user) {
                    return $this->redirect()->toRoute('dashboard');
                }
            }
            $this->flashMessenger()->addErrorMessage('The form has errors');
        }

        return array('form' => $form);
    }
}
Controller

Why choose Zend Framework?

  • PHP
  • Zend
  • PSR (PHP Standard Recommendation)
  • Best Practices and Design Patterns
  • Continuous Improvement
    (ZF1 - 2006, ZF2 - 2012, ZF3 - 2015)
  • Certification, Zend Yellow Pages
Practice software craftsmanship; Don't be a code monkey

"Designing Beautiful Software" talk, ZendCon 2012
Matthew Weier O'Phinney
Project Lead, Zend Framework