6 users online. Create an account or sign in to join them.Users

Search

I'm trying to add a text/css header to a page via an event. I know there is this nifty function addHeaderToPage(), but how do I call it inside my event?

Neither

 $this->addHeaderToPage("Content-Type", "text/css; charset=UTF-8");

nor

$this->_Parent->addHeaderToPage("Content-Type", "text/css; charset=UTF-8");

seems to work.

Unfortunately there are some limitations of rev5 that make it difficult to do. In order to add headers using addHeaderToPage you need access to the instance of FrontendPage held by the main Frontend class. You really only have one practical option, until RC1 where this problem doesn't exist. Use a direct call to the PHP function header();

header('Content-Type: text/css; charset=UTF-8');

In RC1 you will be able to use Frontend::Page()->addHeaderToPage("Content-Type", "text/css; charset=UTF-8"); from anywhere in Symphony.

Here is my solution for text/css headers (which works fine). It is an event called header_text_css.

<?php

    require_once(TOOLKIT . '/class.event.php');

    Class eventheader_text_css extends Event{

        const ROOTELEMENT = 'header-text-css';

        public static function about(){
            return array(
                        'name' => 'Header: text/css',
                        'author' => array(
                            'name' => 'Michael Eichelsdoerfer',
                            'website' => 'http://www.michael-eichelsdoerfer.de',
                            'email' => 'info@michael-eichelsdoerfer.de'),
                        'version' => '1.0',
                        'release-date' => '2008-04-03',
                        'trigger-condition' => 'Page load');
        }

        public function load(){
            return $this->__trigger();
        }

        public static function allowEditorToParse(){
            return false;
        }

        public static function documentation(){
            return new XMLElement('p', 'This is an event that sets the content-type header of Symphony pages to <em>text/css</em>');
        }

        protected function __trigger(){
            header("Content-Type: text/css");
        }       

    }

?>

Thanks Michael. That's exactly what I have been coming up with, too :)

Just noticed that the above event is not working anymore. Nor does the code Alistair posted above (for RC1).

So now I have a problem...

How can I add CSS or JavaScript headers to pages in Symphony 2? All ideas are highly appreciated!

For the moment I solved the problem by hacking /symphony/lib/toolkit/class.frontendpage.php. I shouldn't do that, but I needed a quick solution because a live website was partially broken.

(In the past months I learned that Symphony may be used to generate every type of content, not just HTML and XML, but CSS and JavaScript pages as well. I have some live implementations of this on one website. For this to work well in any browser the correct HTTP headers are a must, I think. My solution broke when I updated from beta to Symphony 2.0.)

As a quick solution I added page types "CSS" resp. "JS" to the pages in question; then in /symphony/lib/toolkit/class.frontendpage.php I added the following in line 48:

elseif(@in_array('CSS', $this->_pageData['type']) || @in_array('css', $this->_pageData['type'])){
    $this->addHeaderToPage('Content-Type', 'text/css; charset=utf-8');
}

elseif(@in_array('JS', $this->_pageData['type']) || @in_array('js', $this->_pageData['type'])){
    $this->addHeaderToPage('Content-Type', 'application/x-javascript; charset=utf-8');
}

Should this functionality (page types like "CSS" and "JS") maybe become a part of the Symphony core? I am not sure about that.

Maybe someone can help to build new events and/or extensions to set those headers correctly without needing dirty hacks.

Should this functionality (page types like "CSS" and "JS") maybe become a part of the Symphony core? I am not sure about that.

My intention was to allow some Content/Type mappings in an extension, or possibility the core. By default it would only have

xml => 'text/xml; charset=utf-8'

or similar. but potentially could add more. This has been asked for a few times, and also is something we'd like too.

Sounds great!

So for the moment I should stay with my hack? Or is there a simple way to make the event(s) work again?

You could use an Extension and make use of the FrontendOutputPreGenerate delegate.

    public function getSubscribedDelegates(){
        return array(
                    array(
                        'page' => '/frontend/',
                        'delegate' => 'FrontendOutputPreGenerate',
                        'callback' => '__addCustomHeader'
                    ),

                );
    }

    public function __addCustomHeader($context){
        .... EH .....
    }

BUT, the problem is you're not given the page data, so figuring out what the page type will be a pain. To be honest, your hack is probably the easiest way around it right now. I'll look at making the change I mentioned soon.

Thanks a lot!

I think that there is already a solution in Symphony, just set text/css in the page type.

@rowanji: No, this does not change the page's HTTP response headers. Are you sure it works for you? (Wouldn't Alistair know about this functionality if it was there?)

Hmm, I could swear it was in there! Anyhow, perhaps I can improve the SEO Manager extension to send page headers as needed.

Thank you very much, rowanji. But as long as there is no increased community interest in sending custom response headers, we might as well wait for Alistair's solution. This is OK for me, since my hack is easy to implement.

Does anyone have a working solution for adding headers without the need of modifying core files?

You can easily add headers with the global parameter loader extension that I wrote.

@carsten: I tried it, but it doesn't work with the conetnt-type header. Seems it is (re-)created later by Symphony.

Open class.frontendpage.php and add the following code at line 48:

 elseif(@in_array('custom', $this->_pageData['type'])){
  // do nothing and set header through global parameter loader extension
 }

Now, if you go to your page and fill in "custom" as Page Type, you can use the global parameter loader extension to set the content-type header. Alternatively, if you don't want to fill in custom as Page Type, comment out line 49 in class.frontendpage.php: $this->addHeaderToPage('Content-Type', 'text/html; charset=utf-8'); to make the extension work. It seems that the text/html content-type is assigned also without this line, but use the first alternative to be on the safe side.

Create an account or sign in to comment.

Symphony • Open Source XSLT CMS

Server Requirements

  • PHP 5.2 or above
  • PHP's LibXML module, with the XSLT extension enabled (--with-xsl)
  • MySQL 5.0 or above
  • An Apache or Litespeed webserver
  • Apache's mod_rewrite module or equivalent

Compatible Hosts

Sign in

Login details