Integrate Existing Pages in Zend Framework

Can I bypass any controller on the Zend Framework website? Instead, I want a regular PHP script to be executed, and all of its output should be placed in a layout / view coming from ZF:

Request โ†’ Execute PHP script โ†’ Output output โ†’ Add output for viewing โ†’ Send response

The challenge is to integrate existing pages / scripts into the newly created Zend Framework site, which works with the MVC template.

Greetings

+3
source share
3 answers

I created a new entry in the file .htaccess:

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

Each request in a regular .php file is now processed by index.php from ZF.

:

$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/

+6

( ) :

$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');
+1

php include/require php-

0

Source: https://habr.com/ru/post/1758475/


All Articles