How to add JavaScript inside controller? I don't want to call build form. I can call css or js in src/Form/Form.php but I want to call css or js inside Controller src/Controller/Controller.php This process I am using to call css and js in Form page

public function buildForm(array $form, FormStateInterface $form_state,$cid = NULL)

$form['#attached']['library'][] = 'cssjs/val_master';


Could anyone write some example to call css or js inside Controller



The same you can use in Controller also:


   namespace Drupal\cssjs\Controller;

   use Drupal\Core\Controller\ControllerBase;

   class CssJs extends ControllerBase{
     public function css_js(){
       $build['#attached']['library'][] = 'cssjs/val_master';
       return $build;


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.