link typu: localhost/index.php/modu³/metoda/parametr/parametr.html
Pisane z palca, mo¿e zawieraæ b³edy.

  1. <?php
  2. define('MAIN_MODULE', 'news');
  3. define('MAIN_METHOD', 'display');
  4. define('ADDRESS', 'http://localhost/index.php/');
  5. class URL {
  6.             /*
  7.             * Module
  8.             * @access public
  9.             * @var string
  10.             */        
  11.             public $module;
  12.  
  13.             /*
  14.             * Method
  15.             * @access public
  16.             * @var string
  17.             */    
  18.             public $method;
  19.  
  20.             /*
  21.             * Values
  22.             * @access public
  23.             * @var array
  24.             */    
  25.             public $vars = array();
  26.  
  27.             /*
  28.             * Dot
  29.             * @access private
  30.             * @var string
  31.             */    
  32.             private $dot;
  33.  
  34.             /*
  35.             * Using GET ?
  36.             * @access private
  37.             * @var bool
  38.             */    
  39.             private $uGET;
  40.  
  41.             /*
  42.             * link end
  43.             * @access private
  44.             * @var string
  45.             */    
  46.             private $end;
  47.  
  48.                 /**
  49.                 * Construct
  50.                 * @param string $dot
  51.                 * @param string $end
  52.                 * @param bool $user_get
  53.                 * @access public
  54.                 * @return void
  55.                 */
  56.                 public function __construct($dot, $end, $use_get = false) {
  57.                         $this->dot = $dot;
  58.                         $this->end = $end
  59.                         $this->uGET = false;
  60.                         $this->setParams();
  61.                 }
  62.  
  63.                 /**
  64.                 * Set params
  65.                 * @param void
  66.                 * @access private
  67.                 * @return void
  68.                 */
  69.                 private function setParams()
  70.                 {
  71.                     if(!$this->uGET) {
  72.                         $arrParams = array();
  73.                         $s  = isset( $_SERVER['PATH_INFO'] ) ? $_SERVER['PATH_INFO'] : '';
  74.                         $arrParams = explode( $this->dot, substr( $s, 1, -count($this->end)) );
  75.                         foreach ( $arrParams as $wartosc ) {
  76.                             $this->vars[] = $wartosc;
  77.                     }            
  78.                     
  79.                     } else {
  80.                         foreach($_GET AS $value) {
  81.                             $this->vars[] = $value;
  82.                         }
  83.                     }
  84.                     
  85.                     $this->setModule();
  86.                     $this->setMethod();
  87.                     
  88.                     $this->setValues(); 
  89.                 }
  90.  
  91.                 /**
  92.                 * Set module
  93.                 * @param void
  94.                 * @access public
  95.                 * @return void
  96.                 */                
  97.                 public function setModule() {
  98.                     $this->module = (isset($this->vars[0])) ? $this->vars[0] : MAIN_MODULE;
  99.                 }
  100.  
  101.                 /**
  102.                 * get module
  103.                 * @param void
  104.                 * @access public
  105.                 * @return void
  106.                 */                
  107.                 public function getModule() {
  108.                     return $this->module;
  109.                 }
  110.  
  111.                 /**
  112.                 * Set method
  113.                 * @param void
  114.                 * @access public
  115.                 * @return void
  116.                 */                
  117.                 public function setMethod() {
  118.                     $this->method = (isset($this->vars[1])) ? $this->vars[1] : TMAIN_METHOD;
  119.                 }
  120.  
  121.                 /**
  122.                 * get method
  123.                 * @param void
  124.                 * @access public
  125.                 * @return void
  126.                 */                
  127.                 public function getMethod() {
  128.                     return $this->method;
  129.                 }
  130.  
  131.                 /**
  132.                 * Set values
  133.                 * @param void
  134.                 * @access public
  135.                 * @return void
  136.                 */                
  137.                 private function setValues() {
  138.                     unset($this->vars[0]);
  139.                     unset($this->vars[1]);
  140.                     sort($this->vars); # mozna uzyc array_values
  141.                     
  142.                 }
  143.  
  144.                 /**
  145.                 * get all values
  146.                 * @param void
  147.                 * @access public
  148.                 * @return array
  149.                 */                
  150.                 public function getValues() {
  151.                     return $this->vars;
  152.                 }
  153.  
  154.                 /**
  155.                 * Construct
  156.                 * @param int $VNumber
  157.                 * @access public
  158.                 * @return string
  159.                 */                
  160.                 public function getValue( $VNumber ) {
  161.                     return $this->vars[$VNumber];
  162.                 }
  163.                 
  164.                 /**
  165.                 * Count values
  166.                 * @param void
  167.                 * @access public
  168.                 * @return int
  169.                 */
  170.                 public function getCValues() {
  171.                     return count($this->vars);
  172.                 }
  173.                 /**
  174.                 * Make url
  175.                 * @param array $urlArg
  176.                 * @access public
  177.                 * @return void
  178.                 */
  179.                 
  180.                 public function makeUrl( $urlArg ) {
  181.                     if($this->uGET == FALSE) {
  182.                         $url = ADDRESS . join($this->dot, $urlArg) .'.html';
  183.                     }else {
  184.                         foreach($urlArg AS $name => $value) {
  185.                             $aURL[] = '?' . $name .'='. $value;
  186.                         }
  187.                         $url= ADDRESS . substr(join($this->dot, $urlArg), 0, 1);
  188.                     }
  189.                     header('location: ' $url);
  190.                 }
  191.                 
  192.         }
  193. ?>


U¿ycie:
  1. <?php
  2. $url = new URL('/', '.html');
  3. $url->makeUrl(array('comments', 'display', 'blog', 'id', '47'));
  4. ?>


pozdrawiam