NotionCommotion Posted February 25, 2015 Share Posted February 25, 2015 Playing around with https://code.google.com/p/minify/. I would like to add an PHP generated JavaScript file to it such as /somedirectory/getsomeconstants.php?x=123. How do I do so? The file will rarely change, but sometimes it will. How does minify know whether it needs to pull a new copy instead of using a cached copy? Thank you Quote Link to comment https://forums.phpfreaks.com/topic/294900-add-php-file-to-minify/ Share on other sites More sharing options...
kicken Posted February 26, 2015 Share Posted February 26, 2015 Custom Source Read that page, it shows you how you can setup a source that is dynamic. Basically you'd just make a function that does whatever your current getsomeconstants.php file does and return the result as a string. Set that up as a source then include whatever the appropriate URL is for that source in your HTML files. Quote Link to comment https://forums.phpfreaks.com/topic/294900-add-php-file-to-minify/#findComment-1506787 Share on other sites More sharing options...
NotionCommotion Posted February 26, 2015 Author Share Posted February 26, 2015 Custom Source Read that page, it shows you how you can setup a source that is dynamic. Basically you'd just make a function that does whatever your current getsomeconstants.php file does and return the result as a string. Set that up as a source then include whatever the appropriate URL is for that source in your HTML files. Not sure I understand. It is my understanding that Minify allows me to use some crazy URI such as the following, and retrieve all of the files as one minified file, and uses a cached version if it was ever accessed by anyone. Correct? My desire is to just add some more dynamically created JS (which will rarely or never change) to the content returned by this URI. I would have expected I could add /lib/getsomeconstants.php?x=123 to the URL, however, this results in a 400 error. How would I know what "include whatever the appropriate URL is for that source in your HTML files." is to include it? <script src="/min/?f=/lib/plugins_3rd/jquery/jquery-1.11.2.js,/lib/plugins_3rd/jquery-ui/jquery-ui-1.11.2.custom/jquery-ui.js,/lib/js/general.js,/lib/components/back/common/js/common.js,/lib/components/back/common/js/help.js,/lib/plugins/printIt/jquery.printIt.js,/lib/plugins_3rd/handlebars-v2.0.0.js,/lib/plugins/ajaxTip/jquery.ajaxTip.js,/lib/components/back/common/js/linkPreview.js,/lib/plugins/toolTip/jquery.toolTip.js,/lib/plugins_3rd/moment.js,/lib/plugins/deleteRecord/jquery.deleteRecord.js,/lib/plugins_3rd/jquery-validation/dist/jquery.validate.js,/lib/plugins_3rd/jquery-validation/dist/additional-methods.js,/lib/js/my-validation-methods.js,/lib/plugins/populateAddress/jquery.populateAddress.js,/lib/components/back/common/js/address_detail.js,/lib/plugins_3rd/jquery.maskedinput-1.3.1.js,/lib/plugins_3rd/fineuploader-3.1/fineuploader-3.1.js,/lib/components/back/common/js/notes.js,/lib/components/back/common/js/private_documents.js,/lib/plugins/sortfixedtable/jquery.sortfixedtable.js,/lib/plugins/addBid/jquery.addBid.js,/lib/components/back/com_projects/js/detail.js" type="text/javascript"></script> Quote Link to comment https://forums.phpfreaks.com/topic/294900-add-php-file-to-minify/#findComment-1506806 Share on other sites More sharing options...
kicken Posted February 26, 2015 Share Posted February 26, 2015 Note: I haven't used this library, so the following is just based on what I've gathered after spending a few minutes reading the docs. What you can do is rather than putting some absurd URL like that into your HTML pages, you make a PHP page that consolidates your scripts and serves them minified. By doing this you can not only include static files (like you do now) but also dynamic contents (which you want to do). Look at the script they have under 'Performance Considerations' on the page I linked above for an example. <?php // myServer.php /** * This script implements a Minify server for a single set of sources. * If you don't want '.php' in the URL, use mod_rewrite... */ // setup Minify set_include_path('path/to/min/lib' . PATH_SEPARATOR . get_include_path()); require 'Minify.php'; require 'Minify/Cache/File.php'; Minify::setCache(new Minify_Cache_File()); // guesses a temp directory function src1_fetch() { return file_get_contents('http://example.org/javascript.php'); } // setup sources $sources = array(); $sources[] = new Minify_Source(array( 'id' => 'source1' ,'getContentFunc' => 'src1_fetch' ,'contentType' => Minify::TYPE_JS ,'lastModified' => max( filemtime('/path/to/javascript.php') ,filemtime('/path/to/javascript_input.js') ) )); $sources[] = '//file2.js'; $sources[] = '//file3.js'; // setup serve options $options = array( 'files' => $sources, 'maxAge' => 86400, ); // handle request Minify::serve('Files', $options); Where they have //setup sources is where you'd list out your files as well as a dynamic source. For example: function getsomeconstants(){ return 'var CONSTANTS='.json_encode(array( 'blah' => 'blah' )); } // setup sources $sources = array(); $sources[] = new Minify_Source(array( 'id' => 'constants' ,'getContentFunc' => 'getsomeconstants' ,'contentType' => Minify::TYPE_JS ,'lastModified' => 123 )); $sources[] = '//lib/plugins_3rd/jquery/jquery-1.11.2.js'; $sources[] = '//lib/plugins_3rd/jquery-ui/jquery-ui-1.11.2.custom/jquery-ui.js'; $sources[] = '//lib/js/general.js'; $sources[] = '//lib/components/back/common/js/common.js'; //... Save that script as a PHP file somewhere, maybe in /min/common.php then in your HTML files just do: <script type="text/javascript" src="/min/common.php"></script> Quote Link to comment https://forums.phpfreaks.com/topic/294900-add-php-file-to-minify/#findComment-1506879 Share on other sites More sharing options...
NotionCommotion Posted February 27, 2015 Author Share Posted February 27, 2015 Note: I haven't used this library, so the following is just based on what I've gathered after spending a few minutes reading the docs. Do you use a different library or have you created your own similar script? Upon your very brief review of this library, do you have any opinions? What you can do is rather than putting some absurd URL like that into your HTML pages, you make a PHP page that consolidates your scripts and serves them minified. By doing this you can not only include static files (like you do now) but also dynamic contents (which you want to do). Ah, I get you. Yea, the URL is rather absurd at about 1,000 characters. One concern about this approach is that every page requires different resources. I suppose I could use /min/common1.php, /min/common2.php, /min/common2.php..., or /min/common.php?pg=1, /min/common.php?pg=2, /min/common.php?pg=3..., but this will require moving the selection of the resources from my template to some other file which is not as intuitive. Furthermore, my pages are not called 1, 2, and 3, but page=users&controller=list, page=shop&controller=detail, etc, however, I suppose I could deal with this. Quote Link to comment https://forums.phpfreaks.com/topic/294900-add-php-file-to-minify/#findComment-1506923 Share on other sites More sharing options...
kicken Posted February 27, 2015 Share Posted February 27, 2015 What I would do is just make common.php include whatever scripts are used on nearly every page. Then on pages that require additional resources you can do a second script tag with the traditional list of URLs. eg: <script type="text/javascript" src="/min/common.php"></script> <script type="text/javascript" src="/min/?f=/additional.js"></script> Or you could extend common.php to accept the same type of ?f= parameter and just do <script type="text/javascript" src="/min/common.php?f=/additional.js"></script> Do you use a different library or have you created your own similar script? Upon your very brief review of this library, do you have any opinions? I don't bother combining scripts. I'll minify them using google's Closure Compiler prior to push things onto the production server but that's about it. Each script still has it's own script tag. I don't use a lot of scripting so it's not a big deal. For the most part all i have is jQuery, jQuery UI, a single site-wide script and sometimes an additional page specific script. jQuery/jQueryUI are loaded from a CDN rather than locally to take advantage of the fact that the CDN url is likely cached already. If I did do something that required more scripting, I'd likely still just do the minify and concatenation of scripts as part of the release process rather than dynamically using a library such as this. I use PHPStorm as my development environment and it has a feature called File Watchers where I can set it up to automatically run some program any time I make a change to a file. That makes it easy to do things like use SCSS or minified JS without having to use some server-side script to do the compilation on the fly. Any time I make a change PHPStorm will re-compile the files automatically so I can just reload the browser as normal. If you don't have such a system in place, then libraries such as this are good alternatives to make development easier. This one seems fine on the surface. I didn't go digging around in the source of it looking for problems but based on the examples it seems to be easy to use and flexible. 1 Quote Link to comment https://forums.phpfreaks.com/topic/294900-add-php-file-to-minify/#findComment-1506971 Share on other sites More sharing options...
NotionCommotion Posted February 27, 2015 Author Share Posted February 27, 2015 Thanks Kicken, Have a good weekend! Quote Link to comment https://forums.phpfreaks.com/topic/294900-add-php-file-to-minify/#findComment-1507006 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.