Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

 
Reply to this topicStart new topic
> Nice URLs, Klasa, PHP5
ActivePlayer
post 1.05.2005, 18:45:34
Post #1





Grupa: Przyjaciele php.pl
Postów: 1 224
Pomógł: 40
Dołączył: 6.07.2004
Skąd: Wuppertal

Ostrzeżenie: (0%)
-----


  1. <?php
  2. class Input_Url {
  3.    private static $oInstance = false;
  4.  
  5.    private $url_params = array();
  6.     
  7.     public static function getInstance()
  8.     {
  9.         if ( self::$oInstance == false ) {
  10.             self::$oInstance = new Input_Url();
  11.              } 
  12.         return self::$oInstance;
  13.     } 
  14.  
  15.  
  16.     private function __construct()
  17.     {
  18.         $this->set_params_from_url();
  19.     } 
  20.  
  21.     // wew funkcja pobierająca wartosci do klasy
  22.     private function set_params_from_url()
  23.     {
  24.         $arrParams = array();
  25.         $_SERVER['PATH_INFO']  = isset( $_SERVER['PATH_INFO'] ) ? $_SERVER['PATH_INFO'] : '';
  26.         $arrParams = explode( '/', substr( $_SERVER['PATH_INFO'], 1) );
  27.          foreach ( $arrParams as $wartosc ) {
  28.             $this->url_params[] = $wartosc;
  29.              } 
  30.     } 
  31.  
  32.     // pobiera parametr z paska (w wywolaniu podajemy numer argumentu)
  33.     public function getParam($atrNumber = false)
  34.      {
  35.         if ( !empty( $atrNumber ) ) {
  36.             return $this->url_params[$atrNumber-1];
  37.         } else {
  38.             return null;
  39.         } 
  40.     } 
  41.  
  42.     // generuje url wg wartosci
  43.     public function makeUrl()
  44.     {
  45.         $fArgs = func_get_args();
  46.         $retUrl = '';
  47.          if ( is_array( $fArgs ) ) {
  48.             foreach( $fArgs as $value ) {
  49.                 $retUrl .= '/' . $value;
  50.             } 
  51.             return $_SERVER['SCRIPT_NAME'] . $retUrl;
  52.         } else {
  53.             return null;
  54.         } 
  55.     }  
  56.     //zwraca url, taki jaki mamy w pasku + dodane w argumentach wartosci
  57.     public function makeUrlAdd()
  58.     {
  59.         $fArgs = func_get_args();
  60.         $retUrl = '';
  61.          foreach ( $this->url_params as $wartosc ) {
  62.             $retUrl .= '/' . $wartosc;
  63.         } 
  64.  
  65.         if ( is_array( $fArgs ) ) {
  66.             foreach( $fArgs as $value ) {
  67.                 $retUrl .= '/' . $value;
  68.             } 
  69.             return $_SERVER['SCRIPT_NAME'] . $retUrl;
  70.         } else {
  71.             return null;
  72.         } 
  73.     } 
  74. } 
  75.  
  76. ?>


Napisalem taką oto klase do obsługi easyUrl. prosze o sugestie, co mozna by jeszcez do niej dopisac, czy dziala i wszelkie uwagi. Dziekuje smile.gif

aha, plik .htacces ustawiamy tak jak w artykule na php.pl smile.gif

Update
racja, dodalem komentarze i poprawilem troche tu na forum bo sie rozjechało

Ten post edytował ActivePlayer 5.06.2005, 23:39:22
Go to the top of the page
+Quote Post
bela
post 1.05.2005, 19:18:35
Post #2


Administrator PHPedia.pl


Grupa: Developerzy
Postów: 1 102
Pomógł: 2
Dołączył: 14.09.2003

Ostrzeżenie: (0%)
-----


Twój styl kodowania jest 'delikatnie' nieczytelny winksmiley.jpg


--------------------
Go to the top of the page
+Quote Post
ActivePlayer
post 1.05.2005, 20:47:48
Post #3





Grupa: Przyjaciele php.pl
Postów: 1 224
Pomógł: 40
Dołączył: 6.07.2004
Skąd: Wuppertal

Ostrzeżenie: (0%)
-----


kod updated
Go to the top of the page
+Quote Post
ebe
post 1.05.2005, 20:58:50
Post #4





Grupa: Zarejestrowani
Postów: 150
Pomógł: 1
Dołączył: 23.01.2004

Ostrzeżenie: (0%)
-----


  1. <?php
  2. public function makeUrl()
  3. public function makeUrlAdd()
  4. ?>


trochę za dużo kodu, funkcja makeUrlAdd robi jeszcze raz to samo co makeUrl + dodaje swoje rzeczy, nie lepiej wywołać funkcję makeUrl w makeUrlAdd() ?


--------------------
Słyszałem, że macie tutaj jakieś takie php... fajne to, dobre to jest?
Go to the top of the page
+Quote Post
Ociu
post 2.05.2005, 09:42:21
Post #5





Grupa: Moderatorzy
Postów: 1 566
Pomógł: 37
Dołączył: 14.05.2003
Skąd: Kraków




A jak chce mieć adres http://localhost/sklep/promocja,pokaz,123 ? wtedy kicha smile.gif
  1. <?php
  2. private $dzielnik = '/';
  3. # ...
  4. $arrParams = explode( $this -> dzielnik, substr( $_SERVER['PATH_INFO'], 1) );
  5. #...
  6. $retUrl .= $this -> dzielnik . $value;
  7. #...
  8. $retUrl .= $dzis -> dzielnik . $wartosc;
  9.  
  10. ?>
Go to the top of the page
+Quote Post
in5ane
post 17.05.2007, 19:56:33
Post #6





Grupa: Zarejestrowani
Postów: 1 335
Pomógł: 34
Dołączył: 9.11.2005
Skąd: Wrocław

Ostrzeżenie: (10%)
X----


Sorka, ale mało się orientuje z tymi klasami (wszystkimi) itd..., o co z tym chodzi? Jak to zrobię to przecież nie będę miał od razu "przyjaznych urli".

Jak to wpleść w życie?


--------------------
> > > Tworzenie stron < < <
Go to the top of the page
+Quote Post
Balon
post 18.05.2007, 19:50:33
Post #7





Grupa: Zarejestrowani
Postów: 422
Pomógł: 0
Dołączył: 14.12.2005
Skąd: Wałbrzych

Ostrzeżenie: (0%)
-----


po co tak ?
  1. <?php
  2. public function makeUrl()
  3. {
  4. $fArgs = func_get_args();
  5. $retUrl = '';
  6. if ( is_array( $fArgs ) ) {
  7. foreach( $fArgs as $value ) {
  8. $retUrl .= '/' . $value;
  9. } 
  10. return $_SERVER['SCRIPT_NAME'] . $retUrl;
  11. } else {
  12. return null;
  13. } 
  14. }  
  15. ?>

jak można tak
  1. <?php
  2. public function makeUrl(){
  3. $params = func_get_args();
  4. return is_array( $params ) ? $_SERVER['SCRIPT_NAME']. '/' . implode( '/', $params ) : null;
  5. }
  6. ?>


Cytat
Jak to zrobię to przecież nie będę miał od razu "przyjaznych urli".

Masz w artykułach na php.pl artykuł o tym winksmiley.jpg

Ten post edytował Balon 18.05.2007, 19:55:09


--------------------
Postura wielka i morda wredna, jestem Balon, kozak z osiedla !
bzdury.pl - Królestwo rozrywki
Go to the top of the page
+Quote Post

Reply to this topicStart new topic
1 Użytkowników czyta ten temat (1 Gości i 0 Anonimowych użytkowników)
0 Zarejestrowanych:

 



RSS Wersja Lo-Fi Aktualny czas: 19.07.2025 - 10:21