/** * リクエストされたURLをパースして、ClassとActionを決定 * * @param string $APP_ROOT 現在のMinアプリの絶対ディレクトリパス * @return void */ protected static function setPathAndAction($APP_ROOT) { # cssフォルダとjsフォルダのファイルはそのまま吐き出す if (preg_match("!^/.*/(css|js)/.*!", $_SERVER['SCRIPT_NAME'], $m)) { $filePath = $APP_ROOT . '/view' . $_SERVER['SCRIPT_NAME']; if (file_exists($filePath)) { $time = filemtime($filePath); header('Last-Modified: ' . gmdate('D, d M Y H:i:s', $time) . ' GMT'); if ($m[1] == 'css') { header("Content-Type: text/css"); } elseif ($m[1] == 'js') { header("Content-Type: text/javascript"); } readfile($filePath); } else { header("HTTP/1.0 404 Not Found"); echo "404 CSS|JS Not Found"; } exit; } # (/imgフォルダ外の)JPEG、PNG、GIF画像を吐き出す if (preg_match("!^/(.*)\\.(jpeg|JPEG|jpg|JPG|png|PNG|gif|GIF)!", $_SERVER['SCRIPT_NAME'], $m)) { $filePath = $APP_ROOT . '/var/images' . $_SERVER['SCRIPT_NAME']; Console::log($filePath); if (file_exists($filePath)) { self::sendImage($filePath); } else { header("HTTP/1.0 404 Not Found"); echo "404 Image Not Found"; } exit; } # 言語を選択 if (preg_match("!^/(en|fr)/(.*)!", $_SERVER['SCRIPT_NAME'], $m)) { $SCRIPT_NAME = '/' . $m[2]; self::$lang = $m[1]; } else { $SCRIPT_NAME = $_SERVER['SCRIPT_NAME']; self::$lang = 'jp'; } # '/' にmatch if ($SCRIPT_NAME == '/') { if (self::$trace) { Console::log('Dispatcher:A'); Console::log($m); } self::$path = '/'; self::$class = 'IndexCtl'; self::$classFile = '/IndexCtl.php'; self::$action = 'index'; // action = function index() return; } # '/abcd' にmatch if (preg_match("|^/([^/]*)\$|", $SCRIPT_NAME, $m)) { if (self::$trace) { Console::log('Dispatcher:B'); Console::log($m); } self::$path = '/'; self::$class = 'IndexCtl'; self::$classFile = '/IndexCtl.php'; self::$action = $m[1]; // action = function abcd() return; } # '/abcd/' にmatch (actionを省略) if (preg_match("|^/([^/]*)/\$|", $SCRIPT_NAME, $m)) { if (self::$trace) { Console::log('Dispatcher:C'); Console::log($m); } self::$path = $SCRIPT_NAME; // pass = /abcd/ self::$class = ucfirst($m[1]) . 'Ctl'; // class = class AbcdCtl self::$classFile = self::$path . self::$class . '.php'; // file = /abcd/AbcdCtl.php self::$action = 'index'; // action = function index() return; } # '/abcd/efg'や'/abcd/efg/hij' にmatch if (preg_match("|^/(.*)/([^/]*?[^/])\$|", $SCRIPT_NAME, $m)) { if (self::$trace) { Console::log('Dispatcher:D'); Console::log($m); } self::$path = '/' . $m[1] . '/'; // pass = /abcd/efg/ $tArray = explode('/', $m[1]); //self::$class = array_pop($tArray).'Ctl'; // class = class efgCtl foreach ($tArray as $key => $val) { $tArray[$key] = ucfirst($val); } self::$class = implode('', $tArray) . 'Ctl'; // class = class AbcdEfgCtl self::$classFile = self::$path . self::$class . '.php'; // file = /abcd/efg/AbcdEfgCtl.php self::$action = $m[2]; // action = function hij() return; } # '/abcd/efg/'や'/abcd/efg/hij/' にmatch (actionを省略) if (preg_match("|^/(.*)/([^/]*?)/\$|", $SCRIPT_NAME, $m)) { if (self::$trace) { Console::log('Dispatcher:E'); Console::log($m); } self::$path = '/' . $m[1] . '/' . $m[2] . '/'; // pass = /abcd/efg/hij/ $tArray = explode('/', $m[1] . '/' . $m[2]); foreach ($tArray as $key => $val) { $tArray[$key] = ucfirst($val); } self::$class = implode('', $tArray) . 'Ctl'; // class = class AbcdEfgHijCtl self::$classFile = self::$path . self::$class . '.php'; // file = /abcd/efg/hij/AbcdEfgHij.php self::$action = 'index'; // action = function index() return; } }
protected function uri($segment = NULL) { $path = explode('/', Dispatcher::path()); array_shift($path); if ($segment === NULL) { // Return an array of the path return $this->sanitize($path); } elseif ($segment === TRUE) { // Return an string of the path return implode('/', $this->sanitize($path)); } else { // Return just a segement of the path return $this->sanitize($path[$segment]); } }