2017-01-18 22 views
4

저는 지금이 altorouter를 몇 주 동안 사용해 왔습니다. 이것은 그물이나 공식 사이트에서 많은 예제가없는 좋은 라우터가 될 것으로 보인다. 어떻게 든 그것을 이해하고 일을 끝내야합니다.AltoRouter GET POST 방법은 어떻게 작동합니까?

나는 altorouter를 사용하여 기본 GET 및 POST를 시도했지만 이것이 올바른 방법인지 여부를 알지 못합니다. PHP에서

간단한 GET 방식

<html> 
<head> 
</head> 
<body> 
<form action="welcome.php" method="post"> 
    Name: <input type="text" name="name"><br> 
    E-mail: <input type="text" name="email"><br> 
    <input type="submit"> 
</form> 
</body> 
</html> 

내가

<?php 
require 'library/AltoRouter.php'; 
$router = new AltoRouter(); 
$router->setBasePath('/AltRouter'); 

$router->map('GET','/', function() {require __DIR__ . '/catalog/controller/home.php';}, 'home'); 
$router->map('GET|POST','/aboutus/', function() {require __DIR__ . '/catalog/controller/aboutus.php';}, 'aboutus'); 
$router->map('GET|POST','/contactus/', function() {require __DIR__ . '/catalog/controller/contactus.php';}, 'contactus'); 
$router->map('GET|POST','/welcome/', function() {require __DIR__ . '/catalog/controller/welcome.php';}, 'welcome'); 

$match = $router->match(); 

if($match && is_callable($match['target'])) { 
    call_user_func_array($match['target'], $match['params']); 
} else { 
    // no route matched 
    header($_SERVER["SERVER_PROTOCOL"] . ' 404 Not Found'); 
} 

contactus.php Index.php는

AltoRouter

를 사용했던 방법 (GET 메서드)

,210

이 작품 몇 가지 이상한 이유로

Welcome <?php echo $_POST["name"]; ?><br> 
Your email address is: <?php echo $_POST["email"]; ?> 

welcome.php 그러나 나는 이것이 옳지 않다 생각합니다. 이유 : 나는 양식을 제출 한 후 얻을 URL로 page.Where이

http://localhost/altrouter/contactus/ 

없음 인 GET 방식으로 전송 된 정보는 모두가 볼 수, 변수가 URL에 표시됩니다, 그것은 북마크 할 수 있습니다 변수는 URL에 양식을 제출 한 후에 표시됩니다.

이제 POST 방법에 대해,이 하나가 당신에게 알려줘야합니다. 우리가 어떻게해야하는지 알려주는 것입니다.

Index.php는

same as the one posted above 

aboutus.php (POST 방식 사용)

<html> 
<head> 
</head> 
<body> 
<?php 
if ($_SERVER["REQUEST_METHOD"] == "POST") { 
     $name = $_POST["first_name"]; 
     $email = $_POST["email_address"]; 

     echo "<h2>Your Input:</h2>"; 
     echo $name; 
     echo "<br>"; 
     echo $email; 
     echo "<br>"; 
} 
?> 

<form action="<?php $_SERVER["PHP_SELF"]?>" method="post"> 
    Name: <input type="text" name="first_name"> 
    <br><br> 
    E-mail: <input type="text" name="email_address"> 
    <br><br> 
    <input type="submit" name="submit" value="Submit"> 
</form> 
</body> 
</html> 
이 작동

데이터는 URL

를 제출 한 후, 밖으로 echo'ed 게시 됨
http://localhost/altrouter/aboutus/ 

무엇이 옳고 그른지 알려주세요.

답변

4

그래도, 좀 관찰을해야합니까 ... 난 당신이 요구하는 것을 이해 생각하지 않는다 : GET 메서드와 함께 전송


정보는 모두가 볼 수, 변수가 표시됩니다 URL에서

예, HTTP 메소드 GET에서 발생합니다. URL 끝의 ?name=Joe&[email protected]은 "쿼리 문자열"이라고합니다. 메소드 POST와의 차이점 중 하나는 데이터가 URL의 일부이므로 보이는 것입니다 (그렇지 않으면 이 표시되지 않는다고 믿을 수 없으며 그렇지 않으면이 표시됨). 그러면 북마크 할 수 있다고 말합니다.


GET과 POST에서 해당 방법의 사용법을 읽고 각 경로에 대해 결정하십시오. 단일 컨트롤러에 여러 가지 방법을 매핑하는 것이 좋은 디자인이라고 생각하지 않습니다. 예를 들어, 다른 방법을지도, 라우터를 활용 :

$router->map('GET','/contactus', 'showContactForm'); 
$router->map('POST','/contactus', 'processContactForm'); 

은 "MVC"에 대한 질문에 태그를 때문에이 더 일을 분리하고 컨트롤러는 단지 컨트롤러 수있을 수있는 차례 호출 또는 보기를 생성합니다. 또는 라우팅, 뷰 템플릿, 데이터베이스 연결, 인증 및 훨씬 더 많은 것을 관리하는 Lumen과 같은 간단한 MVC 프레임 워크를 사용할 수도 있습니다.

<form action="../welcome/" method="post">


http://localhost/altrouter/contactus/에서 http://localhost/altrouter/welcome/에 상대 URL은 welcome 수 있습니다. ..은 "디렉토리 가기"를 의미합니다.


내가 양식을 제출 한 후 얻을 URL은 당신이 말한대로 양식이 성공적으로 제출하면 내가하지 않는

http://localhost/altrouter/contactus/

이 왜, 당신이 있어야 할 것입니다 http://localhost/altrouter/welcome/


피을에. 그것은 insecurities을 가져옵니다. action 속성이없는 양식은 동일한 URL에 제출됩니다. 메서드 POST를 사용하면 앞에서 말했던 것처럼 동일한 URL에 대해 두 작업을 따로 처리 할 수 ​​있습니다.