How to attach css and javascript files in zf2 controller?

≯℡__Kan透↙ 提交于 2019-12-11 10:26:54

问题


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.



来源:https://stackoverflow.com/questions/23271745/how-to-attach-css-and-javascript-files-in-zf2-controller

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!