D
D
Dokuro2015-10-28 13:39:11
PHP
Dokuro, 2015-10-28 13:39:11

Exceptions in PHP or how to pass an object to an included file?

How to pass $e to included file?
If: "Exception objects are exceptions only in the catch block and do not propagate to nested files."
The code:

catch (Exception $e)
{
    if ($config->phalcon->debug)
    {
        include_once(rtrim($config->phalcon->viewsDir, '/').'/error/exception_debug.phtml');
    }
    else
    {
        include_once(rtrim($config->phalcon->viewsDir, '/').'/error/exception.phtml');
    }
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
L
Lesha Kiselev, 2015-10-28
@Yakud

Write a f-th that renders a template and bandits data into it. For example like this:

function render($template, array $data = []) {
  if (!file_exists($template)) {
    throw Exception("Template {$template} not found");
  }

  foreach($data as $key => $value) {
    $$key = $value;
  }
  ob_start();
  include $template;
  return ob_get_clean();
}

In your case, use like this:
catch (Exception $e)
{
    $templateData = [
        'e' => $e,
    ];
    
    if ($config->phalcon->debug)
    {
        echo render(rtrim($config->phalcon->viewsDir, '/').'/error/exception_debug.phtml', $templateData);  
    }
    else
    {
        echo render(rtrim($config->phalcon->viewsDir, '/').'/error/exception.phtml', $templateData);  
    }
}

Inside the template, an instance of the exception object will be available.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question