We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 here.

How to hash the js file using asset management

Hi,

I work with Ruby on Rails and on my dev machine my css files might be named application.css and ui.css or whatever but when rails deploys it will compile all those files into one file with a name like:

application-bec2abb573c652a8d453f24b457e389d.css

How can we do it in phalcon? I would like to hash the filename.

advice appreciated.

the minification you can do it with the asset manager. A plain file name should be fine, but if you really want hashing, md5 should be enough for this purpose.

It works for both css and js: https://docs.phalcon.io/en/latest/reference/assets.html#minification-filtering



27.8k

Hi there,

Thanks for reply. I am already using asset manager. Noticed we have to set the filename. How do we md5 it?

// The name of the final output
->setTargetPath('final.js')

// The script tag is generated with this URI
->setTargetUri('production/final.js')
edited Jul '15

the md5 utility is somewhere on the OS, so you'll need to know if your server has md5sum or md5 and where to get it, then in PHP use exec(). I have Windows 8.1 on my other PC, but kind of lazy to go check if it has MD5, I think it doesn't, I think its a 3rd party thing on Windows.

On Macs md5 is reachable anywhere so there is no need for the full path to it, your server may need the full path, you'll need a bit of trial and error to find out. Try a few probes in PHP:

echo exec('whereis md5'); // or md5sum, or just try ...
echo exec('md5 path/to/somefile.css');

Actual anwser to your question is:

$checksum = exec('md5 -q /Users/edward/Desktop/example.css');
// -q is quite mode, only the checksum is returned, without it, you'll get:
// MD5 (/Users/edward/Desktop/example.css) = 5e7f4d0bfa9da0c1527086c373dfc15f
// ... ain't got time for dat!
// btw that flag stuff is described in the man pages
// man md5


16.0k
Accepted
answer
edited Jul '15

@Derek md5 is part of php. You don't need any exec.

->setTargetPath(md5("final") . ".js")

https://php.net/md5

@Edward don't forget that phalcon is a php framework. No point in overcomplicating things when php already provide you the needs of hashing



27.8k

Thank you guys!

@stefan Oh right, there is md5_file() and sha1_file(), and more, that's new to me, thanks. Normally I don't do it this way, I'll just append a time stamp so the CSS or JS isn't cached.