mod_rewrite urls erstellen

südpol

Erfahrenes Mitglied
Hi,

nachdem ich es nun mit hilfe des Forums geschaft habe mod_rewrite auch auf meinem Server zum laufen zu bringen würde ich gerne eine Funktion in meine Homepage einbauen die mir meine URLs in SUMA freundliche URLs umwandelt. Ich habe mir zu diesem Zweck mal eine kleine Klasse geschrieben. Ich würde nun gerne von euch wissen ob es da bessere / einfachere Ansätze gibt. Zudem habe ich in Kombination mit dieser Klasse gerade ein kleines smarty Problem. Kann es sein, dass man aus smarty raus nicht auf klassen zugreifen kann?

Code:
<?php
/* $ID$ */

/**
 * This class provides several functions regarding the url management 
 * @package jsweb
 * @author Johannes Schmidt
 */
class manageURL {
  var $section = "home"; // selected section 
  var $urlParams = array();  // params for the url string
  
  /**
   * function to generate the URL's used on the website 
   * @param string section array params
   * @return string url
   */
  function genURL($section, $params) {
    $this->urlParams = $params;
    if(SUMA_URLS == true) {
      switch ($section) {
	    case "contact":
		  $this->checkParam("mail");      
	      $url = "/contact/".$this->urlParams['mail']."/index.html";	
	      break;
	    default:
		  // go to error page and send mail	  
	  }
    } else {
	  $url = "index.php?section=".$section."";
	  foreach($params as $key => $value) {
	    $url .= "&amp;".$key."=".$value."";
	  }
    }
	return $url;
  }
  
  /**
   * function to check if the param of the url is set or not
   * @param string paramName 
   */
  function checkParam($paramName) {
    if(isset($this->urlParams[$paramName])) {
	  if($this->urlParams[$paramName] == "") { 
	    // param is empty
	    $this->urlParams[$paramName] == false;
	  }
	} else {
	  // param doesn't exist at all
	  $this->urlParams[$paramName] == false;
	}
  }
}
 
?>

Danke
 
ok, da man ja faul ist :D habe ich die Funktion noch mal überarbeitet - so solle ich meine Homepage jetzt sogar durch ein perl script komplett auf die neuen urls umstellen können ;-)

Code:
<?php
/* $ID$ */

/**
 * This class provides several functions regarding the url management 
 * @package jsweb
 * @author Johannes Schmidt
 */
class manageURL {
  var $section = "home"; // selected section 
  var $urlParams = array();  // params for the url string
  var $paramString = ""; // params as string
  
  /**
   * function to generate the URL's used on the website 
   * @param string section string paramString
   * @return string url
   */
  function genURL($section, $paramString = "") {
    if(SUMA_URLS == true) {
	  $this->paramString = $paramString;
	  $this->urlParams = array();
	  $this->explodeParams();
      switch ($section) {
	    case "contact":
		  $this->checkParam("mail");     
	      $url = "/contact/".$this->urlParams['mail']."/index.html";	
	      break;
	    default:
		  $url = "index.php?section=".$section."".$paramString.""; 
	  }
    } else {
	  $url = "index.php?section=".$section."".$paramString."";
    }
	return $url;
  }
  
  /**
   * function to check if the param of the url is set or not
   * @param string paramName 
   */
  function checkParam($paramName) {
    if(isset($this->urlParams[$paramName])) {
	  if($this->urlParams[$paramName] == "") { 
	    // param is empty
	    $this->urlParams[$paramName] = "0";
	  }
	} else {
	  // param doesn't exist at all
	  $this->urlParams[$paramName] = "0";
	}
  }
  
  /**
   * this functions explodes the param string into singel params
   */
  function explodeParams() {
    if($this->paramString != "") {
      $arr1 = explode("&amp;", $this->paramString);
	  foreach($arr1 as $key => $value) {
	    $arr2 = explode("=", $value, 2);
	    $this->urlParams[$arr2['0']] = $arr2['1'];
	  }
	}
  }
}
 
?>

Die letzte Funktion ist dazu gekommen und zerlegt mir jetzt meinen Parameter String in ein schönes array - so muss ich das nicht bei der Übergabe von Hand machen.

Aus dem o. g. Code erfolgt daher folgende Syntax für den Aufruf:

Code:
$url = $manURL->genURL("contact", "&amp;mail=0");

Die url die zurück kommt sie so aus: /contact/0/index.html

Allerdings bekomme ich diese Funktion noch immer nicht in smarty aufgerufen. Wenn ich die Klasse über eine vorgeschaltete Funktion aufrufe geht es :confused: :suspekt:

Gruß

[edit] und gleich noch ein paar Bugfixes :) [/edit]
 
Zuletzt bearbeitet:
Hi,

*heul* keiner redet mit mir ;)

Dann füge ich eben noch ein paar Funktionen dazu bzw. modifiziere den Aufruf noch ein bisschen...

Code:
<?php
/* $ID$ */

/**
 * This class provides several functions regarding the url management 
 * @package jsweb
 * @author Johannes Schmidt
 */
class manageURL {
  var $section = "home"; // selected section 
  var $urlParams = array();  // params for the url string
  var $paramString = ""; // params as string
  
  /**
   * function to generate the URL's used on the website 
   * @param string section string paramString
   * @return string url
   */
  function genURL($section, $paramString, $anker = "") {
    if(SUMA_URLS == true) {
	  $this->paramString = $paramString;
	  $this->urlParams = array();
	  $this->explodeParams();
      switch ($section) {
	    case "contact":  
	      $url = "/contact/".$this->returnParam['mail']."/index.html".$anker."";	
	      break;
		case "advertisement":
		  $url = "/advertisement/".$this->returnParam['action']."/".$this->returnParam['id']."/".$this->returnParam['forward']."/index.html".$anker."";
		  break;
	    default:
		  $url = "index.php?section=".$section."".$paramString."".$anker.""; 
	  }
    } else {
	  $url = "index.php?section=".$section."".$paramString."".$anker."";
    }
	return $url;
  }
  
  /**
   * this functions returns the requested param if not exists return 0
   * @param string paramName 
   * @return string URLparam
   */
  function returnParam($paramName) {
    if(isset($this->urlParams[$paramName])) {
	  if($this->urlParams[$paramName] == "") { 
	    // param is empty
	    $this->urlParams[$paramName] = "0";
	  }
	} else {
	  // param doesn't exist at all
	  $this->urlParams[$paramName] = "0";
	}
	return $this->urlParams[$paramName];
  }
  
  /**
   * this functions explodes the param string into singel params
   */
  function explodeParams() {
    if($this->paramString != "") {
      $arr1 = explode("&amp;", $this->paramString);
	  foreach($arr1 as $key => $value) {
	    $arr2 = explode("=", $value, 2);
	    $this->urlParams[$arr2['0']] = $arr2['1'];
	  }
	}
  }
}
 
?>
 
Zurück