मैं Django में एक जैसा यूआरएल रूटिंग प्रदान करने का एक तरीका ढूंढ रहा हूं। मैंने बहुत सारे संसाधनों को ऑनलाइन देखा & मुझे cobweb पसंद आया लेकिन समस्या यह है कि मैं पूरे ढांचे का उपयोग नहीं करना चाहता हूं, मैं सिर्फ यूआरएल रीराउटिंग तर्क/कोड का उपयोग करना चाहता हूं। क्या Django- जैसे यूआरएल रूटिंग तर्क के लिए कोई अच्छा संसाधन है?PHP के लिए Django- जैसे यूआरएल रूटिंग
उत्तर
आप CakePHP देख सकते हैं। मुझे नहीं पता कि यूआरएल रूटिंग तर्क को बाकी ढांचे से दूर करना कितना आसान होगा।
मुझे नहीं लगता कि बिल्कुल वही है, लेकिन ज़ेंड फ्रेमवर्क में सुंदर मूल रूटिंग कार्यक्षमता है जिसे आप देखना चाहते हैं। ज़ेंड फ्रेमवर्क एक घटक-आधारित ढांचा है जो आपको पूरी चीज का उपयोग करने के लिए मजबूर नहीं करता है। लेकिन मुझे लगता है कि यदि आप रूटिंग कार्यक्षमता का उपयोग करते हैं तो आपको अपने नियंत्रक तंत्र का भी उपयोग करने की आवश्यकता हो सकती है क्योंकि राउटर शीर्ष पर बनाया गया है।
जो आप खोज रहे हैं वह एक माइक्रोफ्रेमवर्क है। यह मूल रूप से सिर्फ एक ढांचे की रूटिंग परत है। इनमें से कई उपलब्ध हैं। ये मेरे लिए दिलचस्प देखा:
एक है कि वास्तव में मेरे मन हालांकि विस्फोट से उड़ा दिया Silex था, जो Symfony2 के आधार पर और PHP की आवश्यकता है 5.3।
मैंने लिमोनेड माइक्रो PHP फ्रेमवर्क के यूआरएल रूटिंग पर लिमोनेड कोडबेस का उपयोग करके एक उत्तर पोस्ट किया था। हालांकि मैं वापस गया और कोबवेब कोड देखा और यह सुंदर और अच्छा है। तो मैंने अभी कोबवेब के यूआरएल रूटिंग को लिया और दोबारा अपने कोड में उपयोग करने के लिए ओओपी PHP5.x में दोबारा प्रतिक्रिया दी।
तुलना देख के लिए: - http://docs.djangoproject.com/en/dev/topics/http/urls/ बनाम http://www.limonade-php.net/README.htm
सामान्य अस्वीकरण हैं जगह
1 में) मैं बहुत परीक्षण नहीं किया!
2) यह Django के URL मार्ग क्षमताओं
<?php
/**
*
* @author rajeev jha (jha dot rajeev at gmail)
* Django style URL routing.
* Pattern matching logic of this code is copied from cobweb framework
* @see http://code.google.com/p/cobweb/source/browse/trunk/dispatch/url_resolver.class.php
*
*/
class Gloo_Core_Router {
private $table ;
function __construct() {
//initialize routing table
$this->table = new Gloo_Core_RoutingTable();
}
function getRoute($path,$domain=NULL){
if(empty($path)) {
$message = sprintf("Please supply a valid path to match :: got [%s] ", $path);
trigger_error($message,E_USER_ERROR);
}
//all rules for this domain
$rules = $this->table->getRules($domain);
$route = NULL ;
if($path == '/')
$route = $this->matchHome($rules);
else
$route = $this->match($rules,$path);
return $route ;
}
private function matchHome($rules) {
$route = NULL ;
foreach($rules as $rule) {
if($rule["pattern"] == '/') {
$route = $this->createRoute($rule,array());
}
}
return $route ;
}
private function match($rules,$path) {
$path = ltrim($path, '/');
$matches = array();
$route = NULL ;
foreach($rules as $rule) {
if(preg_match($this->patternize($rule["pattern"]),$path,$matches) != 0) {
//match happened
$matches = $this->sanitizeMatches($matches);
$route = $this->createRoute($rule,$matches);
}
}
return $route ;
}
private function createRoute($rule,$matches) {
$route = $rule ;
//add parameters
$route["params"] = $matches ;
return $route ;
}
private function sanitizeMatches($matches){
//discard the first one
if (count($matches) >= 1)
$matches = array_splice($matches,1);
$unset_next = false;
//group name match will create a string key as well as int key
// like match["token"] = soemthing and match[1] = something
// remove int key when string key is present for same value
foreach ($matches as $key => $value) {
if (is_string($key)){
$unset_next = true;
} else if (is_int($key) && $unset_next) {
unset($matches[$key]);
$unset_next = false;
}
}
return array_merge($matches);
}
private function patternize($pattern) {
//http://www.php.net/manual/en/reference.pcre.pattern.modifiers.php
//treat pattern as UTF-8
return '{'.$pattern.'}u' ;
}
}
?>
<?php
/**
*
* @author rajeev jha (jha dot rajeev at gmail)
* Django style URL routing.
* URL routing table
*
*/
class Gloo_Core_RoutingTable {
private $rules ;
private $splrules ;
const ANY_DOMAIN = '__ANY__' ;
function __construct() {
$this->rules = array();
$this->splrules = array();
//@todo inject from outside
$this->createRule(self::ANY_DOMAIN, '/', 'Gloo_Controller_Home');
//match alphanumeric + dashes
//a pcre word (\w) does not contain dashes
$this->createRule(self::ANY_DOMAIN, '^(?P<token>[-\w]+)$','Gloo_Controller_Post');
$this->createRule(self::ANY_DOMAIN, '^page/(?P<pagenum>\d+)$','Gloo_Controller_Home');
$this->createRule(self::ANY_DOMAIN, '^(?P<token>\w+)/page/(?P<pagenum>\d+)$','Gloo_Controller_Post');
$this->createRule(self::ANY_DOMAIN, '^category/(?P<name>\w+)$','Gloo_Controller_Category');
$this->createRule(self::ANY_DOMAIN, '^category/(?P<name>\w+)/page/(?P<pagenum>\d+)$','Gloo_Controller_Category');
//special rules
$this->createRule('www.test1.com','^(?P<token>\w+)$','Gloo_Controller_File', array("template" => "post.php"));
}
function createRule($domain,$pattern,$action,$options=NULL) {
if(empty($domain)) {
trigger_error("No domain supplied for rule" ,E_USER_ERROR);
}
$rule = array();
$rule["pattern"] = $pattern;
$rule["action"] = $action ;
//Add options
if(is_null($options))
$rule["options"] = array();
else
$rule["options"] = $options ;
$rule["domain"] = $domain ;
//add to generic or domain specific rules
if($domain == self::ANY_DOMAIN)
$this->rules[] = $rule ;
else
$this->splrules[$domain][] = $rule ;
}
function getRules($domain) {
if(empty($domain))
return $this->rules ;
//valid domain - rules as well
// add to existing rules
if(array_key_exists($domain,$this->splrules)) {
$splrules = $this->splrules[$domain];
$rules = array_merge($this->rules,$splrules);
return $rules ;
} else {
return $this->rules ;
}
}
}
?>
के सभी अब नहीं हो सकता है, आप रूटिंग तालिका प्रारंभ करने के लिए किसी भी नियम जोड़ सकते हैं। राउटर को आपके द्वारा प्रदान किए जाने वाले डोमेन + "पथ" के आधार पर आप "नामांकित समूह" पैरामीटर और अन्य मिलान पैरामीटर के साथ "नियंत्रक" स्ट्रिंग को वापस ले लेंगे। मैं "नियंत्रक" तारों के लिए क्लास इंस्टेंटेशन लॉजिक शामिल नहीं कर रहा हूं क्योंकि 1) जटिल 2) यदि आपका इरादा सिर्फ डीजेंगो को यूआरएल रूटिंग की तरह प्राप्त करना है तो यह काफी अच्छा है।
संदर्भ
Limonade कोड यहाँ है: - https://github.com/sofadesign/limonade/blob/master/lib/limonade.php
मकड़ी का जाला कोड यहाँ है: - http://code.google.com/p/cobweb/source/browse/trunk/dispatch/url_resolver.class.php
- मेरे कोड यहाँ है: - https://code.google.com/p/webgloo/source/browse/trunk/php/lib/Gloo/Core/Router.php
- के पुराने संस्करण मेरी कोड: - जो मैंने यहां संपादित किया - लिमोनेड पर आधारित था और यह काफी मस्तिष्क क्षतिग्रस्त था। उपरोक्त Limonade लिंक देखने के लिए स्वतंत्र महसूस करें। मैं इसे हटा रहा हूँ।
प्रयोग
$ रूटर = नए Gloo_Core_Router();
$path = 'category/news/page/3' ;
printf("Now matching path [%s]" , $path);
$route = $router->getRoute($path);
print_r($route);
$path = '/category/Health/page' ;
printf("Now matching path [%s]" , $path);
$route = $router->getRoute($path);
print_r($route);
आप वर्ग नाम बदलने के लिए है और निर्भरता शामिल हैं, लेकिन हे, आप एक प्रोग्रामर हैं; डी
मैं CodeIgniter और Django संसाधनों के साथ एक समान ढांचे का विकास किया, http://williamborba.github.io/willer/quick_start/
यह अभी भी बीटा है , लेकिन मैं धीरे-धीरे URL.php फ़ाइल को कार्यान्वित और सुधार रहा हूं URLS.py django के समान है।
अन्य हाइलाइट मॉडल और ओआरएम डीजेंगो की तरह हैं, लेकिन कोडिनेटर सक्रिय रिकॉर्ड के समान कुछ है।
Hmm.ok. मैं नियंत्रक का उपयोग करने से बचने की उम्मीद कर रहा था। – Chantz