How does the header option work

I´m trying to send some additional headers with the header option found in the docs. I did something like this in the config.php:

c::set('header', array('home'=>'header-key: some-value'));

but it doesn´t work.

Does somebody know, how this options should be used?

I´m currently trying to solve this topic, but have to understand the normal function before.

It has to be a callable function or method (and also the option is called headers):

c::set('headers', array(
  'home' => function() {
    header('Header-Key: some-value');

I think your example is a tad better than the one in the docs :grin:

Thank you for that answer. I think this is quite complicated for calling the header function one or multiple times. But appending multiple headers could´t be solved in another way, i think.
You can´t give multiple headers to the header() function at one, right?
And what is the best way to enhance the documentation? Simply a pull request to the repository?

I was looking an the core code and asked myself, why there is a check, if the string is callable. But if it´s not a string, but a function, this makes sense.

That the option is called headers was only a typo in the post. It´s fine in my code.

That’s true. Maybe the core should also support a string (like in your example) or an array for multiple headers.

I don’t think so.

Yep, exactly. :smile:

Actually it could be a string (name of the function to use) or an array (class or instance and method name). So is_callable() === true does not necessarily imply that is_string() === false. #phpIsBest :smiley:

Yes I know. I researched a bit about the is_callable() function. But only giving a function name does not get me to the result I want to have - adding some response headers to my website. But thank you.

I will continue my work on a wildcard für the template given to the headers option tomorrow. And I will experiment a bit with making this option more intuitive (simply passing the headers to append instead of passing a whole function).

Yes, but for more complex operations it will be better to have the currently implemented version. It´s a little bit more to write, but I think, that the headers option is something quite special. Not everyone has to use it.
Adding a second version to define which headers should be send, would add complexity which is not necessary. What do you think? I will simply enhance the docs, so that everyone has an example how to do it.

Just and update:

I have created an pull request with updated docs:

And the update, adding a wildcard functionality to the headers option, is also ready: kirby/pull/294

1 Like