2016-12-15 60 views

回答

1

託管終於找到了如何做正確,多挖後。以下是如何...

的application/config/app.php:

<?php 

return array(
    'assets' => array(

     'foobar/my-account' => array(
      array(
       'javascript', 
       'js/my_account.js' 
      ), 
     ), 

    ), 
); 

控制器:

<?php 
namespace Application\Controller\SinglePage; 

use PageController; 

class MyAccount extends PageController 
{ 
    public function view() 
    { 
    $this->requireAsset('javascript', 'foobar/my-account'); 
    } 
} 
2

你做的工作,但它不是很方便,沒有按的方式沒有利用所有的選擇。 問題來自於您在控制器中需要一個您從未真正聲明過的資產。

現在它是在你的app.php中聲明的,但它不一定是。你可以在控制器中做到這一點,這將使事情更容易維護。

<?php 
namespace Application\Controller\SinglePage; 

use PageController; 
use AssetList; 
use Asset; 

class MyAccount extends PageController 
{ 
    public function view() 
    { 
     $al = AssetList::getInstance(); 

     // Register (declare) a javascript script. here I called it foobar/my-script which is the reference used to request it 
     $al->register(
      'javascript', 'foobar/my-script', 'js/my_account.js', array('version' => '1.0', 'position' => Asset::ASSET_POSITION_FOOTER, 'minify' => true, 'combine' => true) 
     ); 

     // Register (declare) a css stylesheet. here I called it foobar/my-stylesheet which is the reference used to request it 
     $al->register(
      'css', 'foobar/my-stylesheet', 'css/my_account.css', array('version' => '1.0', 'position' => Asset::ASSET_POSITION_HEADER, 'minify' => true, 'combine' => true) 
     ); 

     // Gather all the assets declared above in an array so you can request them all at once if needed 
     $assets = array(
      array('css', 'foobar/my-stylesheet'), 
      array('javascript', 'foobar/my-script') 
     ); 

     // Register the asset group that includes all your assets (or a subset as you like). here I called it foobar/my-account which is the reference used to request it 
     $al->registerGroup('foobar/my-account', $assets); 

     // require the group so all the assets are loaded together 
     $this->requireAsset('foobar/my-account'); 

     // Alternatively you can call only one of them 
     // $this->requireAsset('javascript', 'foobar/my-script'); 
    } 
} 
+0

太棒了!這就是'AssetList'的工作原理。我看到了幾個例子,但沒有一個是完整的,這非常惱人。再次感謝! – adamj