如何在 zf2 控制器中附加 css 和 javascript 文件? [英] How to attach css and javascript files in zf2 controller?

查看:24
本文介绍了如何在 zf2 控制器中附加 css 和 javascript 文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

推荐答案

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

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).

从查看您的代码来看,我猜问题出在对 $this->viewHelperManager 的调用上,因为没有这样的事情(除非您已将其设置在控制器).

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).

您可能想要:

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

尽管如果您需要那么多视图助手调用,则可能值得将 headLink 助手作为依赖项传入.

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

这篇关于如何在 zf2 控制器中附加 css 和 javascript 文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆