2010-08-06 54 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

相關問題