わかった風のことを書くBLOG

仕事(IT)のはなしや、地元(沖縄)の話などなど記載してきます〜

全く参考にならないのでマネしないでねフレームワーク

MVCもいまいち理解していない初心者の全く参考にならないフレームワークのログ
とりあえずコントローラー編

http://example/actionName/methodName/ みたいな感じで特定のアクションクラスの
methodを実行させたい


■library/ashigar/controller/mother.php

_httpIsnstance = new ashigar_controller_request_http();
    }

    /**
     * モジュールアクションを実行
     * 
     */
    public function run()
    {
        // URLから実行するモジュールとアクション名をセットする
        $this->_dispatch();
        // 該当のモジュールを実行する
        $this->_moduleExec();
    }
    
    /**
     * dispatchでURLのマッピングを行う
     *
     */
    protected function _dispatch()
    {
        // モジュールとアクションをUrlにマッピングする
        $this->_setMappingUrl();
    }
    
    /**
     * URLマッピングを行う
     */
    private function _setMappingUrl()
    {
        $moduleDir = $this->_httpIsnstance->getBaseDir() . DIRECTORY_SEPARATOR .
        				'modules' . DIRECTORY_SEPARATOR . $this->_httpIsnstance->getModuleName();
        $this->_actionFile = $moduleDir . DIRECTORY_SEPARATOR . 'action.php';
    }
    
    /**
     * モジュールを実行
     *
     */
    private function _moduleExec($actionFile = null)
    {
        if($actionFile === null) {
            $actionFile = $this->_actionFile;
        }
        
        try {
            // ファイルの存在チェック
            if(!file_exists($actionFile)) {
                echo 'no action file';
                return null;
            }
            
            // 各モジュールのactionファイルを読み込み
            require_once $actionFile;
            $class = $this->_httpIsnstance->getModuleName() . 'Action';
            $method = $this->_httpIsnstance->getActionName() . 'Action';

            // クラスとメソッドの存在チェック
            if(!class_exists($class)) {
                echo 'no class name';
                return null;
            } else if(!method_exists(new $class(), $method)) {
                $method = 'indexAction';
            }
            
            // モジュールを実行
            call_user_func(array($class, $method));
        
        } catch(Exception $e) {
            throw $e;
        }
    }
}

■リクエスト系を操作するクラス library/ashigar/controller/request/http.php

_init();
    }
    
    private function _init()
    {
        $this->_parseUrl();
        $this->_setBaseDir();
    }
    
    /**
     * URLを分解する
     */
    private function _parseUrl()
    {
       $requestUri = $this->getRequestUri();
       $arrSeg = explode('/', trim($requestUri, '/'));
       $this->setModuleName($arrSeg[0]);
       $this->setActionName($arrSeg[1]);
    }
    
    /**
     * _requestUri をセットする
     *
     * @param string $_requestUri
     */
    public function setRequestUri($_requestUri = null)
    {
       if ($_requestUri === null) {
          if (isset($_SERVER['HTTP_X_REWRITE_URL'])) { // check this first so IIS will catch
             $_requestUri = $_SERVER['HTTP_X_REWRITE_URL'];
          } elseif (isset($_SERVER['REQUEST_URI'])) {
             $_requestUri = $_SERVER['REQUEST_URI'];
          } elseif (isset($_SERVER['ORIG_PATH_INFO'])) { // IIS 5.0, PHP as CGI
             $_requestUri = $_SERVER['ORIG_PATH_INFO'];
             if (!empty($_SERVER['QUERY_STRING'])) {
                $_requestUri .= '?' . $_SERVER['QUERY_STRING'];
             }
          }
       }
       $this->_requestUri = $_requestUri;
    }

    /**
     * _requestUri を得る
     *
     * @return string
     */
    public function getRequestUri()
    {
       if (empty($this->__requestUri)) {
          $this->setRequestUri();
       }

       return $this->_requestUri;
    }
            
    /**
     * モジュール名をセット
     */
    public function setModuleName($moduleName)
    {
        $this->_moduleName = $moduleName;
    }

    /**
    * module name
    *
    * @return strig
    */
    public function getModuleName()
    {
       return $this->_moduleName;
    }
    
    /**
    * アクション名を取得
    *
    * @return string
    */
    public function getActionName()
    {
       return $this->_actionName;
    }

    /**
    * アクション名を取得
    *
    * @return string
    */
    public function setActionName($actionName)
    {
       return $this->_actionName = $actionName;
    }
    
    /**
     * baseディレクトリの設定
     *
     */
    private function _setBaseDir()
    {
        $this->_baseDir = dirname($_SERVER['SCRIPT_FILENAME']);
    }
    
    /**
     * baseディレクトリを取得
     */
    public function getBaseDir()
    {
        if($this->_baseDir === null)
        {
            $this->_setBaseDir();
        }
        
        return $this->_baseDir;
    }
}

■テストモジュール modules/example/action.php

■rootディレクトリにindex.phpを設置 mod_rewriteでこのindex.phpを絶対に実行する

run();

■実行

http://example.com/example/index/

とりあえず表示されました。