Php 在Zend Framework应用程序中集成现有页面
可以绕过Zend Framework网站中的任何控制器吗?相反,我希望执行一个普通的PHP脚本,它的所有输出都应该放在来自ZF的布局/视图中: 请求-->执行PHP脚本-->捕获输出-->添加输出以查看-->发送响应 挑战在于将现有页面/脚本集成到新创建的Zend Framework站点中,该站点使用MVC模式Php 在Zend Framework应用程序中集成现有页面,php,zend-framework,Php,Zend Framework,可以绕过Zend Framework网站中的任何控制器吗?相反,我希望执行一个普通的PHP脚本,它的所有输出都应该放在来自ZF的布局/视图中: 请求-->执行PHP脚本-->捕获输出-->添加输出以查看-->发送响应 挑战在于将现有页面/脚本集成到新创建的Zend Framework站点中,该站点使用MVC模式 Cheers在视图中创建一个标准php include/require,将php脚本的输出嵌入控制器(或模型)中,您可以添加: $output = shell_exec('php /lo
Cheers在视图中创建一个标准php include/require,将php脚本的输出嵌入控制器(或模型)中,您可以添加:
$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');
我在我的
.htaccess
文件中创建了一个新条目:
RewriteRule (.*).php(.*)$ index.php [NC,L]
这是适当的行动:
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);
}
非常有用:除非您从视图中调用它,否则这将不起作用,否则它将仅放置在实际视图内容之后。此外,根据输出的格式,可能还有一些额外的标记需要解析(HTML、HEAD、BODY等)。最好是在控制器和/或模型中处理,而不是在视图中。我认为
shell\u exec()
不会填充$\u服务器
变量,这可能会导致php脚本无法运行。问得好,我认为您的思路是正确的。+1用于引用Chris Abernethy页面,这是一个很棒的页面。@user413773:您如何处理对site.com/news/的请求,它应该是site.com/news/index.php,而不是NewsController::indexAction()
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);
}