2013-05-12 78 views
1

我有兩個班,路由器和森美,兩者都是在不同的命名空間,但我需要調用Router類薩米類,但它給我一個錯誤Call類

Notice: Undefined index: GlobeAPI\Classes\Router\Sammy in /opt/lampp/htdocs/web/globeapi/GlobeAPI/Classes/Autoloader/Autoloader.php on line 151 

錯誤及其自我現在是irelavent,我不知道如何正確調用Sammy類。

這裏是Router.php代碼

namespace GlobeAPI\Classes\Router; 
class Router 
{ 
    public static $call = null; 

    function __construct() 
    { 

    } 

    public static function get($route, $call) { 
     self::$call = $call; 
     Sammy::process($route, 'GET'); 
    } 

    public static function post($route, $call) { 
     self::$call = $call; 
     Sammy::process($route, 'POST'); 
    } 

    public static function put($route, $call) { 
     self::$call = $call; 
     Sammy::process($route, 'PUT'); 
    } 

    public static function delete($route, $call) { 
     self::$call = $call; 
     Sammy::process($route, 'DELETE'); 
    } 

    public static function ajax($route, $call) { 
     self::$call = $call; 
     Sammy::process($route, 'XMLHttpRequest'); 
    } 

    public static function dispatch() 
    { 

     $call   = explode('#', self::$call); 
     $controller  = $call[0]; 
     $function  = $call[1]; 

     self::loadController($controller); 
     self::loadFunction($controller, $function); 
    } 

    public static function loadController($controller) 
    { 
     $path = APP_PATH . 'controllers/' . ucfirst($controller) . '.php'; 
     if(file_exists($path)) 
     { 
      include_once($path); 
     }else 
     { 
      return false; 
     } 
    } 

    public static function loadFunction($controller, $function) 
    { 

     $cls = ucfirst($controller); 

     if(class_exists($cls)) 
     { 
      $tmp = new $cls(); 

      if(is_callable(array($tmp, $function))) 
      { 
       $tmp->$function(); 
      }else 
      { 
       echo 0; 
      } 
     } 
    } 
} 

這裏是Sammy.php代碼

namespace GlobeAPI\Classes\Sammy; 


class Sammy { 

    public static $route_found = false; 

    public $uri = ''; 

    public $segments = ''; 

    public $method = ''; 

    public $format = ''; 

    public static function instance() { 
     static $instance = null; 

     if($instance === null) { 
      $instance = new Sammy; 
     } 

     return $instance; 
    } 

    public static function run() { 
     if(!static::$route_found) { 
      echo 'Route not defined!'; 
     } 

     ob_end_flush(); 
    } 

    public static function process($route, $type) { 
     $sammy = static::instance(); 

    // Check for ajax 
     if($type == 'XMLHttpRequest') 
      $sammy->method = isset($_SERVER['HTTP_X_REQUESTED_WITH']) ? $_SERVER['HTTP_X_REQUESTED_WITH'] : 'GET'; 

     if(static::$route_found || (!preg_match('@^'.$route.'(?:\.(\w+))[email protected]', $sammy->uri, $matches) || $sammy->method != $type)) { 
      return false; 
     } 

    // Get the extension 
    $extension = $matches[count($matches)-1]; 
    $extension_test = substr($sammy->uri, -(strlen($extension)+1), (strlen($extension)+1)); 

    if($extension_test == '.' . $extension) 
     $sammy->format = $extension; 

     static::$route_found = true; 
     Router::dispatch(); 

    } 

    public function __construct() { 
     ob_start(); 
     $this->uri = $this->get_uri(); 
     $this->segments = explode('/', trim($this->uri, '/')); 
     $this->method = $this->get_method(); 
    } 

    public function segment($num) { 
     $num--; 

    // Remove the extension 
    $this->segments[$num] = isset($this->segments[$num]) ? rtrim($this->segments[$num], '.' . $this->format) : null; 

     return isset($this->segments[$num]) ? $this->segments[$num] : null; 
    } 

    protected function get_method() { 
     return isset($_SERVER['REQUEST_METHOD']) ? $_SERVER['REQUEST_METHOD'] : 'GET'; 
    } 

    protected function get_uri($prefix_slash = true) { 
     if(isset($_SERVER['PATH_INFO'])) { 
      $uri = $_SERVER['PATH_INFO']; 
     }elseif(isset($_SERVER['REQUEST_URI'])) { 
      $uri = $_SERVER['REQUEST_URI']; 

      if(strpos($uri, $_SERVER['SCRIPT_NAME']) === 0) { 
       $uri = substr($uri, strlen($_SERVER['SCRIPT_NAME'])); 
      }elseif(strpos($uri, dirname($_SERVER['SCRIPT_NAME'])) === 0) { 
       $uri = substr($uri, strlen(dirname($_SERVER['SCRIPT_NAME']))); 
      } 

      // This section ensures that even on servers that require the URI to be in the query string (Nginx) a correct 
      // URI is found, and also fixes the QUERY_STRING server var and $_GET array. 
      if(strncmp($uri, '?/', 2) === 0) { 
       $uri = substr($uri, 2); 
      } 

      $parts = preg_split('#\?#i', $uri, 2); 
      $uri = $parts[0]; 

      if(isset($parts[1])) { 
       $_SERVER['QUERY_STRING'] = $parts[1]; 
       parse_str($_SERVER['QUERY_STRING'], $_GET); 
      }else { 
       $_SERVER['QUERY_STRING'] = ''; 
       $_GET = array(); 
      } 
      $uri = parse_url($uri, PHP_URL_PATH); 
     }else { 
      // Couldn't determine the URI, so just return false 
      return false; 
     } 

     // Do some final cleaning of the URI and return it 
     return ($prefix_slash ? '/' : '').str_replace(array('//', '../'), '/', trim($uri, '/')); 
    } 

    public function format($name, $callback) { 
     $sammy = static::instance(); 
     if(!empty($sammy->format) && $name == $sammy->format) 
     echo $callback($sammy); 
     else 
     return false; 
    } 
} 

$sammy = Sammy::instance(); 
+0

你可以更具體 – 2013-05-12 17:08:29

回答

1

由於這兩個文件不在同一個命名空間,你必須指定你想要哪一類通過添加單詞use,然後在您的Router.php上的名稱空間聲明之後添加完整的類命名空間

use GlobeAPI\Classes\Sammy\Sammy; 

您可以在documentation上看到更多解釋。

+0

,只是給我更多的錯誤 – 2013-05-12 17:32:53

+0

你應該給新的錯誤更多的細節。 – antoox 2013-05-12 17:38:53

+0

altough這是合乎邏輯的解決方案,出於某種原因,他拋出了我這個 致命錯誤:要求():失敗需要開放「/opt/lampp/htdocs/web/globeapi/Public/..//GlobeAPI/Classes/Sammy /Router.php'(include_path ='。:/ opt/lampp/lib/php')在163行基本上他正在尋找/opt/lampp/htdocs/web/globeapi/GlobeAPI/Classes/Autoloader/Autoloader.php對於GlobeAPI \ Classes \ Sammy命名空間中的Router.php – 2013-05-12 17:40:38