Embed CSS in the layout from the zf2 module

I almost finished writing a very simple module for zf2. The only thing I would like to make my module is to add some css to the layout so that the HTML that it generates displays in the best possible way.

Is this possible from within the module? If so, how?

EDIT: Thanks to everyone for the quick answers. However, I think I probably didn’t explain myself very clearly. When I say "inject some kind of css", I mean take the css string and actually display INSIDE in the layout. I did not mean a link to an external css file or the presence of an asset manager publishing my files, such as the answers that have so far been suggested.

+4
source share
3 answers

Add a variable to your layout where you want to insert CSS:

<a href="#" style="<?php echo($this->CSS); ?>">Some Link</a> 

Then in your controller, download and assign it, but you want:

 $this->layout()->CSS = "CSS"; $this->layout()->CSS = $this->getRequest()->getPost('CSStoInject'); $this->layout()->CSS = fopen(), curl(), etc. 
+1
source

See Publishing Assets from Modules to Zend Framework 2 or How to Combine Shared Directories of Zend Framework 2 Module 2 for a discussion of the options that you have for crowding out public assets from a module.

And in addition to distributing your module assets to public, you can add the application to the running method, for example onBootstrap:

 public function onBootstrap($e) { $sm = $e->getApplication()->getServiceManager(); $headLink = $sm->get('viewhelpermanager')->get('headLink'); $headLink->appendStylesheet('/assets/MyModule/css/mystylesheet.css'); } 
+7
source

Try using something like:

 $sm = $this->getEvent()->getApplication()->getServiceManager(); $helper = $sm->get('viewhelpermanager')->get('headLink'); $helper->prependStylesheet('/css/mystylesheet.css'); 

in the controller module.

EDIT:

If you want to save the css style in the module, you can either display it in your layout file (section of the chapter), or, all the better, create another route in the module, for example /get/style/[:name] . This route points to another action that returns only text with text / css. More or less:)

+2
source

Source: https://habr.com/ru/post/1445293/


All Articles