|
 |
|
ขอวิธี เอาค่า absolute และ relative link ของ php ครับ |
|
 |
|
|
 |
 |
|
Code (PHP)
หลายๆ คนคงรู้จักกับ mod_rewrite ซึ่งเป็น module ของ apache ซึ่งตัวนี้น่ะ มันมีประโยชน์ค่อนข้างมากในการทำ url friendly เพื่อให้ google รัก yahoo หลง
ซึ่งโปเกม่อน หลายคนอาจจะเขียนตรงๆ ไปเลยเช่น
1. RewriteCond %{REQUEST_FILENAME} !-f
2. RewriteCond %{REQUEST_FILENAME} !-d
3. RewriteRule ^tag\/([^\?].*)$ n5-tag.php?tag=$1 [L]
RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule ^tag\/([^\?].*)$ n5-tag.php?tag=$1 [L]
แบบนี้ แล้วทีนี้ถ้าเวบเรามีขนาดใหญ่ module เป็นร้อยๆ ไฟล์ .htaccess ของรามีหวังยาวเป็น ร้อยๆ บรรทัดเป็นแน่ เอางี้ดีกว่าลองมาทำตามวิธีผม ไฟล์เดียวครอบจักรวาล
1. ไฟล์ .htaccess
1. RewriteEngine On
2. RewriteCond %{REQUEST_FILENAME} -f [OR]
3. RewriteCond %{REQUEST_FILENAME} -d
4. RewriteRule ^(.+) - [PT,L]
5. RewriteRule ^(.*) index.php
RewriteEngine On RewriteCond %{REQUEST_FILENAME} -f [OR] RewriteCond %{REQUEST_FILENAME} -d RewriteRule ^(.+) - [PT,L] RewriteRule ^(.*) index.php
จบแล้วครับไฟล์ .htaccess เราใช้แค่นี้ คือบอกมันว่า "ไม่ว่าจะเจออะไร วิ่งมาที่ index.php ให้หมดเลยนะ"
2. ไฟล์ common.inc.php
1. // Relative path
2. $conf['dir'] = str_replace('\\', '/', dirname(__FILE__));
3. // Absolute path
4. $conf['path'] = "http://".$_SERVER['SERVER_NAME'].str_replace($_SERVER['DOCUMENT_ROOT'], '', $conf['dir']);
5. // uri แบบที่ตัด root folder ออก
6. $conf['uri'] = str_replace($conf['dir'].'/', '', $_SERVER['DOCUMENT_ROOT'].$_SERVER['REQUEST_URI']);
7. // explode เพื่อตัดข้อมูลหลังจาก ? ออกไป
8. list($xuri) = explode('?', $conf['uri'], 2);
9. // explode เพื่อสร้าง array โดยการ แยก ที่ตำแหน่ง /
10. $uri = explode("/", $xuri);
// Relative path $conf['dir'] = str_replace('\\', '/', dirname(__FILE__)); // Absolute path $conf['path'] = "http://".$_SERVER['SERVER_NAME'].str_replace($_SERVER['DOCUMENT_ROOT'], '', $conf['dir']); // uri แบบที่ตัด root folder ออก $conf['uri'] = str_replace($conf['dir'].'/', '', $_SERVER['DOCUMENT_ROOT'].$_SERVER['REQUEST_URI']); // explode เพื่อตัดข้อมูลหลังจาก ? ออกไป list($xuri) = explode('?', $conf['uri'], 2); // explode เพื่อสร้าง array โดยการ แยก ที่ตำแหน่ง / $uri = explode("/", $xuri);
ส่วนนี้เป็นการเอา absolute path มาแยกออกเป็น array เพื่อที่จะนำไปใช้
3. ไฟล์ index.php
1. require('common.inc.php');
2. // ถ้าไม่เจอข้อมูลอะไรให้เรียก home/ เป็น default
3. $loadmodule = ($uri[0]) ? $uri[0] : 'home';
4. $inc = 'modules/'.$loadmodule;
5. // เช็คดูว่าถ้ามี module นี้จริงๆ ให้ include เข้ามา
6. if (is_dir($inc)) {
7. include($inc.'/compile.php');
8. }
require('common.inc.php'); // ถ้าไม่เจอข้อมูลอะไรให้เรียก home/ เป็น default $loadmodule = ($uri[0]) ? $uri[0] : 'home'; $inc = 'modules/'.$loadmodule; // เช็คดูว่าถ้ามี module นี้จริงๆ ให้ include เข้ามา if (is_dir($inc)) { include($inc.'/compile.php'); }
ไฟล์เรียกใช้งานโดยมีการ inc common.inc.php เข้ามาใช้ ทีนี้เราก็สามารถเอา array ต่างๆ ไปเรียกใช้ได้ตามสะดวกแล้วครับ เช่น
http://www.domain.com/blog/tee/addnew
แบบนี้เราก็จะได้ array ออกมา 3 ชุดคือ
$uri[0] จะได้ blog
$uri[1] จะได้ tee
$uri[2] จะได้ addnew
ซึ่งพวกนี้เราสามารถนำไปใช้ใน เงื่อนไข ได้ตามปกติเลย เช่น
1. if ($uri[0] == 'blog') {
2. //load blog module
3. include('modules/blog/compile.php');
4. }
if ($uri[0] == 'blog') { //load blog module include('modules/blog/compile.php'); }
แล้วใน modules/blog/compile.php เรายังสามารถเรียก action ต่างๆ ได้อีกเช่น
1. switch($uri[2]) {
2. case 'addnew' :
3. // do something
4. break;
5. default :
6. // do somthing else
7. break;
8. }
อ่านอันนี้แล้วทำความเข้าใจ นะครับ ที่มาครับ http://php.deeserver.net/webboard/odshow.php?No=38430&rep=6
|
 |
 |
 |
 |
Date :
2010-09-13 17:02:02 |
By :
SOUL |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ขอบคุณครับ
|
 |
 |
 |
 |
Date :
2010-09-13 20:07:51 |
By :
ความรู้เท่าหางอึ่ง |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
|
|
|

|
Load balance : Server 01
|