We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 here.

Load CSS, JS files in modules directory

Hello friends,

I have 2 module directories

/apps/backend/

/apps/frontend/

All assets files loaded and working with in /public/css/ directory, but I am need this: every module must be assets directory.

/apps/backend/css/

/apps/backend/js/

I am try for load any css or js files with volt. But I have get this error:

CssController handler class cannot be loaded

What is the best way for load assets files in module directories ? , Thanks.

You need to load those assets in controller in each module. Or register CssController before loading modules so it would be accessible in any controller.



5.1k

Can you explain this with an example? I cant found any examples. thanks.

Where you have defined CssController and where you loading it ?



5.1k

I am not defined any CssController, I am trying load css in module directory "/apps/backend/css/theme.css" with this

->addCss('css/theme.css')

and

output on VOLT

{{ assets.outputCss() }}

getting this error

CssController handler class cannot be loaded

I said in previous post, I want load css, js etc.. on "\apps\backend\css\" directory.

per module must be have css and js directory and I need load assets in module directory etc: "\apps\backend\css\theme.css"

Thanks.

->addCss('css/theme.css')

where you put this code ?



5.1k
edited Dec '15

namespace KolayWeb\Backend\Controllers;

use Phalcon\DI;

use Phalcon\Mvc\Controller;

class ControllerBase extends Controller

public function onConstruct(){

$this->assets

            ->addCss('css/theme.css');

}

->addCss('css/theme.css')

where you put this code ?

edited Dec '15

And then where are you sing this ControllerBase ?



5.1k

my structure

"apps\backend"

----"controller\LoginController"

----"controller\IndexController"

I am enter backend panel with this url

localhost/kolayWeb/yonetim/



5.1k

I think I can't explain my problem. I need to import “assets files” in “module directories”.

Each module must have “self assets directory” and CSS – JS files must import from module directory.

Hope I explained clearly.

thanks

edited Dec '15

Okay. First, when user request you page, whole application is loaded to index.php that means that you need to provide path to theme.css/theme.js from index.php directory. Put something like this in your BaseController:

class BaseController extends Controller
{
    public function initialize()
    {
        $moduleName = $this->dispatcher->getModuleName();
        $this->assets->setTargetPath("js/combined-$moduleName.js") // its imporant, it will be created under /public/js
          ->setTargetUri("js/combined-$moduleName.js") // its imporant, its saying in view output there will be that link
          ->addJs("../apps/$moduleName/js/somefile.js"
          ->join(true);
          // ->addFilter(new Jsmin()) - on production you should add filter
          $this->assets->setTargetPath("css/combined-$moduleName.css") // its imporant, it will be created under /public/css
          ->setTargetUri("css/combined-$moduleName.css") // its imporant, its saying in view output there will be that link
          ->addCss("../apps/$moduleName/css/somefile.css"
          ->join(true);
          // ->addFilter(new Cssmin()) - on production you should add filter
    }
}

Then your controllers should extend BaseController, and this class should be load before loading modules, or load this class in each module - your choice. Also consider changing class to asbtract class - cuz you not gonna ever use it as object/put actions in there.

If you want your files be automatically loaded without putting names there just get all file names in category using scandir and do addCss/addJs for each element.



5.1k
edited Dec '15

Thanks for helping Jurigag.

$this->assets->setTargetPath

Have little error, setTargetPath Method Not found in assets ? I am use phpstorm with phalcon latest version.



5.1k
Accepted
answer
edited Dec '15

I am solved all problem with this code on BaseController onConstruct Method.

$this->assets->collection('myCssLibary')

        ->setTargetPath('css/theme.css')
        ->setTargetUri("css/theme.css")
        ->addCss(APP_PATH."/apps/backend/themes/tech/css/theme.css")
        ->join(true)
        ->addFilter(new \Phalcon\Assets\Filters\Cssmin());

Thanks again :)