これは、jsおよびcssファイルアセットの順序を手動で操作する方法です。
まず、コントローラーにcssパスとjsパスを追加する場合は、自動Phalcon \ Assets\Collectionコンテナーを使用してアドホックアセットを保存します。
$this->assets->addJs('js/bootstrap-multiselect.js');
$this->assets->addCss('css/bootstrap-multiselect.css');
すべてのコントローラーが拡張されるカスタムBaseControllerに、afterExecuteRoute()パブリックメソッドを追加します。
/**
* stuff to do after a route has been executed
* this is where we attach standard js and css assets
* */
public function afterExecuteRoute(){
// wait to rebuild assets until after the dispatcher is finished
if( ! $this->dispatcher->isFinished() ){
return;
}
...
}
現在のルートの実行が完了したことを確認したら、Phalcon \ Assets \ Managerの自動jsおよびcssコレクションからアドホックアセットを取得し、それらを一般的なアセットファイルのリストに追加し、順序付けして重複排除します。次に、それらを新しいカスタムコレクションに入れます。
// get list of js files added to the standard js collection
$append_js = array();
forEach( $this->assets->getJs() as $js){
$append_js[] = $js->getPath();
}
// declare paths to common js assets
$js_assets = array(
'js/jquery-2.1.1.min.js',
'js/jquery-ui.min.js',
'js/bootstrap.min.js',
);
// merge common paths with ad-hoc paths
$js_assets = array_merge( $js_assets, $append_js );
$js_assets = array_unique( $js_assets ); // dedup
// add js assets to a new collection
$js_collection = $this->assets->collection('header_js');
forEach( $js_assets as $js_path ){
$js_collection->addJs( $js_path );
}
cssアセットを新しいコレクションに再構築することは同じように機能します。
// get list of css files added to the standard css collection
$append_css = array();
forEach( $this->assets->getCss() as $css ){
$append_css[] = $css->getPath();
}
// declare paths to common css assets
$css_assets = array(
'css/jquery-ui.min.css',
'css/jquery-ui.theme.min.css',
'css/jquery-ui.structure.min.css',
'css/bootstrap.min.css',
'css/bootstrap-theme.min.css',
'css/main.css',
);
// merge common paths with ad-hoc paths
$css_assets = array_merge( $css_assets, $append_css );
$css_assets = array_unique( $css_assets ); // dedup
// add css assets to a new collection
$css_collection = $this->assets->collection('header_css');
forEach( $css_assets as $css_path ){
$css_collection->addCss( $css_path );
}
最後に、jsおよびcssアセットをテンプレートに出力するときに、Phalcon \ Assets \ Managerが自動的に作成したデフォルトのコレクションの代わりに、新しいカスタムコレクションを出力します。
{{ getDoctype() }}
<html>
<head>
{{ tag.getTitle() }}
{{ assets.outputCss( 'header_css' ) }}
{{ assets.outputJs( 'header_js' ) }}
</head>