Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- localhost/user/viewall
- $controller = new UserController();
- $controller->viewall();
- public function setData($key, $value = null)
- {
- if (!is_string($key)) {
- throw new ExceptionsTypeException("The "key" parameter must be a string");
- }
- $this->data[$key] = $value;
- }
- public function setFile($file)
- {
- if (!is_string($file)) {
- throw new ExceptionsTypeException("The "file" parameter must be a string");
- }
- $this->file = $file;
- }
- public function render()
- {
- if (empty($this->file)) {
- return "";
- }
- if (!empty($this->data)) {
- extract($this->data);
- }
- // dirty-hack: use output buffering so we can easily check if a file can be included
- // if not, simply reject the output and throw an exception (let the caller handle it)
- // if the inclusion was successfull then return with the buffered content
- ob_start();
- if (!include($this->file)) {
- ob_end_clean();
- throw new ExceptionsFatalException("View file "{$this->file}" not found");
- }
- return ob_get_clean();
- }
- public function __call($name, $args)
- {
- // ...
- $getMatches = StringHelper::match($name, "^get([a-zA-Z0-9]+)$");
- if (!empty($getMatches)) {
- $property = lcfirst($getMatches[0][1]);
- if (!property_exists($this, $property)) {
- throw new ExceptionsFatalException("Cannot execute {$name}: property {$property} not found");
- }
- $metadata = $this->inspector->getPropertyMeta($property);
- if (!isset($metadata["@readonly"]) && !isset($metadata["@readwrite"])) {
- throw new ExceptionsFatalException("Cannot execute getter on write-only property: {$property}");
- }
- return $this->$property;
- }
- // ...
- }
- // input url: localhost/user/view/12
- // =========================
- // index.php
- // =========================
- // create services
- $services = new FrameworkServiceContainer();
- {
- // TEST SERVICES
- //$services->add("user", new UserServiceMock());
- // REAL SERVICES
- $services->add("user", new UserServiceDB($db));
- }
- // create routes
- $router = new FrameworkRouter($services);
- {
- $router->addRoute("", "HomeController", "home");
- }
- // try to find a controller/action pair for this url and render the specified view
- try {
- $router->dispatchAndRender(new PageNotFoundView());
- } catch (Exception $ex) {
- die($ex->getMessage());
- }
- // =========================
- // User.php
- // =========================
- class User extends FrameworkModel
- {
- /**
- * @readwrite
- * @column
- * @primary
- * @type int
- */
- protected $id;
- /**
- * @readwrite
- * @column
- * @type text
- * @length 20
- */
- protected $name;
- // ...
- }
- // =========================
- // UserService.php
- // =========================
- abstract class UserService
- {
- public abstract function findAll();
- public abstract function findById($id);
- // ...
- }
- // =========================
- // UserController.php
- // =========================
- class UserController extends FrameworkController
- {
- private $service;
- private $view;
- public function __construct($services)
- {
- $this->service = $services->get("user");
- $this->view = new UserView();
- $this->setView($this->view);
- }
- public function view($id)
- {
- $user = null;
- if (is_numeric($id)) {
- $user = $this->service->findById($id);
- }
- $this->view->viewUser($user);
- }
- }
- // =========================
- // MainLayoutView.php
- // =========================
- class MainLayoutView extends FrameworkView
- {
- public function __construct()
- {
- $this->setFile("templates/MainLayout.php");
- }
- public function assignContent($contentView)
- {
- $this->setData("content", $contentView->render());
- }
- }
- // =========================
- // UserView.php
- // =========================
- class UserView extends MainLayoutView
- {
- public function viewUser($user)
- {
- $contentView = new FrameworkView();
- $contentView->setFile("templates/User_View.php");
- if ($user) {
- $userData = array();
- $userData["id"] = $user->getId();
- $userData["name"] = $user->getName();
- // ... any other visible data
- $contentView->setData("user", $userData);
- }
- $this->assignContent($contentView);
- }
- }
- // =========================
- // templates/User_View.php
- // =========================
- if (isset($user)) {
- echo "id: {$user['id']}<br />";
- echo "name: {$user['name']}<br />";
- } else {
- echo "<b>User not found</b>";
- }
- public function register()
- {
- $formSent = $this->view->readRegisterForm($username, $password);
- if ($formSent) {
- $user = new User();
- // validate input (using the User instance?)
- // TODO
- $valid = false;
- if ($valid) {
- // hash password
- $password = password_hash($password, PASSWORD_DEFAULT);
- $user->setName($username);
- $user->setPassword($password);
- $this->service->add($user);
- $this->view->viewRegisterComplete($user);
- return;
- }
- }
- $this->view->viewRegisterForm();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement