2014-02-07 2 views
0

나는 PHP로 내 자신의 프레임 워크를 만들고있다.
URL에 따라 프레임 워크를 작동하는 것 외에는 모두 계획대로 진행됩니다.
나는 이것을 이해할 수 없다 :
이 같은 URL에서 www.mydomain.com/folderone/foldertwo/index.php
젠드 프레임 워크 URL로드에서 정상 URL 로딩
www.mydomain.com/folderone(controller)/folder2(action)/variables

젠드 프레임 워크처럼 URL을 읽거나 렌더링하는 방법

것 내가 로직 것을 만드는 방법?
내가 무엇이 누락 되었습니까?
저는이 프레임 워크를 만드는 데 전념하고 있습니다.

+0

이 유 젠드 프레임 워크 소스에서 찾고 시도 않은 새 기본 프레임 워크 키트 즐겨? – emaillenin

+0

나는 zend 프레임 워크에서 어떻게 수행되고 있는지 이해할 수 없었다. – Justin

답변

0

젠드 프레임 워크와 대부분의 MVC 프레임 워크는 부트 스트랩을 사용합니다. 하나 개의 파일에

이 경로의 URL (사용 htaccess로는)의처럼 뭔가를 사용하여 (index.php를) 가정 해 봅시다 : 부트 스트랩과 필요한 어떤 데이터 걸리는 라우팅 클래스를로드

RewriteEngine on 
RewriteRule !\.(js|ico|gif|jpg|png|css|html)$ index.php 

URL에서.

라우팅, 컨트롤러 (베이스 URL 이후에 제공되는 URI 의 일부) URI 끝점을 복용하고있는 모듈을 확인 을 파라미터로 분해하는 과정이다 ZEND 설명서에 따르면

해당 컨트롤러 의 작업이 요청을 받아야합니다. 이 값은 모듈, 컨트롤러, 액션 및 기타 매개 변수입니다. 라우팅은 한 번만 발생합니다. 요청이 처음 수신되고 첫 번째 컨트롤러가 이 발송되기 전에 라우팅이 발생합니다.

편집 : 귀하의 코멘트에 대한 답변 : 멀리 젠드 프레임 워크에서 , 즉 새로운 PHP 파일에 테스트하는 코드이다 : 당신이 라우터를 구축하려는 경우

<?php 
    $url = 'http://test.com/test1/test2/news.php'; 
    $parse = parse_url($url); 
    $tokens = explode("/", $parse[path]); 
    print_r($tokens); 
?> 
+0

덕분에, 이것은 나에게 의미가있다. 이제는 URL의 나머지를 도메인 이름 뒤에 변수로 바꾸는 방법을 알아야한다. 이거 뭐라도 권할 수 있니? – Justin

+0

테스트 할 코드를 추가했습니다. –

0

, 당신이 볼 수 있었다 예제를 제공하기 위해 Glue PHP. 라우팅 부분 만 수행하는 마이크로 프레임 워크입니다.

2

프레임 워크를 설정 한 것과 같은 작업이있었습니다. 이것은 나를위한 솔루션 작업입니다.

먼저 .htaccess 파일을 만듭니다. 재 작성 조건을 설정하고 템플릿 경로를 제외하십시오. 당신은 (단지 & 붙여 넣기 복사) 그런 식으로 작업을 수행 할 수 있습니다 내가 앞서 가기 전에

RewriteEngine On 

Options +Indexes 
Options +FollowSymLinks 

RewriteCond %{REQUEST_FILENAME} !-f 
RewriteCond %{REQUEST_FILENAME} !-d 
RewriteCond %{DOCUMENT_ROOT}/index\.php -f 
RewriteCond %{DOCUMENT_ROOT}/template -d 

RewriteRule ^(.*)$ index\.php?$1 [QSA] 

, 우리는 다섯 디렉토리 최소한 작성해야합니다 : 이제

/var/www/models/ 
/var/www/controllers/ 
/var/www/classes/ 
/var/www/system/ 
/var/www/template/ 

을, 나는에 자동 로딩을 추가 내 index.php :

<?php 
error_reporting(E_ALL^E_NOTICE); 
session_start(); 

function autoload($class_name) 
{ 
    $autoloadDirs = array('models', 'classes', 'controllers'); 

    foreach($autoloadDirs as $dir) 
    { 
     if(file_exists($dir.'/'.$class_name.'.php')) 
     { 
      require_once($dir.'/'.$class_name.'.php'); 
     } 
    } 
} 

spl_autoload_register('autoload'); 

require_once('system/Calling.php'); 

Calling::Run(); 

?> 

위 스크립트에서 여러분은 호출 중 require_once를 볼 수 있습니다.

//-> Load the main controller with default action 
www.example.com/ 
//-> Load the main controller with default action 
www.example.com/main/default/ 
//-> Load the main controller with second action 
www.example.com/main/second/ 
//-> Load the main controller with second action and gives two params 
www.example.com/main/second/key1/value1/key2/value2/ 

: PHP는

class Calling 
{ 
    public static function Run($querystring = null) 
    { 
     //1. Parameter = Contollername 
     //2. Parameter = Action 
     $qString = preg_replace('/(\/$|^\/)/','',$querystring === null ? $_SERVER['QUERY_STRING'] : $querystring); 
     $callParam = !empty($qString) ? explode('/', $qString) : array(); 

     $controllerName = count($callParam) > 0 ?  (class_exists(ucfirst($callParam[0]).'Controller') ? ucfirst(array_shift($callParam)) : 'Error') : 'Main'; 
     //All controllers have suffix "Controller" -> NameController.php 
     //and class name ike NameController 
     //If no controller name given, use MainController.php 
     $controllerClassName = $controllerName.'Controller'; 
     //All public methods have suffix "Action" -> myMethodAction 
     //If there is no method named, use DefaultAction 
     $actionName = count($callParam) > 0 && method_exists($controllerClassName, ucfirst($callParam[0]).'Action') ? ucfirst(array_shift($callParam)) : 'Default'; 
     $actionFunctionName = $actionName.'Action'; 

     //Fetch the params 
     $param = new stdClass(); 
     for($i = 0; $i < count($callParam); $i += 2) 
     { 
      $param->{$callParam[$i]} = isset($callParam[$i + 1]) ? $callParam[$i+1] : null; 
     } 

     //////////////////////////////////////////////////////////// 
     //Init the Controller 
     $controller = new $controllerClassName($controllerName, $actionName); 
     $controller->$actionFunctionName($param); 
     //////////////////////////////////////////////////////////// 
     //If you adapt this code: Is up to you to extends your controller 
     //from an internal controller which has the method Display(); 
     $controller->Display(); 
    } 
} 

또한, 컨트롤러 디렉토리에 당신이 그와 같은 컨트롤러, 방법 및 PARAMS를 호출 할 수 있습니다,

//--> just better if you have also an internal controller with your global stuff 
//--> class MainController extends Controller 
class MainController 
{ 

    /** This is the default action 
    * @param $params 
    * @access public 
    * @return 
    */ 
    public function DefaultAction(stdClass $params) 
    { 
      //-> Do your staff here 
    } 

    /** This is the second action 
    * @param $params 
    * @access public 
    * @return 
    */ 
    public function SecondAction(stdClass $params) 
    { 
      //-> Do your staff here 
    } 

    /** This is the view handling method which has to run at least 
    * and I recommend to set up an internal controller and to extend 
    * all other controller from it and include this method in your 
    * internal controller 
    * @param 
    * @access 
    * @return 
    */ 
    public function Display() 
    { 
     //-> Run your template here 
    } 
?> 

이제 첫 번째 컨트롤러 namend MainController.php를 추가 이제 자신 만의 프레임 워크를 시작하기 위해 다음 파일과 디렉토리를 갖게됩니다.

/var/www/.htaccess 
/var/www/index.php 
/var/www/controllers/MainController.php 
/var/www/system/Calling.php 

/var/www/models/ 
/var/www/classes/ 
/var/www/template/ 

관련 문제