Overview

Packages

  • tipy

Classes

  • Tipy
  • TipyApp
  • TipyCli
  • TipyCliSession
  • TipyConfig
  • TipyController
  • TipyCookie
  • TipyDAO
  • TipyEnv
  • TipyFlash
  • TipyInflector
  • TipyInput
  • TipyIOWrapper
  • TipyLogger
  • TipyMailer
  • TipyModel
  • TipyOutput
  • TipyRequest
  • TipySession
  • TipyTestCase
  • TipyTestRunner
  • TipyView

Exceptions

  • AssertionFailedException
  • CompileErrorException
  • CompileWarningException
  • CoreErrorException
  • CoreWarningException
  • DeprecatedException
  • NoMethodException
  • NoticeException
  • ParseException
  • RecoverableErrorException
  • StrictException
  • TipyDaoException
  • TipyException
  • TipyModelException
  • TipyRollbackException
  • TipyValidationException
  • UserDeprecatedException
  • UserErrorException
  • UserNoticeException
  • UserWarningException
  • WarningException
  • Overview
  • Package
  • Class
  • Deprecated
  • Todo
  1:   2:   3:   4:   5:   6:   7:   8:   9:  10:  11:  12:  13:  14:  15:  16:  17:  18:  19:  20:  21:  22:  23:  24:  25:  26:  27:  28:  29:  30:  31:  32:  33:  34:  35:  36:  37:  38:  39:  40:  41:  42:  43:  44:  45:  46:  47:  48:  49:  50:  51:  52:  53:  54:  55:  56:  57:  58:  59:  60:  61:  62:  63:  64:  65:  66:  67:  68:  69:  70:  71:  72:  73:  74:  75:  76:  77:  78:  79:  80:  81:  82:  83:  84:  85:  86:  87:  88:  89:  90:  91:  92:  93:  94:  95:  96:  97:  98:  99: 100: 101: 102: 103: 104: 105: 106: 107: 108: 109: 110: 111: 112: 113: 114: 115: 116: 117: 118: 119: 120: 121: 122: 123: 124: 125: 126: 127: 128: 129: 130: 131: 132: 133: 134: 135: 136: 137: 138: 139: 140: 141: 142: 143: 144: 145: 146: 147: 148: 149: 150: 151: 152: 153: 154: 155: 156: 157: 158: 159: 160: 161: 162: 163: 164: 165: 166: 167: 168: 169: 170: 171: 172: 173: 174: 175: 176: 177: 178: 179: 180: 181: 182: 183: 184: 185: 186: 187: 188: 189: 190: 191: 192: 193: 194: 195: 196: 197: 198: 199: 200: 201: 202: 203: 204: 205: 206: 207: 208: 209: 210: 211: 212: 213: 214: 215: 216: 217: 218: 219: 220: 221: 222: 223: 224: 225: 226: 227: 228: 229: 230: 231: 232: 233: 234: 235: 236: 237: 238: 239: 240: 241: 242: 243: 244: 245: 246: 247: 248: 249: 250: 251: 
<?php
/**
 * TipyController
 *
 * @package tipy
 */

/**
 * C in MVC. Receive the request, fetch or save data from a models, and use a TipyView to create HTML output
 *
 * <code>
 * // app/controllers/BlogController.php
 * class MyController extends TipyController {
 *     public function myAction() {
 *         // your application logic
 *     }
 * }
 * </code>
 *
 * Application input is available in *$this->in*, application output goes to *$this->out*
 *
 * ## Default template rendering
 *
 * At the end of the action HTML template will be rendered automatically.
 * Automatic template name is contructed from controller name (without "Controller")
 * and action name
 *
 * <code>
 * Action                    Template Path
 * ------------------------------------------
 * BlogController::index()   /app/views/Blog/index.php
 * BlogController::post()    /app/views/Blog/post.php
 * </code>
 *
 * ## Custom template rendering
 *
 * You can explicitely render template with custom name
 *
 * <code>
 * // app/controllers/BlogController.php
 * class BlogController extends TipyController {
 *     public function article() {
 *         $this->out('title', 'Hello');
 *         $this->out('message', 'World!');
 *         $this->renderView('path/to/custom/template');
 *     }
 * }
 * </code>
 *
 * ## Disable TipyView rendering
 *
 * If you use custom template engine or your action outputs formats different
 * from text or HTML you may want to disable default tipy rendering
 *
 * <code>
 * $this->skipRender = true;
 * </code>
 *
 */
class TipyController {

    /**
     * @var TipyConfig
     */
    public $config;

    /**
     * @var TipyInput
     */
    public $in;

    /**
     * @var TipyOutput
     */
    public $out;

    /**
     * @var TipyEnv
     */
    public $env;

    /**
     * @var TipyView
     */
    public $view;

    /**
     * @var mysqli|null
     */
    public $db;

    /**
     * @var TipySession
     */
    public $session;

    /**
     * @var TipyCookie
     */
    public $cookie;

    /**
     * @var TipyFlash
     */
    protected $flash;

    /**
     * @var string
     */
    private $templateName;

    /**
     * Set this to *true* to turn off TipyView template rendering
     * @var boolean
     */
    public $skipRender = false;

    /**
     * Instantiate controller with application context
     */
    public function __construct() {
        $app = TipyApp::getInstance();
        $this->config   = $app->config;
        $this->request  = $app->request;
        $this->in       = $app->in;
        $this->out      = $app->out;
        $this->env      = $app->env;
        $this->cookie   = $app->cookie;
        $this->view     = $app->view;
        $this->db       = $app->db;
        $this->session  = $app->session;
        $this->flash    = new TipyFlash($this->session);
    }

    /**
     * Shortcut to $this->in->get()
     * @param string $key
     * @param mixed $defaultValue
     * @return mixed
     */
    public function in($key, $defaultValue = null) {
        return $this->in->get($key, $defaultValue);
    }

    /**
     * Shortcut to $this->in->set()
     * @param string $key
     * @param mixed $value
     */
    public function out($key, $value) {
        return $this->out->set($key, $value);
    }

    /**
     * Render template and return result as a string
     * @param string $templateName
     * @return string
     */
    public function renderTemplate($templateName) {
        $this->view->bind($this->out->getAll());
        return $this->view->processTemplate($templateName);
    }

    /**
     * Set custom template name for rendering.
     * This method force controller to render template
     * even if {@link $skipRender} is set to true
     * @param string $templateName
     */
    public function renderView($templateName) {
        $this->skipRender = false;
        $this->templateName = $templateName;
    }

    /**
     * Execute controller action by name with all before/after hooks
     * @param string $action
     */
    public function execute($action) {
        $this->executeBefore();
        $this->$action();
        $this->executeAfter();
        if (!$this->skipRender) {
            if (!$this->templateName) {
                $this->template = $this->actionToTemplaName($action);
            }
            // Render template to output buffer
            echo $this->renderTemplate($this->templateName);
        }
    }

    /**
     * Path to template
     * @param string $action
     * @return string
     */
    public function actionToTemplaName($action) {
        $className = get_class($this);
        $className = str_replace('Controller', '', $className);
        $this->templateName = $className.'/'.$action;
    }

    /**
     * Execute function wrapper. In TEST_MODE it does not exit but
     * throws exception to continue tests
     * @param string $action
     */
    private function safeExit($message) {
        if (defined('TEST_MODE') and TEST_MODE) {
            throw new TipyException($msg);
        } else {
            exit;
        }
    }

    /**
     * Redirect
     * @param string $path
     */
    public function redirect($path) {
        while (@ob_end_clean()) {
        }
        header('HTTP/1.0 302 Moved Temporarily');
        header('Location: '.$path);
        $this->safeExit('Redirected to '.$path);
    }

    /**
     * 404 
     * @todo Implement fancy 404 page
     */
    public function pageNotFound() {
        while (@ob_end_clean()) {
        }
        header('HTTP/1.0 404 Not Found');
        $this->safeExit('Status: 404 Not Found');
    }

    /**
     * Hook to execute before action. Override this in your controller.
     */
    public function executeBefore() {
    }

    /**
     * Hook to executed after action. Override this in your controller.
     */
    public function executeAfter() {
    }
}
tipy API documentation generated by ApiGen