2017-05-02 119 views
0

我寫了一點框架。無法訪問網址/page/test。說page not found。可能是urls.php中的正則表達式有問題?它看起來像瀏覽器處理URL本身,但腳本應該這樣做。URI解析的正則表達式

<?php 

class griEngine { 

public 
    $settings, //settings 
    $uri, //current URI 
    $app; //curent app 

public function __construct($settings) { 

    $this->settings= $settings; 
    $this->uri = urldecode(preg_replace('/\?.*/iu','',$_SERVER['REQUEST_URI'])); 
    $this->app = false; 
    $this->process_path(); 
    $this->process_controllers(); 

} 

public function process_path() { 

    foreach($this->settings['apps'] as $iterable_app) 
    { 
     $iterable_urls = require(BASE_DIR. '/apps/'. $iterable_app. '/urls.php'); 
     foreach($iterable_urls as $pattern => $method) 
     { 
      $matches = array(); 
      if (preg_match($pattern, $this->uri, $matches)) 
      { 

       $this->app = array($iterable_app, array('pattern' => $pattern, 'method' => $method, 'args' => $matches)); 
       break(2); 


    } 

    if($this->app ==='false') 
    { 
     exit('App not found.'); 
    } 
} 

public function process_controllers() { 

    if ($this->app || is_array($this->app)) 
    { 
     require(BASE_DIR.'/apps/'.$this->app['0'].'/controller.php'); 
     $controller_name = $this->app['0'].'_Controller'; 
     $this->app_controller = new $controller_name(); 
     $this->app_controller->{$this->app['1']['method']}($this->app['1']['args']); 
    } 
} 


} 

文件urls.php

<?php 

return array(

'#^/*$#i' => 'MainPage', 
'#^/Page/([A-z0-9_-])/*#i' => 'ViewPage' 

); 

文件Controller.php這樣

<?php 


class Simple_Pages_Controller extends gri_Controller { 

public function MainPage($args){ 
    echo 'Hello world'; 
} 


public function ViewPage(){ 
    echo 'test'; 
} 
} 
?> 

回答

0

你在這裏^/Page/([A-z0-9_-])/*#i失蹤+。作爲/page/testtest包含超過1的性格,所以你應該使用+

更改爲:

return array(

'#^/*$#i' => 'MainPage', 
'#^/Page/([A-z0-9_-])/*#i' => 'ViewPage' 

); 

此:

<?php 
return array(

'#^/*$#i' => 'MainPage', 
'#^/Page/([A-z0-9_-]+)/*#i' => 'ViewPage' 

); 
+0

謝謝,但還是「對象不找到了!」錯誤404:((( – Grigorie

+0

@Grigorie現在'regex'的問題已經修復,你目前的問題可能是你的'服務器'的問題) –

+0

謝謝)我會試試) – Grigorie