I'm new to cakephp and I found this in the manual:

It’s a well known fact that serving assets through PHP is guaranteed to be slower than serving those assets without invoking PHP. And while the core team has taken steps to make plugin and theme asset serving as fast as possible, there may be situations where more performance is required. In these situations it’s recommended that you either symlink or copy out plugin/theme assets to directories in app/webroot with paths matching those used by CakePHP.

app/Plugin/DebugKit/webroot/js/my_file.js 
   becomes app/webroot/debug_kit/js/my_file.js
app/View/Themed/Navy/webroot/css/navy.css 
   becomes app/webroot/theme/Navy/css/navy.css

Are files in plugin/webroot/asset required to be read by PHP then inserted into HTML rather than served directly by the server itself because really isn't a webroot directory that can be accessed by the http module?

The manual says soft links will speed this process up. Does cakephp first look in /app/webroot/asset then call the dispatcher to find it in plugin/webroot/asset and read it and serve it?

Or is the process identical in how the file is found/read except cake must use the dispatcher to locate the asset if it is not in the app/webroot/asset location?

有帮助吗?

解决方案

For serving files...

Webservers are fastest

The default rewrite rules are as follows:

RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]

That means if the request is for a file that the webserver can see - don't talk to php just respond with the file's contents (or appropriate headers). In this circumstance there is no "Does cakephp first look in /app/webroot/asset ..." as there is no CakePHP - or PHP - involved in handling the request at all.

So, in brief that's:

Request 
-> webserver 
  -> check if file exists
    -> response (file contents)

If a different webserver (not apache) is being used, CakePHP expects equivalent rewrite rules. It will never check if the equivalent of app/webroot/<the current url> exists - as the webserver should be doing that itself.

PHP is slow

If the request is for a file that does not exist in the webroot, things are much slower because quite simply there's more processes involved. Even a php script like so:

<?php
// example app/webroot/index.php
$path = 'server/this/file.html';
echo file_get_contents($path);
exit;

is slower than an equivalent request handled directly by a webserver, as that's:

Request 
-> webserver 
  -> check if file exists
    -> invoke php 
      -> get file contents 
    -> respond to webserver 
  -> response

Plus php wasn't specifically designed for serving files (like a webserver is or should be) and so is inherently slower than a webserver alone at doing so.

CakePHP is slower

The only path that is directly web-accessible for a CakePHP project is ´app/webroot`.

For a request handled by CakePHP, even using the Asset dispatch filter (which is a slimmed down dispatch process) - obviously there is more logic involved, so it's slower than the bare minimum logic required to server a file with php. In brief the request becomes:

Request 
-> webserver 
  -> check if file exists
    -> invoke php 
      -> Bootstrap CakePHP
      -> Dispatch Request
        -> Check Dispatch filters
          -> check if request matches a configured plugin/theme file path
            -> check if file exists
          -> generate response
        -> output response
    -> respond to webserver 
  -> response

The difference in performance compared to letting the webserver handle the request for a static file can be very significant.

Conclusion

Serving files with php when it's not necessary is a waste of resources, if at all possible allow the response to come from higher up the request - the webserver, a proxy or preferably the user's own browser cache (~0 latency!).

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top