javascript - 如何在 zf2 Controller 中附加 css 和 javascript 文件?

标签 javascript zend-framework2 stylesheet zend-controller

我在 zend Controller 中附加了一些样式表,但显示空白页面,我如何在 zend Controller 中包含样式表和 javascript 文件?这是我的代码:

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);
    }

最佳答案

“空白页”仅表示正在生成错误,但您已关闭 display_errors。检查您的网络服务器错误日志以了解实际问题是什么(这也将帮助您调试 future 的问题)。

从你的代码来看,我猜问题出在对 $this->viewHelperManager 的调用上,因为没有这样的东西(除非你在 Controller 的其他地方设置了它) )。

您可能想要:

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

尽管如果您需要那么多 View 帮助程序调用,则可能值得将 headLink 帮助程序作为依赖项传递。

关于javascript - 如何在 zf2 Controller 中附加 css 和 javascript 文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23271745/

相关文章:

javascript - 更新后重新加载 AJAX 加载的页面

Javascript - 从对象内的数组中获取唯一值

php - 强制 Canonical 使用 HTTPS

php - ZF2, ZF2-NoCaptcha, FormValidation

CSS 问题 - 通过将样式链接到另一个样式来设置(例如字体大小)一组样式的任何方法?

html - <Body> 标签的边距和填充

javascript - 如何在滚动条上逐个字母地加载文本

zend-framework2 - 在 ZF2 上配置多 DB 连接

performance - React-Native中动态样式的最高性能方式是什么?

javascript - 仅当在页面上找到元素时才运行函数?