How do I dump and print variables in Yii for debugging? I would like to use var_dump() or print_r(). I tried to use Yii::trace() but it crashes with this error in runtime/logs/app.log. It doesn't even tell me the line in my code that it fails.
2015-03-18 20:54:11 [::1][-][-][warning][yii\log\Dispatcher::dispatch] Unable to send log via yii\debug\LogTarget: Exception 'Exception' with message 'Serialization of 'SimpleXMLElement' is not allowed'
in /cygdrive/c/Users/Chloe/workspace/AffiliateArbitrage/vendor/yiisoft/yii2-debug/LogTarget.php:58
Stack trace:
#0 /cygdrive/c/Users/Chloe/workspace/AffiliateArbitrage/vendor/yiisoft/yii2-debug/LogTarget.php(58): serialize(Array)
#1 /cygdrive/c/Users/Chloe/workspace/AffiliateArbitrage/vendor/yiisoft/yii2-debug/LogTarget.php(112): yii\debug\LogTarget->export(Array)
#2 /cygdrive/c/Users/Chloe/workspace/AffiliateArbitrage/vendor/yiisoft/yii2/log/Dispatcher.php(183): yii\debug\LogTarget->collect(Array, true)
#3 /cygdrive/c/Users/Chloe/workspace/AffiliateArbitrage/vendor/yiisoft/yii2/log/Logger.php(170): yii\log\Dispatcher->dispatch(Array, true)
#4 [internal function]: yii\log\Logger->flush(true)
#5 {main}
Reference
http://www.yiiframework.com/doc-2.0/guide-runtime-logging.html
Because you are asking about something like var_dump and print_r, I can advise built-in helper for this. It's called yii\helpers\VarDumper. Yii::trace() is for logging trace messages.
VarDumper is intended to replace the buggy PHP function var_dump and print_r.
It can correctly identify the recursively referenced objects in a
complex object structure. It also has a recursive depth control to
avoid indefinite recursive display of some peculiar variables.
VarDumper can be used as follows,
VarDumper::dump($var);
Personally I don't use it, just tried only couple of times for testing.
I think is better to use Xdebug for that purposes.
See also PsySH.
Use this:
<?php echo '<pre>'; print_r($model); exit; ?>
I used this, but I'm sure there's a better way.
Yii::warning('**********************', var_export($category,true));
config/web.php
'log' => [
...
'flushInterval' => 1, // for debug
'targets' => [
[
...
'exportInterval' => 1, // for debug - slow
],
],
],
Use this, to see your variable or object array.
use yii\helpers\VarDumper;
VarDumper::dump($variableArray , $dept = 10, $highlight = true);
To detail you can read the doc
http://www.yiiframework.com/doc-2.0/yii-helpers-basevardumper.html#dump()-detail
You can do it yourself:
In your main index page (back/index.php or front/index.php) add this code to the top ob_start();. Then define 2 functions for better debugging
function dd($v){
ob_clean();
var_dump($v);
exit;
}
function dj($v){
ob_clean();
echo CJSON::encode($v);
exit;
}
and add ob_end_flush(); at the very end of your main index page . Now you can call dd($model) or dj($model). There you had your dumper working. Congratulations!
I had written an article about this problem with debug variables in Yii 2:
Since for the sake of Simplicity and Quick Development, I’ve created a helper for those who use Yii. You can just call dd($var1, $var2, ….); for dump & die or d($var1, $var2, ….); for dump data.
Details and installation instructions are on: https://dangnhsite.wordpress.com/2016/04/06/variable-debug-in-yii-2/
Related
I'm working on a side project and I've been trying to interact with the WooCommerce REST API.
I have everything seemingly correct. I've created the proper keys because they work with cURL. But when I try to utilize the php-wrapper, provided by Automattic, everything stops working.
I have the code set up exactly like they've instructed:
require DIR . '/../vendor/autoload.php';
use Automattic\WooCommerce\Client;
$woocommerce = new Client(
'https://mywebsitenamegoeshere.com',
'##########',
'##########',
[
'wp_api' => true,
'version' => 'wc/v2',
'query_string_auth' => true
]
);
I'm simply trying to do a request to list the endpoints (which also works with cURL) but as soon as I attempt to interact via the php-wrapper I get this error message:
Fatal error: Uncaught exception 'Automattic\WooCommerce\HttpClient\HttpClientException' with message 'Syntax error' in /nas/content/live/wwitssltest/wp-content/themes/twentyseventeen/vendor/automattic/woocommerce/src/WooCommerce/HttpClient/HttpClient.php:346
Stack trace: #0 /nas/content/live/wwitssltest/wp-content/themes/twentyseventeen/vendor/automattic/woocommerce/src/WooCommerce/HttpClient/HttpClient.php(385): Automattic\WooCommerce\HttpClient\HttpClient->processResponse()
#1 /nas/content/live/wwitssltest/wp-content/themes/twentyseventeen/vendor/automattic/woocommerce/src/WooCommerce/Client.php(82): Automattic\WooCommerce\HttpClient\HttpClient->request('', 'GET', Array, Array)
#2 /nas/content/live/wwitssltest/wp-content/themes/twentyseventeen/ticket-data-test-template.php(15): Automattic\WooCommerce\Client->get('')
#3 /nas/content/live/wwitssltest/wp-includes/template-loader.php(74): include('/nas/content/li...')
#4 /nas/content/live/wwitssltest/wp-blog-header.php(19): require_once('/nas/content/li...')
#5 /nas/content in /nas/content/live/wwitssltest/wp-content/themes/twentyseventeen/vendor/automattic/woocommerce/src/WooCommerce/HttpClient/HttpClient.php on line 346
When I look in the files, specifically HttpClient.php the most specific information I can come across is that the JSON might not be formatted properly?
The documentation seems pretty cut and dry but nothing I do makes any difference.
With the exception of <php print_r( $woocommerce ); ?> which returns the Client object.
You're missing a comma after query_string_auth' => true.
It should be: query_string_auth' => true.
Cheers!
I have to write a log class that logs the data coming from an exception. To make things simpler, I wanted to know if it is okay to retrieve data inside the 'trace' array and extend this by adding 'message' and 'code' properties of the exception instance in PHP. But I am not sure if all thrown exception in PHP are guaranteed to have at least these three properties ('trace', 'message' and 'code'). Also suggest if there is a better way to log things.
Firstly it's better to use code already proven to be good. PHP community widely use Monolog library check it. If you still want to write your own logger you should stick to PSR-3 rules
Answering your question. PHP object thrown must be an instance of the Exception class or a subclass of Exception.
But you if you look at constructor of an Exception
public function __construct($message = null, $code = 0, Exception $previous = null);
You can throw empty exception.But there will be always code, file, trace and code. Code and Message can be empty.
You can read more about it on http://php.net/manual/en/language.exceptions.php
I am new to yii2 and php web app development. I am using dep-drop for selecting input
depending on parent selection. Now I get an error message in yii2 debugger,
exception 'yii\base\ErrorException' with message 'Class 'app\controllers\Response' not found' in G:\xampp\htdocs\project\controllers\SalesOrderController.php:129 Stack trace: #0 [internal function]: yii\base\ErrorHandler->handleFatalError() #1 {main}
Just adding an update because at least in one hand the answer from above can be misleading.
I think that beside issue you mentioned above you had additional issue which is resolved by adding "use yii\helpers\Json;" or something similar because Response is not within namespace you mentioned.
If you want to have a Json Output from your controller and if you want to use Response class for managing action output you will need to use
use yii\web\Response;
Additionally, example of usage:
public function actionApi() {
Yii::$app->response->format = Response::FORMAT_JSON;
return array(
"test" => "test",
"test2" => "test2"
);
}
Sorry It was my mistake.
I just included this :
use yii\helpers\Json;
Problem Solved. And dep dropdown is now working as expected
please use this code.
use yii\web\Response;
I get this error when enabling sessions, using the filesystem, in Laravel PHP.
Only variables should be passed by reference in SYS_PATH/session.php on line 230.
Stack Trace:
0 /Applications/XAMPP/xamppfiles/htdocs/laravel/system/session.php(230): System\{closure}(2048, 'Only variables ...', '/Applications/X...', 230, Array)
1 /Applications/XAMPP/xamppfiles/htdocs/laravel/system/session.php(190): System\Session::write_cookie()
2 /Applications/XAMPP/xamppfiles/htdocs/laravel/system/laravel.php(187): System\Session::close()
3 /Applications/XAMPP/xamppfiles/htdocs/laravel/public/index.php(44): require('/Applications/X...')
4 {main}
Snapshot:
225: */
226: private static function write_cookie()
227: {
228: if ( ! headers_sent())
229: {
230: extract(Config::get('session'));
231:
232: $minutes = ($expire_on_close) ? 0 : $lifetime;
233:
234: Cookie::put('laravel_session', static::$session['id'], $minutes, $path, $domain, $https, $http_only);
235: }
Has anybody run into this issue? Even if you don't use Laravel, any insight?
That you're seeing the error message is a flaw in the Laravel PHP framework.
The session class makes use of the extract function that is expecting a variable, but it get's a function return.
I have reported this behavior to the project and you can discuss the options with them as well. You find a suggestion how to fix it attached, let me know if that works for you.
What if instead of extracting the config to your local scope you just assign it to an array, and use those values? The error seems to be there, not with sessions.
By default error page in ZF looks like this:
Fatal error: Uncaught exception 'Zend_Form_Exception' with message 'Method setDeault does not exist' in /usr/home/.../library/Zend/Form.php:2838
Stack trace:
#0 [internal function]: Zend_Form->__call('setDeault', Array)
#1 /usr/home/.../application/admin/controllers/TagsController.php(40): Web_Form_Admintags->setDeault('tagstext', '....')
#2 /usr/home/.../library/Zend/
Is there any ways to change this messy error description to more clearly form (something like Django error page)?
This is not an error page but a standard PHP output for uncaught exceptions. As far as I know there is no default error page in Zend Framework -- you will have to create your own by creating an appropriate view for the ErrorController.
AFAICR this is using the Error controller right? That message is basically the dump of the exception object. You can split this up by calling different parts individually rather than dumping the whole lot as a string. See the manual page I linked to.
Make sure you're only providing exception information when the APPLICATION_ENVIRONMENT is set to development as well.
The exception is formatted using new lines, which are however not transformed to in html.
You have several options...
Wrap the error with <pre></pre> tags
Create custom exception handler
function exception_handler($exception) {
echo "Uncaught exception: " , $exception->getMessage(), "\n";
}
set_exception_handler('exception_handler');