2011-01-18 11 views
2

나는 작업하고있는 소셜 네트워크 유형 프로젝트에서 올바른 페이지를 라우팅하고 라우팅하기위한 좋은 해결책을 찾고 있습니다. 나는 모든 라우팅을 하나의 진입 점 파일을 통해 실행하기로 결정했다. 나는 이것을 위해 기존의 틀을 사용하고 싶지 않다. 아래의 샘플 코드를 검토하면 페이징이 필요한 모듈의 경우 모듈이 붙어있는 경우를 제외하고는 트래픽이 상당히 많은 사이트에서 성능이 상당히 좋아야한다는 것을 알 수 있습니다. 이 점의 핵심은 사이트의 모든 곳에서 깨끗한 URL을 찾는 것입니다.URL에서 페이지 번호를 얻고이를 어떻게 포함합니까?

URL에 페이징 번호가있는 경우 페이징 작업을 수정하도록 도와주십시오.

(ie; domain.com/blogs/comments/234) 

htaccess로 파일

RewriteEngine On 
RewriteCond %{REQUEST_FILENAME} !-d 
RewriteCond %{REQUEST_FILENAME} !-f 
RewriteRule ^(.*)$ test.php?uri=$1 [NC,L,QSA] 

<?PHP 

//get url from URL 
$uri = isset($_GET['uri']) ? $_GET['uri'] : null; 

// Array of possible pages to include 
$pageId = array( 
       'account' => 'modules/account/index.php', 
       'account/create' => 'modules/account/create.php', 
       'account/settings' => 'modules/account/settings.php', 
       'account/login' => 'modules/account/login.php', 
       //example of a URL which will need paging.... 
       'users/online/' => 'modules/users/online.php', 
       'users/online/12' => 'modules/users/online.php?page=12' // not sure if including a page like this is even possible? 
       ); 

// If $_GET['uri'] exist and there is a valid key/value for it in the $pageId array then include the file 
if($uri !== null) { 
    if (isset($pageId[$uri])) { 
     require_once $pageId[$uri]; 
    } 
    else { 
     require_once 'home.php'; 
    } 
} 
else { 
    require_once 'home.php'; 
} 

?> 

답변

1

파일 test.php 난 당신이 여기에 몇 가지 조기 최적화를하고있다 생각합니다. 트래픽이 많은 웹 사이트의 경우 아파치 설정에 관한 quora에 대한 토론을 살펴보십시오. 나는 그것이 당신을 도울 것 같아요. >replacement 배열 - regex에 오늘로>page - 그렇다 당신은 아마 당신의 key에서 $pageId 배열을 의미를 변경해야한다고에서

http://www.quora.com/What-is-the-best-apache-conf-configuration-for-a-webserver-whose-handle-100-request-per-second-with-PHP-APC-enabled-and-htaccess-image-compression?q=requests+per+secont

. 이와 유사한

뭔가 :

$pageId = array( 
       'account' => 'modules/account/index.php', 
       'account/create' => 'modules/account/create.php', 
       'account/settings' => 'modules/account/settings.php', 
       'account/login' => 'modules/account/login.php', 
       //example of a URL which will need paging.... 
       'users/online/' => 'modules/users/online.php', 
       'users/online/(\d+)' => 'modules/users/online.php?page=$1' 
       ); 

그리고 라우팅 코드

당신이 배열이 정규 표현식을 구축하고 URL을 일치 반복한다. 일치하면 교체를 수행하고 호출 할 페이지를 얻습니다. 그러나 이것이 곧바로 이루어지면 라우팅 성능이 저하 될 것입니다. (프레임 워크가 많은데 이미 알고 있습니다. 당신이 빌릴 수있는 것을보기 위해 거기를 살펴 봐야한다고 생각합니다 :).

+0

제안 해 주셔서 감사합니다. 프레임 워크에서 제안하는 부분이있어 찾고자하는 부분을 쉽게 찾을 수 있습니까? – JasonDavis

+0

이 글을 살펴보십시오. http://stackoverflow.com/questions/115629/simplest-php-routing-framework –

관련 문제