I’m writing a plugin.
Below is code that route a css file to another url. It works but I think the it’s a bit messy.
<?php
kirby()->routes(array(
array(
'pattern' => 'myplugin.css',
'action' => function() {
header("Content-type: text/css; charset: UTF-8");
$path = kirby()->roots()->plugins() . DS . 'myplugin' . DS . 'assets' . DS . 'css' . DS . 'myplugin.css';
tpl::load( $path, array(), false );
}
)
);
Is there a way I can just add a link to the CSS file? Now I set a header and pretend that the CSS file is a template, which feels wrong.
I do not need to parse it in anyway.
You can use the following in your main plugin file:
<?php
kirby()->routes(array(
array(
'pattern' => 'myplugin.css',
'action' => function() {
$path = __DIR__ . DS . 'assets' . DS . 'css' . DS . 'myplugin.css';
return new Response(file_get_contents($path), 'css');
}
)
);
Nice try, but in this case I will continue to use my solution. On many hostings file_get_content
s are disabled by security reasons. My hosting for example:
https://support.loopia.se/wiki/alternativ-till-file-get-contents/
I could use cURL but that would be much more code.
I don’t think your hosting provider will ever disable file_get_contents()
itself. Only getting files over HTTP is sometimes disabled. But in my code, I get the file from the file system. Are you sure it doesn’t work?
Ahh, now I remember. They are just blocking full urls like “http://…”. Paths should be fine. Thanks! 
Or to do it the Kirby way you can use f::read()
instead of file_get_contents()
2 Likes
Well, yes. But adding additional Kirby-specific code to such simple examples is a bit overkill in my opinion. More to research for beginners. 
Well, once you know it is there, it makes your life easier, less to type in simple examples. And you don’t want to tell me that @jenstornell still qualifies as beginner? 
Are you calling me a beginner?
Everything is relative I guess… *feeling like a pro.
You are absolutely not a beginner. 
But the whole point of a forum is that other people can use the solutions as well. 
2 Likes
I think it’s nice to have both versions, the file_get_contents
and the f::read
. Then me and other people can choose if we want to use plain PHP syntax or Kirby syntax.
I usually go for Kirby syntax when available because it often makes the code cleaner and shorter. For the ones who want to make more independent stuff I guess the PHP syntax is better.
Thank you both!
2 Likes