0
votes

i am working on a Cakephp 2.3.. I worked with minify using this plugin

https://github.com/maurymmarques/minify-cakephp

but i am not able to seem it to worked .. my website is not correctly loading the css and js ..

i copied the folder into app/plugins directory , importing all the css and js in the default.php file which is in layouts folder i am doing this

echo $this->Minify->css(array('reset', 'style','colors'));

and in browser the url becomes like this

<link rel="stylesheet" type="text/css" href="/cakephp/min-css?f=cakephp/css/reset.css,cakephp/css/style.css,cakephp/css/colors.css

when i click the url i get this error

  400 Bad request 
 Please see http://code.google.com/p/minify/wiki/Debugging.

i am calling helper in my appController

 class AppController extends Controller {

public $helpers = array('Minify.Minify');
1
HTTP 400 is often caused by the security component, it throws a bad request exception in the blackhole handler. Maybe you are using any of the SecurityComponent:require* methods or something. Disable the component temporarily to check whether this is the problem. - ndm
@ndm yeah i was using the blackhole security component .. but know i hhave removed the code but still it displaying the same message - hellosheikh
please help me i am really stuck here - hellosheikh
I've just realized that the URL you've added to the error is actually part of the error message, so it seems to be clear that it's the minifier itself issuing the 400 response. Have you already tried to debug using FirePHP as described on that page? If so, what are the results? - ndm
Then please do so and post the results, I have a suspicion that it's a problem with the asset URL to file conversion, resulting in non-existent paths. - ndm

1 Answers

2
votes

I've tried it with a setup similar to yours, ie CakePHP 2.3 in a subfolder, and with this setup I do get an 400 error too (including error messages in FirePHP!).

After digging through the sources I found two culprits, one being Asset::getAssetFile() in Minify/Utility/Routing/Asset.php, where the proper webroot path for the assets is generated, and asset URLs are being "converted" to asset file paths. These values are used in the minifiers config file (Minify/Vendor/minify/config.php) to overwrite the values passed in the f URL variable and to overwrite the $_SERVER['DOCUMENT_ROOT'] variable, which is then later on used by the minifier to locate the asset file. The problem here is that for theme and plugin assets it converts the paths folder separator from / to DS, which will led to \ being used on Windows systems, causing the minifier to fail because it doesn't allow backslashes. To fix that, exchange implode(DS, $parts) for implode('/', $parts).

https://gist.github.com/ndm2/6192506

Another problem with this method is that it can only handle assets from one location, ie either normal assets, plugin assets, or theme assets. However this is probably more of a feature limitation rather than a bug.

The other culprit is the Minify helper. When CakePHP is located in and accessed via a subfolder path, then this subfolder path is prepended to the asset URLs generated by the HTML helper that is being utilized in the Minify helper. The problem with this is that in the minifier the $_SERVER['DOCUMENT_ROOT'] path will be directly concatenated with the asset file path (both being prepared by Asset::getAssetFile() as mentioned before), resulting in file paths like /cakephp/app/webroot/cakephp/css/style.css, where /cakephp/app/webroot/ is the webroot file path, and cakephp/css/style.css is the asset file path (where in fact it's still a URL fragment), and of course these files do not exist. A quick and maybe dirty fix for this one would be to exchange

array_push($files, substr($this->assetUrl($asset, $options), 1));

for

array_push($files, mb_substr($this->assetUrl($asset, $options), mb_strlen($this->request->webroot)));

in the MinifyHelper::_path() method located in Minify/View/Helper/MinifyHelper.php. This would strip the subfolder part from the paths, resulting in correct file paths being generated

https://gist.github.com/ndm2/6192504

Please have in mind that both of these possible fixes would need some testing to make sure there are no side effects!

Now if you don't use a Windows system and/or do not use theme and/or plugin assets, then you can ditch the first fix, and go over to the subfolder problem. As mentioned the possible fix needs further testing, so if you are unsure about that, then another solution would be to move your CakePHP installation out of the /cakephp/ subfolder into the document root (or change the document root to the subfolder where you CakePHP installation is located in case this is applicable).

Now that was a long write, I hope what I've found is actually the same problem that you are experiencing. On a side note, it might be worth reporting the issues to the plugin developer anyways.