私の CakePHP プロジェクトには、ajax 呼び出し用のパスが必要な js ファイルがいくつかあります。サーバー上でパスを生成し、それらを一般的な方法で JavaScript に渡したいと考えています。それらは静的であるため、関数にパラメーターとして追加したくありません。今、私はこのようにやっています:
<?php
$categories = Router::url(array('controller' => 'categories', 'action' => 'getChildCategories'));
$brands = Router::url(array('controller' => 'brands', 'action' => 'autoComplete'));
// first add the variables
echo $this->Html->scriptBlock(
'var CATEGORIE_GETSUBCATEGORY = "' . $categories . '";
var BRAND_AUTOCOMPLETE = "' . $brands . '";',
array('inline' => false)
);
// then include the file that uses them
echo $this->Html->script('add');
?>
それらをパラメーターとして渡さずにこれを行うより良い方法はありますか?