How to attach css and javascript files in zf2 cont

2019-08-04 12:25发布

问题:

i attach some stylesheets in zend controller but blank page shown,how i include stylesheets and javascript files in zend controller?here is my code:

public function showAction()
    {       
            //css

            $this->viewHelperManager->get('headLink')->appendStylesheet('/css/style.css');
            $this->viewHelperManager->get('headLink')->appendStylesheet('/css/bootstrap-theme.min.css');
            $this->viewHelperManager->get('headLink')->appendStylesheet('/css/tweaks.css');
            $this->viewHelperManager->get('headLink')->appendStylesheet('/css/fullcalendar.css');
            $this->viewHelperManager->get('headLink')->appendStylesheet('/css/fullcalendar.print.css');
            $this->viewHelperManager->get('headLink')->appendStylesheet('/css/jquery-ui.min.css');
            $this->viewHelperManager->get('headLink')->appendStylesheet('/css/jquery.simple-dtpicker.css');
            //js
            $this->viewHelperManager->get('headScript')->appendFile('/admin_static/js/jquery-ui-1.10.3.custom/js/jquery-ui-1.10.3.custom.min.js');
            $id = (int) $this->params()->fromRoute('id', 0);
            if (!$id) {
                return $this->redirect()->toRoute('calendar', array(
                    'action' => 'create'
                ));
            }
            $calendar = $id;
            $dm = $this->getServiceLocator()->get('doctrine.documentmanager.odm_default');
            $eventshow = $dm->createQueryBuilder('Calendar\Document\Calendar')
            ->hydrate(false)
            ->field("id")->equals($id)
            ->getQuery()->execute();
            $array = array();
            if($eventshow && !is_null($eventshow) && is_object($eventshow)){                    
                foreach($eventshow as $key=>$value) {   
                $array[] = array(
                        'calendar_id' => $value['_id'],
                        'user_id' => $value['user_id'],
                        'title' => $value['title'],
                        'description' => $value['description'], 
                    );
                }
            }
            return array('calendar' => $calendar , 'calendardata' => $array);
    }

回答1:

The 'blank page' just means an error is being generated but you have display_errors turned off. Check your web server error log to see what the actual problem is (this will help you debug future issues as well).

From looking at your code, I'd guess the problem is with the calls to $this->viewHelperManager, as there is no such thing (unless you've set that elsewhere in your controller).

You probably want:

$this->getServiceLocator()->get('viewhelpermanager')->get('headLink')->appendStylesheet('/css/style.css');

although if you need that many view helper calls it might be worth passing the headLink helper in as a dependency.