Integrating existing pages in a Zend Framework application
Is it possible to bypass any controller in a Zend Framework web site? Instead I want a normal PHP script executed and all its o开发者_开发问答utput should be placed in the layout/view coming from ZF:
Request --> Execute PHP script --> Catch output --> Add output to view --> Send response
The challenge is to integrate existing pages/scripts into a newly created Zend Framework site which is working with the MVC pattern.
Cheers
I created a new entry in my .htaccess
file:
RewriteRule (.*).php(.*)$ index.php [NC,L]
Every request on a usual .php file is handled by index.php from ZF now.
Next I created an additional route to route those requests to a certain controller action:
$router->addRoute(
'legacy',
new Zend_Controller_Router_Route_Regex(
'(.+)\.php$',
array(
'module' => 'default',
'controller' => 'legacy',
'action' => 'index'
)
)
);
And this is the appropriate action:
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);
}
Very useful: http://www.chrisabernethy.com/zend-framework-legacy-scripts/
In your Controller (or Model) you can add:
$output = shell_exec('php /local/path/to/file.php');
At that point you can parse and clean up $output
as needed and then store it in your View.
You can store the php file you are going to execute in your scripts
directory.
If the PHP file is stored on a remote server you can use:
$output = file_get_contents('http://www.example.com/path/to/file.php');
Make a standard php include/require in your view to embed the output of your php scripts
精彩评论