문제

자바에서는 다음과 같이 쓸 수 있는 기본적인 JSP index.jsp 그래서 다음과 같:

<% request.getRequestDispatcher("/home.action").forward(request, response); %>

이것의 효과는 사용자가 요청 index.jsp (또는 그냥을 포함하는 가정 디렉토리 index.jsp 은 기본 문서 디렉토리)을 볼 수 있습니다 home.action 없이는 브라우저,즉는[forward](http://java.sun.com/javaee/5/docs/api/javax/servlet/RequestDispatcher.html#forward(javax.servlet.ServletRequest,%20javax.servlet.ServletResponse)) 일어나 서버 사이드에서 작동합니다.

을 할 수 있습니 비슷한 PHP?내가 심는 것이 가능하도록 아파치를 설정할 수은 이러한 사례를 지능적으로 처리하지만,이후에 액세스하지 못할 수 있습 관련 아파치 구성 내가 관심이있을 거라고의 솔루션을 사용하는 PHP 혼자입니다.

도움이 되었습니까?

해결책

컬 가용성에 대해 걱정하는 경우 사용할 수 있습니다. file_get_contents() 그리고 스트림. 다음과 같은 기능 설정 :

function forward($location, $vars = array()) 
{
    $file ='http://'.$_SERVER['HTTP_HOST']
    .substr($_SERVER['REQUEST_URI'],0,strrpos($_SERVER['REQUEST_URI'], '/')+1)
    .$location;

    if(!empty($vars))
    {
         $file .="?".http_build_query($vars);
    }

    $response = file_get_contents($file);

    echo $response;
}

이것은 단지 얻을 수 있지만 다음과 함께 게시물을 수행 할 수 있습니다. file_get_contents() 또한.

다른 팁

트릭에 대한 요청을 합니다.앞으로는 깨끗하고,새로운 요청을 하려는 작업.따라서 당신은 없 residu 에서 현재 요청,그리고 예를 들어,아무 문제가에 의존하는 스크립트는 자바의 eq$_SERVER['REQUEST_URI']는 무언가이다.

당신의 마음에 드는 컬 클래스와 쓰기에 간단한 기능을 수행하십시오:

<?php 
/**
 * CURLHandler handles simple HTTP GETs and POSTs via Curl 
 * 
 * @author SchizoDuckie
 * @version 1.0
 * @access public
 */
class CURLHandler
{

    /**
     * CURLHandler::Get()
     * 
     * Executes a standard GET request via Curl.
     * Static function, so that you can use: CurlHandler::Get('http://www.google.com');
     * 
     * @param string $url url to get
     * @return string HTML output
     */
    public static function Get($url)
    {
       return self::doRequest('GET', $url);
    }

    /**
     * CURLHandler::Post()
     * 
     * Executes a standard POST request via Curl.
     * Static function, so you can use CurlHandler::Post('http://www.google.com', array('q'=>'belfabriek'));
     * If you want to send a File via post (to e.g. PHP's $_FILES), prefix the value of an item with an @ ! 
     * @param string $url url to post data to
     * @param Array $vars Array with key=>value pairs to post.
     * @return string HTML output
     */
    public static function Post($url, $vars, $auth = false) 
    {
       return self::doRequest('POST', $url, $vars, $auth);
    }

    /**
     * CURLHandler::doRequest()
     * This is what actually does the request
     * <pre>
     * - Create Curl handle with curl_init
     * - Set options like CURLOPT_URL, CURLOPT_RETURNTRANSFER and CURLOPT_HEADER
     * - Set eventual optional options (like CURLOPT_POST and CURLOPT_POSTFIELDS)
     * - Call curl_exec on the interface
     * - Close the connection
     * - Return the result or throw an exception.
     * </pre>
     * @param mixed $method Request Method (Get/ Post)
     * @param mixed $url URI to get or post to
     * @param mixed $vars Array of variables (only mandatory in POST requests)
     * @return string HTML output
     */
    public static function doRequest($method, $url, $vars=array(), $auth = false)
    {
        $curlInterface = curl_init();

        curl_setopt_array ($curlInterface, array( 
            CURLOPT_URL => $url,
            CURLOPT_CONNECTTIMEOUT => 2,
            CURLOPT_RETURNTRANSFER => 1,
            CURLOPT_FOLLOWLOCATION =>1,
            CURLOPT_HEADER => 0));

        if (strtoupper($method) == 'POST')
        {
            curl_setopt_array($curlInterface, array(
                CURLOPT_POST => 1,
                CURLOPT_POSTFIELDS => http_build_query($vars))
            );  
        }
        if($auth !== false)
        {
              curl_setopt($curlInterface, CURLOPT_USERPWD, $auth['username'] . ":" . $auth['password']);
        }
        $result = curl_exec ($curlInterface);
        curl_close ($curlInterface);

        if($result === NULL)
        {
            throw new Exception('Curl Request Error: '.curl_errno($curlInterface) . " - " . curl_error($curlInterface));
        }
        else
        {
            return($result);
        }
    }

}

덤프 이 class.CURLHandler.php 하고 당신이 할 수 있다:

당연히 사용하여,$_REQUEST 는지 정말로 안전하(확인해야 합$_SERVER['REQUEST_METHOD'])그러나 당신은 지점입니다.

<?php
include('class.CURLHandler.php');
die CURLHandler::doRequest($_SERVER['REQUEST_METHOD'], 'http://server/myaction', $_REQUEST);
?>

당연히,컬 설치되어 있지 않은 어디에나 하지만 우리는 native PHP 컬 에뮬레이터입니다.

또한,이것은 당신에게 더 많은 유연성보다는 요청을 합니다.로 전달할 수 있도 및 후 출력입니다.

가장 가까운 유사한 방법 중 하나는 가상() PHP를 Apache 모듈로 실행하는 동안 작동합니다.

가상() mod_include에서 유사한 Apache- 특이 적 함수입니다. 아파치 하위 요청을 수행합니다.

Zend Framework와 같은 MVC를 사용하는 경우 컨트롤러 동작을 변경하거나 컨트롤러 동작 사이를 점프 할 수 있습니다. 이 방법은 _forward AS입니다 여기에 설명되어 있습니다.

이 시도.

function forward($page, $vars = null){
    ob_clean();
    include($page);
    exit;
}

포함 된 페이지에서 $vars 변수는 Java 요청 속성으로 작동합니다

개념을 리디렉션하고 앞으로 다음과 같 Java,할 수 있습 달성 PHP too.

Redirect:: header("Location: redirect.php"); --(URL 주소 표시줄에 변경)

앞으로:: include forward.php ; --(URL 이 변경되지 않은 주소에서 바)

그리 관리 및 프로그래밍 로직

당신은 다음과 같이 사용할 수 있습니다.

header ("Location: /path/");
exit;

일부 HTML 출력이 전에 전송 된 경우에만 출구가 필요합니다. 헤더 ()는 작동하지 않으므로 이전에 새 헤더를 보내야합니다. 어느 브라우저에 출력.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top