2010-08-06 51 views
3

是否可以绕过Zend Framework网站中的任何控制器?相反,我希望执行一个正常的PHP脚本,并且它的所有输出应该放在来自ZF的布局/视图中:将现有页面集成到Zend Framework应用程序

请求 - >执行PHP脚本 - >捕获输出 - >将输出添加到视图 - >发送回复

挑战在于将现有页面/脚本集成到新创建的Zend Framework站点中,该站点正在使用MVC模式。

干杯

+0

问得好,我认为你在正确的轨道上。 – chelmertz 2010-08-07 20:23:04

回答

0

做一个标准的PHP在视图中包括/需要嵌入PHP脚本

+1

除非您从视图中调用它,否则这将不起作用,否则它将仅放置在实际视图内容之后。另外,根据输出如何格式化,可能会有一些额外的标签解析出来(HTML,HEAD,BODY等)。在控制器和/或模型中处理它可能会更好,但不是视图。 – pferate 2010-08-07 01:14:19

1

在你的控制器(或模型)的输出,你可以添加:

$output = shell_exec('php /local/path/to/file.php'); 

在您可以根据需要解析并清理$output,然后将其存储在您的视图中。

您可以将您要执行的php文件存储在您的scripts目录中。

如果PHP文件存储在远程服务器上,你可以使用:

$output = file_get_contents('http://www.example.com/path/to/file.php'); 
+0

我不认为'shell_exec()'填充了'$ _SERVER'变量,这可能导致php脚本无法运行。 – chelmertz 2010-08-07 20:15:44

6

我在.htaccess文件中创建一个新条目:

RewriteRule (.*).php(.*)$ index.php [NC,L]

上通常PHP文件的每个请求现在由ZF的index.php处理。

接下来,我创建了一个额外路由的路由这些请求到一定的控制作用:

$router->addRoute(
    'legacy', 
    new Zend_Controller_Router_Route_Regex(
    '(.+)\.php$', 
    array(
     'module' => 'default', 
     'controller' => 'legacy', 
     'action' => 'index' 
    ) 
) 
); 

这是适当的操作:

public function indexAction() { 
    $this->_helper->viewRenderer->setNoRender(); 
    $this->_helper->layout->setLayout('full'); 

    // Execute the script and catch its output 
    ob_start(); 
    require($this->_request->get('DOCUMENT_ROOT') . $this->_request->getPathInfo()); 
    $output = ob_get_contents(); 
    ob_end_clean(); 

    $doc = new DOMDocument(); 
    // Load HTML document and suppress parser warnings 
    @$doc->loadHTML($output); 

    // Add keywords and description of the page to the view 
    $meta_elements = $doc->getElementsByTagName('meta'); 
    foreach($meta_elements as $element) { 
    $name = $element->getAttribute('name'); 
    if($name == 'keywords') { 
     $this->view->headMeta()->appendName('keywords', $element->getAttribute('content')); 
    } 
    elseif($name == 'description') { 
     $this->view->headMeta()->appendName('description', $element->getAttribute('content')); 
    } 
    } 

    // Set page title 
    $title_elements = $doc->getElementsByTagName('title'); 
    foreach($title_elements as $element) { 
    $this->view->headTitle($element->textContent); 
    } 

    // Extract the content area of the old page 
    $element = $doc->getElementById('content'); 
    // Render XML as string 
    $body = $doc->saveXML($element); 

    $response = $this->getResponse(); 
    $response->setBody($body); 
} 

非常有用:http://www.chrisabernethy.com/zend-framework-legacy-scripts/

+0

对于Chris Abernethy页面的引用非常棒。 – 2010-08-08 12:10:09

+0

@ user413773:你如何处理** site.com/news/**的请求,该请求应该是** site.com/news/index.php **而不是'NewsController :: indexAction()' – chelmertz 2010-08-09 08:05:16

相关问题