<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                企業??AI智能體構建引擎,智能編排和調試,一鍵部署,支持知識庫和私有化部署方案 廣告
                **模型:** 所有的輸入最終被推送到模型結束,所有的輸出數據也都來自于模型(一般是操作數據庫、文件或者web服務) **視圖** 將數據從模型中取出并輸出給用戶,頁面和表單在這里輸出 **控制器** 控制器根據用戶的請求確定用戶可以做什么,接著我們用模型執行請求的操作并檢索組裝請求數據,最后調用視圖將操作的結果顯示給用戶 控制器有一個最基本的用途就是接受GET參數以確定需要傳送什么樣的頁面 ``` $page=basename($_GET['page']); if ($page=='user-account') { require_once 'user-account.php'; } require_once $page.'-view.php' ``` 需要.../test.php?page=user-accent&user-id=123&action=view這種url格式訪問頁面,不是我們想要的,我們怎么將url變成我們想要的/user-account/view/123這樣的pathinfo訪問模式呢?答:通過[url重寫](http://www.hmoore.net/a173512/thinkphp5/1806786) 我們可以通過$_SERVER['REQUEST_URI'];(這個變量只有 apache 才支持)來檢索請求字符串 掌握了url重寫和$_SERVER['REQUEST_URI']后我們就可以通過任何方式來解析url,要做到這一點我們需要創建一個路由router >[info]**創建路由的原因:** >* 允許指定精確的正則表達式 >* 支持指定鍵值對的語法 >* 創建一個帶有限結構的完整解析器 > >在這個路由器中,我們可以指定:key或type:key中人一個作為url結構中的占位符,支持的類型如下: >* any >* integers >* alpha(包括破折號和下劃線) >* alpha plus numeric >* 正則表達式(自定義模式) ``` abstract class RouterAbstract{ abstract public function addRoute($route,$options=array()); abstract public function getRoute($request); } class RouterRegex extends RouterAbstract{ const REGEX_ANY="([^/]+?)"; const REGEX_INT="([0-9]+?)"; const REGEX_ALPHA="([a-zA-Z_-]+?)"; const REGEX_ALPHANUMERIC="([0-9a-zA-Z_-]+?)"; const REGEX_STATIC="%s"; /** * [$routes 保存編譯后的路由 * @var array */ protected $routes=array(); /** * $basUrl 保存基本url便于使用子文件夾(/store/app)中的路由器 * @var string */ protected $basUrl=''; /** * 指定基本的url * @param string $baseUrl 基本url */ public function setBaseUrl($baseUrl){ //url中充滿了默認的分隔符,所以當需要轉義時用@代替這些默認的/正則分隔符而是我們能夠更加簡單的創建正則表達式 $this->baseUrl=preg_quote($baseUrl,"@"); } /** * 增加路由,允許指定模式及一組將于鍵/值對結合的選項 * @param [type] $route [description] * @param array $options [description] */ public function addRoute($route,$options=array()){ $this->routes[]=array('pattern'=>$this->_pareRoute($route),'options'=>$options); } public function _pareRoute($route){ $baseUrl= $this->baseUrl; if ($route=='/') { return "@^$baseUrl/$@"; } //用/分割路由規則(如:/alpha:page/alpha:action/:id)得到每個部分 $parts=explode('/',$route);//array ( 0 => '', 1 => 'alpha:page', 2 => 'alpha:action', 3 => ':id', ) //初始url @^ $regex="@^$baseUrl"; //刪除以/開頭的路由規則分割時產生的空數組,并重組(索引也會變) if ($route[0]=="/") { array_shift($parts);//$parts=array ( 0 => 'alpha:page', 1 => 'alpha:action', 2 => ':id', ) } //便利路由規則分割后的數組已處理url foreach ($parts as $part) { $regex.="/";//$regex=@^/ $args=explode(':',$part);//array(2) { [0]=> string(5) "alpha" [1]=> string(6) "action" } if (count($args)==1) {// 沒有:和alpha等這些只有值如page時的情況 $regex.=sprintf(self::REGEX_STATIC,preg_quote(array_shift($args),"@")); continue; }elseif ($args[0]=='') { //:id走這里 array_shift($args); $type=false; }else{ //alpha:page/alpha:action走這里,刪除alpha,并返回刪除的元素,數組為空返回null $type=array_shift($args); } //刪除第一個元素并返回,這里返回的刪除值分別就是page、action、id $key=array_shift($args); //如果類型不是alpha、int、等,而是regex(正則) if ($type=="regex") { $regex.=$key; continue; } //刪除鍵名 $this->normalize($key); $regex.="(?P<$key>"; switch (strtolower($type)) { case 'int': case 'integer': $regex.=self::REGEX_INT; break; case 'alpha': $regex.=self::REGEX_ALPHA; break; case 'alphanumeric': case 'alphanum': case 'alnum': $regex.=self::REGEX_ALPHANUMERIC; break; default: $regex.=self::REGEX_ANY; break; } $regex.=")"; } $regex.='$@u'; //@^/(?p<page>([a-zA-Z_-]+?))/(?p<action>([a-zA-Z_-]+?))/(?p<id>([^/]+?))$@u //echo '<br>'.htmlentities($regex).'<br>'; return $regex; } /** * 清理alpha、int、alnum等鍵名 * @param [type] &$param [description] * @return [type] [description] */ public function normalize(&$param){ $param=preg_replace("/[^a-zA-Z0-9]/", "", $param); } /** * 解析指定的url,并將它解析到路由的鍵值對下 將url轉化為mvc對應的數組 * @param string $request $_SERVER['REQUEST_URI']的值,如:user-account/view/123 * @return [type] [description] */ public function getRoute($request){ $matches=array(); foreach ($this->routes as $route) { if (preg_match($route['pattern'],$request ,$matches)) { foreach ($matches as $key => $value) { if (is_int($key)) { unset($matches[$key]); } } //合并數組 $result=$matches+$route['options']; return $result; } } return false; } } $router=new RouterRegex(); //$router->setBaseUrl('store'); $router->addRoute("/alpha:page/alpha:action/:id",array('controller'=>'default')); //$route=$router->getRoute('/user-account/view/123'); //var_export($route);//array ( 'page' => 'user-account', 'action' => 'view', 'id' => '123', 'controller' => 'default', ) //$router->addRoute("/photos/alnum:user/int:photoId/in/regex:(?P<groupType>([a-z]+?))-(?P<groupId>([0-9]+?))"); //$route=$router->getRoute('/photos/dishafik/5584919786/in/set-72157626290864145'); //var_export($route);//array ( 'user' => 'dishafik', 'photoId' => '5584919786', 'groupType' => 'set', 'groupId' => '72157626290864145', ) ``` 到這里我們實現了將 ``` class Photos_Controller{ protected $router=false; //dispatch:調度/分發/分派分配 public function dispatch($url,$default_data=array()){ //try{ if (!$this->router) { throw new Exception("沒有設置路由", 1); } //將url轉化為mvc對應的數組 $route=$this->router->getRoute($url); var_export($route); $controller=ucfirst($route['controller']); $action=ucfirst($route['action']); unset($route['controller']); unset($route['action']); //獲取model實例 $model=$this->getModel($controller); $data=$model->{$action}($router); $data=$data+$default_data; //獲取view實例 $view=$this->getView($controller,$action); //輸出模板 echo $view->render($data); try{}catch(Exception $e){ try{ if ($url!='/error') { $data=array('message'=>$e->getMessage()); $this->dispatch('/error',$data); }else{ throw new Exception("Error Route Undefined", 1); } }catch(Exception $e){ echo "<h1>An Unkonwn error occurred.</h1>"; } } } public function setRouter(RouterAbstract $router){ $this->router=$router; } /** * 獲取mode類實例 * @param [type] $name [description] * @return [type] [description] */ protected function getModel($name){ $name.='_Model'; $this->includeClass($name); return new $name; } /** * 獲取view類實例 * @param [type] $name [description] * @param [type] $action [description] * @return [type] [description] */ protected function getView($name,$action){ $name.='_'.$action.'View'; $this->includeClass($name); return new $name; } protected function includeClass($name){ echo '<br>',$name;echo '<br>'; //str_replace(':','#', $str);//將:替換為# $file=str_replace(DIRECTORY_SEPARATOR, '_', $name).'.php'; echo $file; if (!file_exists($file)) { throw new Exception("類不存在", 1); } require_once $file; } } $router=new RouterRegex(); //$router->setBaseUrl('store'); $router->addRoute("/alpha:page/alpha:action/:id",array('controller'=>'photos')); $pc=new Photos_Controller(); $pc->setRouter($router); //$pc->dispatch('/photos/getPhoto/123'); //瀏覽器訪問 http://localhost/test.php/photos/getPhoto/123 $pc->dispatch($_SERVER['REQUEST_URI']); ``` Photos_Model.php ``` <?php //模型 class Photos_Model{ public function getPhoto($options){ return array( 'title'=>'Booke in the Woods', 'width'=>427, 'height'=>640, 'url'=>'http://farm6.static.flickr.com/5142/55884010786_95a4c15e8a_z.jpg' ); } } ``` Photos_GetPhotoView.php ``` <?php //視圖 //這里使用簡單的sprintf調用HTML模板,后面用smarty、twig、savant等模板引擎 class Photos_GetPhotoView{ public function render($data){ $html='<h1>%s</h1>'.PHP_EOL; $html.='<img src="%s" width="%s" height="%s">'.PHP_EOL; $return=sprintf($html,$data['title'],$data['url'],$data['width'],$data['height']); return $return; } } ```
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看