Looking to figure out how to measure the total PHP execution time of a CakePHP site. It looks like in 1.2 this was included in the rendered HTML as a HTML comment when in debug mode, but this is not happening on my 1.3 site, and in any case I want it as an element I can output to the user, not a comment.
I can do this easily in regular PHP using microtime() but I'm not sure where to add the code in CakePHP, and I suspect it might have a more robust execution timer anyway. Ideas?
Just in case anyone else is curious, I solved this by adding the following code to my layout.ctp. You could also do this in the controller and pass it in as a variable, which might be a little more classic MVC-friendly, but I wanted this on every page of the site without duplicating code in each controller.
Page rendered in <?php echo round((getMicroTime() - $_SERVER['REQUEST_TIME']) * 1000) ?>ms.
Use Debug Kit. Among other functionality, you can grab the total execution time via
DebugKitDebugger::requestTime()
This may not be the "right" way to do it, but you can add the following PHP code back into app/webroot/index.php (at the very end). Then if you have debug on > 0, you'll get the old 1.2 functionality back.
if (Configure::read() > 0) {
echo "<!-- " . round(getMicrotime() - $TIME_START, 4) . "s -->";
}
Related
I am trying to redirect everything from /app/.... to a certain page - will use that later for angular js.
This is what I came up with :
add_rewrite_rule('/app/.*', 'index.php?page_id=11', 'top');
However this doesn't work...
In settings->permalink when I set it to: domain/sample-post/ - it sami-working domain.com/app/{number} will work and redirect to app - nothing else though. like : domain.com/app{anything else} wont work.
When I try different permalink setting I get different results.
what you probably want is:
add_rewrite_rule('/app.*', 'index.php?page_id=11', 'top');
But you need to tighten up the url structure as much as possible, for example the above will match /appetizer /applease etc. since they are all leading to the same page your first attempt was probably correct, /app/ and then add whatever you want after the /
Could anyone tell me if there's some way of "hooking in" to DotNetNuke so that I can, for example, search and replace text for ALL HTML modules on the site?
e.g. if I use an HTML editor and enter the text {{replace_me}}, then I could have some code that detects "{{replace_me}}" every time a page is rendered and replace it with something else.
Please note that this is a simple example - there may be other ways of "replacing" text - however the actual use case we have is very specific and there will be some significant processing to decide what to replace :) - so whatever solution we implement should basically be:
Get HTML from DB -> Process it however we wish in full C# -> Deliver the modified string.
Thanks!
I believe you can do this with the use of an HTTPModule. Ifinity.com.au used to sell a module that did this, looks like you might be able to download it now for free (maybe?) at http://www.ifinity.com.au/Products/Inline_Link_Master/Product_Details
I know that it's good to minify assets because doing so reduces their file size, which reduces the amount of time it takes for the page to load. I also know that it's good to combine assets because doing so reduces the number of HTTP requests, which, once again, reduces the amount of time it takes for the page to load. This is important because there are still people on dial-up and mobile devices often don't have a fast connection.
The thing I'm struggling with is how to easily add asset minification and combining into my workflow. I develop locally using CakePHP and I use Git for version control. When it's time to go live, I ssh into the server hosting the live site and merge in the latest commit.
Here's how I would go about rolling my own solution (only accounts for minification and is not tested!):
1.) My development environment's "app/Config/core.php" file would always have its "debug" level set to a value greater than 0 and the production environment's would always be at 0.
2.) On the file system, all CSS and JavaScript would be stored in external files, like so:
app/webroot/css/used-site-wide.css
app/webroot/css/used-on-a-few-pages.css
app/webroot/css/used-on-one-page.css
app/webroot/js/used-site-wide.js
app/webroot/js/used-on-a-few-pages.js
app/webroot/js/used-on-one-page.js
3.) Rather than using echo $this->Html->script(array('used-on-a-few-pages', 'used-on-one-page'), array('inline' => false)); in the view file, I would use this:
Configure::write('external_js', array('used-on-a-few-pages'));
Configure::write('inline_js', array('used-on-one-page'));
4.) Rather than using echo $this->fetch('script'); in the layout file, I would use this:
if (Configure::read('external_js') !== null) {
$external_js = Configure::read('external_js');
if (Configure::read('debug') == 0) {
foreach ($external_js as &$external_js_filename) {
$external_js_filename .= '-min';
}
}
echo $this->Html->script($external_js);
}
if (Configure::read('inline_js') !== null) {
$inline_js = Configure::read('inline_js');
if (Configure::read('debug') == 0) {
foreach ($inline_js as &$inline_js_filename) {
$inline_js_filename .= '-min';
}
}
echo "\n<script type=\"text/javascript\">\n\t/* <![CDATA[ */";
foreach ($inline_js as $inline_js_filename) {
echo file_get_contents(JS . Configure::read('inline_js') . '.js');
}
echo "\n\t/* ]]> */\n</script>";
}
5.) Finally, I would set up Git to create the minified assets whenever a commit is made.
Using this setup, I would be working with the unminified assets in development and the minified ones in production. The thing is, I don't want to re-invent the wheel if I don't have to. I believe that re-inventing the wheel should only be done if you're solving a problem that is both significant and uncommon.
How do you all handle this?
Thanks!
If you're after something more simple than Mark Story's AssetCompress plugin, check this out:
https://github.com/joshuapaling/CakePHP-Combinator-Plugin
It will combine and minify JS and CSS files, and you can easily make it only combine/minify when debug mode is 0 (there's a example of that in the .markdown on GitHub). It uses the date modified of the included JS/CSS files to decide when it needs to make a new cached file.
It isn't nearly as full-featured as Mark Story's plugin, but it is simple, does the job, and it should only take you 10 or 15 mins to set up.
I've recently stumbled upon this situation myself and I have tried the (now deprecated) Combinator plugin which uses jsmin that isn't very reliable. So then I tried Mark Story's plugin which is way to complicated and it doesn't even do the builds automagically when files have changed, you have to do some cake bake every time you wish to create the combined and minified files.
Therefore I have wrote my own simple helper which you can check out here: https://github.com/Highstrike/cakephp-compressor. Everything is explained in the readme file and it's very easy to use, taking advantage of google closure for js minification. It also handles HTML minification.
It's even easier than maurymmarques's solution because it's just a helper file, no controller or configuration needed.
I'm hoping it will help someone.
You also can check this plugin https://github.com/maurymmarques/minify-cakephp
It's very easy to install and configure.
I've taken a reCaptcha plugin from this guy
(github link of the plugin)
I've entered the following code form in my view:
[form creation]
[table]
[inputs]
[/table]
echo $this->Recaptcha->show(array('theme' => 'white'));
echo $this->Recaptcha->error();
[/form]
I've followed the steps suggested, and the reCaptcha window appears properly, but no matter what I enter in the captcha, it never gets verified and I always receive the 'message' field of beforeValidate (I've set it to "You've entered a wrong message" etc).
I'm not even sure how to debug it to see at which point it fails. Even if I just replace all the code in checkRecaptcha function with "return true" to try and skip the validation with the keys and just see if the rule itself is correct, it still remains the same, and I'm generally not getting any of the specific incorrect-captcha-sol messages that I read around.
Am I correct to assume that the only code I need inside my controller function (assuming I've already included the component and helper in the controller) is Configure::load('Recaptcha.key'); and no further manual validation checks?
(unfortunately I can't link you my whole project due to rights)
I had a similar issue. Try removing the 2 response and challenge field lines in the component and overwrite them with these:
$controller->$modelClass->set('recaptcha_response_field',
$controller->request->data['recaptcha_response_field']);
$controller->$modelClass->set('recaptcha_challenge_field',
$controller->request->data['recaptcha_challenge_field']);
For some reason my cakephp application is not showing any of the queries made to the database. It prints the table fine, but there are not records. What could cause this?
Check to make sure you are pulling the records correctly.
$models = $this->Model->find('all');
// or
$this->Model->recursive = 0;
$this->set('models', $this->paginate());
Then when you add them in the view, be sure you are looping through them correctly:
foreach ($models as $model) {
echo $model['Model']['field_name'];
}
UPDATE
To show the SQL statements, be sure you have the following set in core.php
Configure::write('debug', 2);
Also, in the Layout, besure you have this included someone between the <body> and </body> tags:
<?php echo $this->element('sql_dump'); ?>
I assume that you are getting an empty table with just the "Nr","Query","Error", etc. column headers?
You are getting the empty table because you have "Configure::write('debug',0);" set somewhere before you have "Configure::write('debug',2);" set. Find the first instance of it and delete it or change it to "2".
I know that you have long since fixed this problem but hopefully it helps somebody else in the future.
The CakePHP debug kit can help you. After you install it, you will notice a small (pie-chart) icon on the top right of your CakePHP pages. Clicking on that will allow you to see various useful information, and most importantly for this issue, all the SQL queries that occurred in the back-end upon the page loading.
I faced a similar problem in cakephp. I found that debug($variable) has limitation to the content size. Since you are fetching huge sized content from database, it is not able to print. Try doing print_r($variable) instead. To format it properly, you can do like this
echo "<pre>".print_r($variable)."</pre>";