public function indexAction() { $db = new DataBase(); $negasei = $db->selectQuery('tbl_negasei')->fetch(); // アクセスカウンター加算 $negasei['access_count']++; $db->updateQuery('tbl_negasei')->set('access_count', $negasei['access_count'])->execute(); $this->set('negasei', $negasei); }
/** * 記事のデフォルトのAction */ public function pageAction($param) { $datetime = new \DateTime($param[0]); $db = new DataBase(); $blog = $db->selectQuery('tbl_blog')->where('datetime', $param[0])->fetch(); $blog['text'] = str_replace('。', '。<br/>', $blog['text']); // コメント取得 $commentList = $db->selectQuery('tbl_comment')->where('comment_id', $blog['blog_id'])->orderBy('created_at', 'ASC')->fetchAll(); $this->set('blog', $blog)->set('datetime', $datetime->format('Y-m-d'))->set('commentList', $commentList); }
/** * コメントの記録 */ public function recordAction() { $blogId = intval($this->post('blog_id')); $name = htmlspecialchars($this->post('name', ''), ENT_QUOTES | ENT_HTML5); $text = $this->post('text', ''); $admin = 'on' === $this->post('admin') ? true : false; // text無しは登録しない if ('' === $text) { $this->redirect($this->getReferer()); return; } // 特殊文字の回避 $text = str_replace(PHP_EOL, '{{br}}', $text); $text = htmlspecialchars($text, ENT_QUOTES | ENT_HTML5); // 簡単なシンタックスハイライト $syntaxHightLightParams = ['br\\/?', 'hr\\/?', 'i', 'b', 's', 'ol', 'ul', 'li', 'pre']; $syntaxHightLights = []; foreach ($syntaxHightLightParams as $syntaxHightLightParam) { $syntaxHightLights[] = '/\\{{2}(\\/?' . $syntaxHightLightParam . ')\\}{2}/ui'; } $text = preg_replace($syntaxHightLights, '<$1>', $text); // nameの匿名化 $name = '' === $name ? '無も無き無職' : $name; $datetime = new \DateTime('NOW'); $db = new DataBase(); $numberMax = 1 + intval($db->select('number, comment_id', 'tbl_comment')->where('comment_id', $blogId)->orderBy('created_at')->limit(1)->fetch()['number']); // コメントの記録 $db->insertQuery('tbl_comment')->values([$blogId, $numberMax, $name, $text, $admin << 1, $datetime->format('Y-m-d H:i:s')]); $db->execute(); // tbl_blogのコメント数記録 $db->updateQuery('tbl_blog')->set('comment_count', $numberMax)->where('blog_id', $blogId); $db->execute(); $this->redirect($this->getReferer()); }
/** * /domain/<xxx>/<yyy>Controller.php の <zzz>Action を実行 */ public function dispatch() { // パラメーター取得 $path = parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH); $arg = parse_url($_SERVER['REQUEST_URI'], PHP_URL_QUERY); // 短いURL用 if ('' !== $this->get('s', '')) { $urlId = intval($this->get('s', '')); $db = new DataBase(); $shortUrl = $db->selectQuery('tbl_short_url')->where('url_id', $urlId)->fetch(); if (isset($shortUrl['url'])) { $this->redirect($shortUrl['url']); } } // パラメーターを / で分割 $params = array_map(function ($prm) { if ('' == $prm or 'index.php' == $prm) { return 'index'; } return $prm; }, array_slice(explode('/', $path), 1)); $this->setReadDir('/'); if (2 === count($params)) { $params[2] = 'index'; } if (2 > count($params)) { $params = ['index', 'index', 'index']; } else { $dirs = array_map(function ($param) { return ucfirst($param); }, array_slice($params, 1, -1)); $this->setReadDir('/' . implode('/', $dirs) . '/'); } $paramCount = count($params); // パラメータより取得したコントローラー名によりクラス振分け $indexName = strtolower($params[0]); $actionBase = $params[$paramCount - 1]; $className = ucfirst($indexName) . 'Controller'; $this->setReadFile($indexName, $className); // ない場合はindexへ if (!file_exists($this->controllerFile)) { error_log('file "' . $this->controllerFile . '" is not found. move to index'); $this->setReadDir('/'); $indexName = 'index'; $actionBase = $indexName; $className = 'IndexController'; $this->setReadFile($indexName, $className); if (!file_exists($this->controllerFile)) { echo $this->controllerFile; error_log('index file is not found.'); throw RuntimeException('index file not found.'); } } // クラスファイル読込 require_once $this->controllerFile; // クラスインスタンス生成 $classFullName = '\\src\\Controller' . str_replace('/', '\\', $this->readDir) . $className; $controllerInstance = new $classFullName(); // 土台smartyファイル読み込み $controllerInstance->append('view.html'); $controllerInstance->append('Elements/header.html'); $controllerInstance->append('Elements/footer.html'); $controllerInstance->append('Elements/SNSbutton.html'); // viewファイルを取得 $actionMethod = 'indexAction'; $this->viewFile = $controllerInstance->getViewFile($this->readDir, $actionBase, $actionMethod); // アクションメソッドを実行 if (is_array($actionMethod)) { $controllerInstance->{$actionMethod}[0](array_slice($actionMethod, 1)); } else { $controllerInstance->{$actionMethod}(); } // smarty 親変数 $controllerInstance->set('nowtime', (new DateTime('NOW'))->format('Y-m-d H:i:s')); $controllerInstance->set('pageName', $params[1]); // smarty表示 $controllerInstance->show($this->viewFile); }