[ZF 2.x] Renvoyer un flux javascript depuis un controller
Bonjour,
Je voudrais renvoyer un flux javascript depuis un controller. Le problème est que zend essaye de charger un renderer et vue que le fichier phtml n'existe pas pour mon action il génère une exception.
J'ai réussi à faire quelque chose d'équivalent pour renvoyer du JSon. POur celà dans le fichier de config de mon module j'ai rajouté :
Citation:
'strategies' => array(
'ViewJsonStrategy',
),
Dans le view manager.
Mais je ne vois pas comment faire pour le javascript. Voici une partie du code de mon action :
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
| function getImageListJSAction()
{
$output = '';
$delimiter = "n"; // for eye candy... code gets new lines
$output .= 'var tinyMCEImageList = new Array(';
$directory = "./public/img/";
....
if (is_dir($directory)) {
$direc = opendir($directory);
while ($file = readdir($direc)) {
if (is_file("$directory/$file") && getimagesize("$directory/$file") != FALSE) {
// We got ourselves a file! Make an array entry:
$output .= $delimiter
. '["'
. utf8_encode($file)
. '", "'
. utf8_encode("$abspath/$directory/$file")
. '"],';
}
}
$output = substr($output, 0, -1); // remove last comma from array item list (breaks some browsers)
$output .= $delimiter;
closedir($direc);
}
// Finish code: end of array definition. Now we have the JavaScript code ready!
$output .= ');';
// Make output a real JavaScript file!
header('Content-type: text/javascript'); // browser will now recognize the file as a valid JS file
// prevent browser from caching
header('pragma: no-cache');
header('expires: 0'); // i.e. contents have already expired
$output = $output . "\n\n";
// Now we can send data to the browser because all headers have been set!
echo $output;
} |
Et voici le flux retourné par le serveur :
Citation:
var tinyMCEImageList = new Array(n["en_US.gif", "//./public/img//en_US.gif"],n["favicon.ico", "//./public/img//favicon.ico"],n["fr_FR.gif", "//./public/img//fr_FR.gif"],n["glyphicons-halflings.png", "//./public/img//glyphicons-halflings.png"],n["top_bg.png", "//./public/img//top_bg.png"],n["client_logo.png", "//./public/img//client_logo.png"]n);
<br />
<font size='1'><table class='xdebug-error' dir='ltr' border='1' cellspacing='0' cellpadding='1'>
<tr><th align='left' bgcolor='#f57900' colspan="5"><span style='background-color: #cc0000; color: #fce94f; font-size: x-large;'>( ! )</span> Fatal error: Uncaught exception 'Zend\View\Exception\RuntimeException' with message 'Zend\View\Renderer\PhpRenderer::render: Unable to render template "nav-site/ajax/get-image-list-js"; resolver could not resolve to a file' in C:\eclipse\workspace\client\vendor\zendframework\zendframework\library\Zend\View\Renderer\PhpRenderer.php on line <i>461</i></th></tr>
<tr><th align='left' bgcolor='#f57900' colspan="5"><span style='background-color: #cc0000; color: #fce94f; font-size: x-large;'>( ! )</span> Zend\View\Exception\RuntimeException: Zend\View\Renderer\PhpRenderer::render: Unable to render template "nav-site/ajax/get-image-list-js"; resolver could not resolve to a file in C:\eclipse\workspace\client\vendor\zendframework\zendframework\library\Zend\View\Renderer\PhpRenderer.php on line <i>461</i></th></tr>
<tr><th align='left' bgcolor='#e9b96e' colspan='5'>Call Stack</th></tr>
merci