2011-07-16 2 views
4

私は検索してきましたが、有用な情報はほとんど見つかりませんでした。 symfony2ルーティングコンポーネント(とyamlコンポーネント)を私の個人的なフレームワークで使用しようとしていますが、基本的なセットアップに関するドキュメントは見つかりませんでした。 symfony siteのドキュメントは、コンポーネントの使用方法を説明していますが、スタンドアロンセットアップの方法ではあまり役に立ちません。symfony2ルーティングコンポーネントを使用する(symfony2の外で)

symfony2の個々のコンポーネントを設定する方法を説明できる良い場所をお勧めしますか?

+0

良い質問!私は現在、まったく同じことに不思議です! – Raffael

答えて

-3

私はsymfony siteのドキュメントを見終わっただけでなく、githubの上のソースコード(Route.phpRouteCollection.phpRouteCompiler.php)と私自身の基本的なルータを思い付きました。

symfonyと同じように、私はルートクラスに渡す引数を1)パターン、2)デフォルトのコントローラ/アクション、3) - これは私の要件ではないので、まだコード化していません。

私のメイン/フロントコントローラ(index.php)は、lib \ルート名前空間内の他のルートクラスにアクセスできるスタティックルータクラスにのみ通話します。

Router :: Mapは、Router :: _ ProcessでhttpRequestを照合するために使用されるcompiledRouteCollectionに渡される正規表現文字列(デフォルトのコントローラ/アクションと変数名とともに)を基本的にコンパイルします。そこから、プロセスメソッドに一致するものがあれば、一致したcompiledRouteのデフォルトコントローラ/アクションの値に基づいてコントローラ/アクション/ argsを設定します。 preg_matchがなければ、私は要求オブジェクトからのデフォルトのコントローラ/アクション/ argsを使用します。残りはケーキです...

誰かがこれを見つけて、私が今日この時間を費やした時間を節約できたら幸いです。乾杯!

のindex.php

require_once 'config/config.php'; 
require_once 'lib/autoload.php'; 
lib\Router::Map(
    'users_username_id', 
    'users/{username}/{id}', 
    array('controller' => 'testController', 'action' => 'users') 
); 
lib\Router::Route(new lib\Request()); 

router.php

namespace lib; 

class Router { 
protected static $_controller; 
protected static $_action; 
protected static $_args; 
protected static $_compiledRouteCollection; 
private static $_instance; 

private function __construct() { 
    self::$_compiledRouteCollection = new \lib\route\CompiledRouteCollection(); 
} 

public static function Singleton() { 
    if(!isset(self::$_instance)) { 
     $className = __CLASS__; 
     self::$_instance = new $className; 
    } 
    return self::$_instance; 
} 

public static function Route(Request $request, $resetProperties = false) { 
    self::Singleton(); 

    self::_Process($request,$resetProperties); 

    $className = '\\app\\controllers\\' . self::$_controller; 
    if(class_exists($className, true)) { 
     self::$_controller = new $className; 

     if(is_callable(array(self::$_controller, self::$_action))) { 
      if(!empty(self::$_args)) { 
       call_user_func_array(array(self::$_controller, self::$_action), self::$_args); 
      } else { 
       call_user_func(array(self::$_controller, self::$_action)); 
      } 
      return; 
     } 
    } 
    self::Route(new \lib\Request('error'),true); 
} 

public static function Map($name, $pattern, array $defaults, array $requirements = array(), array $options = array()) { 
    self::Singleton(); 
    $route = new \lib\route\Route($pattern,$defaults,$requirements,$options); 
    $compiledRoute = $route->Compile(); 
    self::$_compiledRouteCollection->Add($name,$compiledRoute); 
} 

private static function _Process(Request $request, $resetProperties = false) { 
    $routes = array(); 
    $routes = self::$_compiledRouteCollection->routes; 
    foreach($routes as $route) { 
     preg_match($route[0]['regex'], $request->GetRequest(), $matches); 
     if(count($matches)) { 
      array_shift($matches); 
      $args = array(); 
      foreach($route[0]['variables'] as $variable) { 
       $args[$variable] = array_shift($matches); 
      } 
      self::$_controller = (!isset(self::$_controller) || $resetProperties) ? $route[0]['defaults']['controller'] : self::$_controller; 
      self::$_action = (!isset(self::$_action) || $resetProperties) ? $route[0]['defaults']['action'] : self::$_action; 
      self::$_args = (!isset(self::$_args) || $resetProperties) ? $args : self::$_args; 

      return; 
     } 
    } 
    self::$_controller = (!isset(self::$_controller) || $resetProperties) ? $request->GetController() : self::$_controller; 
    self::$_action = (!isset(self::$_action) || $resetProperties) ? $request->GetAction() : self::$_action; 
    self::$_args = (!isset(self::$_args) || $resetProperties) ? $request->GetArgs() : self::$_args; 
} 
} 

request.php

namespace lib; 

class Request { 
protected $_controller; 
protected $_action; 
protected $_args; 
protected $_request; 

public function __construct($urlPath = null) { 
    $this->_request = $urlPath !== null ? $urlPath : $_SERVER['REQUEST_URI']; 

    $parts = explode('/', $urlPath !== null ? $urlPath : $_SERVER['REQUEST_URI']); 
    $parts = array_filter($parts); 

    $this->_controller = (($c = array_shift($parts)) ? $c : 'index').'Controller'; 
    $this->_action = ($c = array_shift($parts)) ? $c : 'index'; 
    $this->_args = (isset($parts[0])) ? $parts : array(); 
} 

public function GetRequest() { 
    return $this->_request; 
} 

public function GetController() { 
    return $this->_controller; 
} 

public function GetAction() { 
    return $this->_action; 
} 

public function GetArgs() { 
    return $this->_args; 
} 
} 

route.php

namespace lib\route; 

class Route { 
private $_pattern; 
private $_defaults; 
private $_requirements; 
public $_options; 

public function __construct($pattern, array $defaults = array(), array $requirements = array(), array $options = array()) { 
    $this->SetPattern($pattern); 
    $this->SetDefaults($defaults); 
    $this->SetRequirements($requirements); 
    $this->SetOptions($options); 
} 

public function SetPattern($pattern) { 
    $this->_pattern = trim($pattern); 

    if($this->_pattern[0] !== '/' || empty($this->_pattern)) { 
     $this->_pattern = '/'.$this->_pattern; 
    } 
} 

public function GetPattern() { 
    return $this->_pattern; 
} 

public function SetDefaults(array $defaults) { 
    $this->_defaults = $defaults; 
} 

public function GetDefaults() { 
    return $this->_defaults; 
} 

public function SetRequirements(array $requirements) { 
    $this->_requirements = array(); 
    foreach($requirements as $key => $value) { 
     $this->_requirements[$key] = $this->_SanitizeRequirement($key,$value); 
    } 
} 

public function GetRequirements() { 
    return $this->_requirements; 
} 

public function SetOptions(array $options) { 
    $this->_options = array_merge(
     array('compiler_class' => 'lib\\route\\RouteCompiler'), 
     $options 
    ); 
} 

public function GetOptions() { 
    return $this->_options; 
} 

public function GetOption($name) { 
    return isset($this->_options[$name]) ? $this->_options[$name] : null; 
} 

private function _SanitizeRequirement($key, $regex) { 
    if($regex[0] == '^') { 
     $regex = substr($regex, 1); 
    } 

    if(substr($regex, -1) == '$') { 
     $regex = substr($regex,0,-1); 
    } 

    return $regex; 
} 

public function Compile() { 
    $className = $this->GetOption('compiler_class'); 
    $routeCompiler = new $className; 

    $compiledRoute = array(); 
    $compiledRoute = $routeCompiler->Compile($this); 
    return $compiledRoute; 
} 
} 

routeCompiler.php

namespace lib\route; 

class RouteCompiler { 

//'#\/tellme\/users\/[\w\d_]+\/[\w\d_]+#' 
public function Compile(Route $route) { 
    $pattern = $route->GetPattern(); 
    $requirements = $route->GetRequirements(); 
    $options = $route->GetOptions(); 
    $defaults = $route->GetDefaults(); 
    $len = strlen($pattern); 
    $tokens = array(); 
    $variables = array(); 
    $pos = 0; 
    $regex = '#'; 

    preg_match_all('#.\{([\w\d_]+)\}#', $pattern, $matches, PREG_OFFSET_CAPTURE | PREG_SET_ORDER); 
    if(count($matches)) { 
     foreach($matches as $match) { 
      if($text = substr($pattern, $pos, $match[0][1] - $pos)) { 
       $regex .= str_replace('/', '\/', $text).'\/'; 
      } 
      if($var = $match[1][0]) { 
       if(isset($requirements[$var])) { 
        $regex .= '('.$requirements[$var].')\/'; 
       } else { 
        $regex .= '([\w\d_]+)\/'; 
       } 
       $variables[] = $match[1][0]; 
      } 
      $pos = $match[0][1] + strlen($match[0][0]); 

     } 
     $regex = rtrim($regex,'\/').'#'; 
    } else { 
     $regex .= str_replace('/', '\/', $pattern).'#'; 
    } 

    $tokens[] = array(
     'regex' => $regex, 
     'variables' => $variables, 
     'defaults' => $defaults 
    ); 
    return $tokens; 
} 
} 

compiledRouteCollection.php

namespace lib\route; 

class CompiledRouteCollection { 
public $routes; 

public function __construct() { 
    $this->routes = array(); 
} 

public function Add($name, array $route) { 
    $this->routes[$name] = $route; 
} 
} 
+0

あなたはsymfony2コンポーネントを使用しません。自分のフロントコントローラをコーディングしました。すばらしいです。しかし、これは何の問題についてのものではありません! – Raffael

+0

@ Raffael1984私はルーティングの基礎としてsymfony 2ルーティングコンポーネントを使用しました。私が答えを得られなかったとき、私はsymfonyルータを私のニーズに合わせて単純化することに決意しました。このように思えますが、ソースコードを見れば、これはどのように動作するのか、それを理解すればフレームワークに組み込む方法を説明します。 – bflemi3

+1

easy pal ...私はあなたと感じています...特定のコンポーネントの現在の文書はまだサブです。ところで、私はあなたのIRCチャンネル上でsf2-stuffについて非常に有能なフィードバックを得ていることを今まで知っていました。 – Raffael

関連する問題