简体   繁体   English

如何在zend Framework 1.12中使用此php代码

[英]How to use this php code in zend framework 1.12

I need the ability to enter an IP address in a form and then that IP would be used to obtain a status.php page that is running on http://'IP':port/status.php and then display the contents below the form. 我需要能够以某种形式输入IP地址,然后将该IP用于获取在http://'IP':port / status.php上运行的status.php页面,然后在形成。

I attached the two index.php and status.php files I have. 我附加了两个index.php和status.php文件。 They work outside of zend. 他们在zend之外工作。

How would I do this with zend 1.12 ? 我将如何使用zend 1.12做到这一点?

status.php status.php

<?php

/*error_log(date("U").": ".implode(", ",array_keys(($_REQUEST)))."\n",3,"/tmp/phperror.log");
if(!isset($_REQUEST['ip']))
exit(0);
*/
$ip = $_POST['IP'];


function getIP($ip)
{
    $port = ( $_REQUEST[ 'port' ] != "" ? $_REQUEST[ 'port' ] : '1234' );
    $site='http://'.$ip.':'.$port.'/status.php';
    //foreach( $_REQUEST as $k => $v)
    if ( $_REQUEST[ 'mode' ] != 'json' ) {
        //error_log(date("U").": ".$site." : ".implode($_REQUEST)."\n",3,"/tmp/phperror.log");
        error_log( date( "U" ) . ": " . $site . " : " . implode( ", ", array_keys( ( $_REQUEST ) ) ) . "\n", 3, "/tmp/phperror.log" );
        $file = file_get_contents( $site, false, $context );
        echo $file;
    }
    else {
        if ( $_SERVER[ 'REQUEST_METHOD' ] == 'POST' ) {
            $r    = $site . "?d=v";
            $data = array();
            foreach ( $_REQUEST as $key => $value ) {
                $r .= "&$key=$value";
                $data[ $key ] = $value;
            }
            $options = array( 'http' => array(
                'method'  => 'POST',
                'content' => http_build_query( $data )
            ) );
            error_log( date( "U" ) . ": " . $r . "\n", 3, "/tmp/phperror.log" );
            $context = stream_context_create( $options );
            //$file = file_get_contents($r, false, $context);
            $file = file_get_contents( $site, false, $context );
            echo $file;
        }
    }
}

getIP($ip);
?>

index. 指数。 php 的PHP

<!DOCTYPE html>
<html lang="en">
<head>
    <title>Title</title>
    <meta charset="UTF-8">
    <meta name=description content="">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <!-- Bootstrap CSS -->
    <link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css" rel="stylesheet" media="screen">
</head>
<body>
    <div class="container">
        <div class="row">
            <div class="col-xs-12 col-sm-12 col-md-12 col-lg-12">
                <h1 class="text-center">IP Address</h1>
                <form action="status.php" method="post" role="form" class="form-horizontal">

                    <div class="form-group">
                        <label for="IP" class="col-sm-2 control-label">Enter IP Address</label>
                        <div class="col-sm-10">
                            <input type="text" class="form-control" name="IP" id="IP" placeholder="IP Address">
                        </div>
                    </div>


                    <button type="button" class="btn btn-primary" onclick="deleteFile()">Submit</button>
                    <button type="reset" class="btn btn-danger">Clear</button>
                </form>
            </div>
            <div id="section"></div>
        </div>
    </div>

    <!-- jQuery -->
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
    <!-- Bootstrap JavaScript -->
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.min.js"></script>
    <script>

        /* Deletes the selected file */
        function deleteFile() {
            $.ajax({
                type: "POST",
                url: "status.php",
                data: {
                    IP: $( "#IP" ).val()
                }
            })
                .success(function (data) {
                    $('#section').html(data);
                });
        }
    </script>
</body>

EDIT: 编辑:

I followed Lucian's steps and I believe I do not have my indexaction on the controller correct. 我遵循了Lucian的步骤,我认为我对控制器的索引操作不正确。

I pasted status.php there and its erroring.... What do I have wrong 我在那里粘贴了status.php及其错误...。我有什么错

indexcontroller.php indexcontroller.php

<?php

class IndexController extends Zend_Controller_Action
{

public function init()
{
    /* Initialize action controller here */
}

public function indexAction()
{


/*error_log(date("U").": ".implode(", ",array_keys(($_REQUEST)))."\n",3,"/tmp/phperror.log");
if(!isset($_REQUEST['ip']))
exit(0);
*/
$ip = $_POST['IP'];


function getIP($ip)
{
    $port = ( $_REQUEST[ 'port' ] != "" ? $_REQUEST[ 'port' ] : '1234' );
    $site='http://'.$ip.':'.$port.'/status.php';
    //foreach( $_REQUEST as $k => $v)
    if ( $_REQUEST[ 'mode' ] != 'json' ) {
        //error_log(date("U").": ".$site." : ".implode($_REQUEST)."\n",3,"/tmp/phperror.log");
        error_log( date( "U" ) . ": " . $site . " : " . implode( ", ", array_keys( ( $_REQUEST ) ) ) . "\n", 3, "/tmp/phperror.log" );
        $file = file_get_contents( $site, false, $context );
        echo $file;
    }
    else {
        if ( $_SERVER[ 'REQUEST_METHOD' ] == 'POST' ) {
            $r    = $site . "?d=v";
            $data = array();
            foreach ( $_REQUEST as $key => $value ) {
                $r .= "&$key=$value";
                $data[ $key ] = $value;
            }
            $options = array( 'http' => array(
                'method'  => 'POST',
                'content' => http_build_query( $data )
            ) );
            error_log( date( "U" ) . ": " . $r . "\n", 3, "/tmp/phperror.log" );
            $context = stream_context_create( $options );
            //$file = file_get_contents($r, false, $context);
            $file = file_get_contents( $site, false, $context );
            echo $file;
        }
    }
}

getIP($ip);
    ?>

}

First of all create a layout (layout.phtml) using the html content of your index.php that is static and gets repeated on every page. 首先,使用index.php的html内容创建一个布局(layout.phtml),该内容是静态的,并且在每个页面上都会重复出现。

Second create a controller and put the php from the status.php inside an action within the controller. 第二个创建一个控制器,并将status.php中的php放入控制器内的一个动作中。

Third create a corresponding view to the controller and echo out everything that is computed in the action of the controller. 第三,为控制器创建一个对应的视图,并回显在控制器操作中计算出的所有内容。

Fourth create a Zend Form for the html form... 第四个为html表单创建一个Zend表单...

You could do a lot more, but i would adivse you to start with the basics (Step 1 - 3). 您可以做更多的事情,但我会建议您从基础开始(步骤1-3)。 For more information on view / controller / action in zend, have a look at: 有关zend中的view / controller / action的更多信息,请查看:

http://framework.zend.com/manual/1.12/de/zend.controller.action.html http://framework.zend.com/manual/1.12/de/zend.controller.action.html

EDIT: 编辑:

There are some quick start skeletons that can help you to jumpstart your application. 有一些快速入门框架可以帮助您快速启动应用程序。 Have a look at 看一下

https://github.com/RichardKnop/zend-v112-skeleton https://github.com/RichardKnop/zend-v112-skeleton

You will find the mentioned stuff like "controller","action","view" among the files / folders inside the skeleton, just customize them to your needs. 您可以在框架内的文件/文件夹中找到诸如“控制器”,“动作”,“视图”之类的内容,只需根据需要对其进行自定义即可。

EDIT 2 编辑2

You can pass data from the controller to the view using $this->view from the controller. 您可以使用控制器中的$ this-> view将数据从控制器传递到视图。 For that checkout this answer on stackoverflow: Zend Framework, passing variables to view 对于该检查,请在stackoverflow 上查看以下答案: Zend Framework,将变量传递给视图

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM