Debugging in CodeIgniter (and MVC) - php

Often times I feel the need to dump a variable for debugging purpose. In CodeIgniter I am struggling to do that since I cannot simply "echo" a variable without passing it to the view first, which I think is pain-staking. I have read the official documentation, though I had found a good solution with the log_message function but cannot make it work, even though I successfully made the "logs" folder writable and changed the "threshold" as advised. Any suggestions?
https://www.codeigniter.com/user_guide/general/errors.html

It's not good practice, but you can output anything to the browser at any point in execution (from core classes, libraries, models and controllers) by simply using print_r() at the point you want to output the info.
Of course, this will sometimes not display as there may be redirects/routes/etc that take the user to the next step in the controller execution, but if you want to suppress this (again, for debug purposes only), you can use:
print_r($string_or_variable_to_debug);
die();
The die() command will stop all execution and leave you with just the info you want to output.
Using log_message() is better practice, but it's difficult to understand why you're not having success there if you say you've followed the following steps:
Make sure that your logs folder is set to be writable
Set $config['log_threshold'] to 2, 3 or 4
Used the function like so: log_message('debug','Message you want to log');
If you want to use print_r() to nicely format an array or object inside the log message, then you'll need to set the second parameter of print_r() to TRUE, like so:
log_message('debug',print_r($array_or_object,TRUE));

You need to set your $config['log_threshold'] in file config.php since by default it's 0 (no logs)
0 = Disables logging, Error logging TURNED OFF
1 = Error Messages (including PHP errors)
2 = Debug Messages
3 = Informational Messages
4 = All Messages

I think the answer is you are looking for is:
Codeigniter - Developer's Debug Helper
You can add this file to application/helper folder. Then autoload vayes_helper in application/config/autoload.php for generic use. Then all you need to is writing:
vdebug($variable); // In Controllers, Models, Libraries, Helpers
<?=vdebug($variable)?> // In View files

log_message('debug',print_r($array_or_object_you_want_to_print,TRUE));
This will print your array nicely into log . Following is the output example :
[1] => Array
(
[uid] => 10049082
[phone_id] => 2
[friend_status] => 0
[friend_event_hide_status] =>
)
[2] => Array
(
[uid] => 10042768
[phone_id] => 4
[friend_status] => 3
)
[3] => Array
(
[uid] => 10078255
[phone_id] => 6
[friend_status] => 2
)
[4] => Array
(
[uid] => 10078255
[phone_id] => -1
[friend_status] => 1
[name] => Rajesh
)
[5] => Array
(
[uid] => 10078255
[phone_id] => -1
[friend_status] => 1
[name] => ritesh kumar
)
[6] => Array
(
[uid] => 10078255
[phone_id] => -1
[friend_status] => 1
[name] => Le
)

Related

How to get an object array's field?

I currently receive an array with this structure:
Array
(
[0] => Symfony\Component\HttpFoundation\File\UploadedFile Object
(
[test:Symfony\Component\HttpFoundation\File\UploadedFile:private] =>
[originalName:Symfony\Component\HttpFoundation\File\UploadedFile:private] => apple-tv-ad
.jpg
[mimeType:Symfony\Component\HttpFoundation\File\UploadedFile:private] => image/jpeg
[size:Symfony\Component\HttpFoundation\File\UploadedFile:private] => 36555
[error:Symfony\Component\HttpFoundation\File\UploadedFile:private] => 0
[pathName:SplFileInfo:private] => /private/var/tmp/phpe6INuk
[fileName:SplFileInfo:private] => phpe6INuk
)
)
When I try to print_r($file[0]['originalName']); I receive a 500 internal server error. I try to upload a file via an AJAX call. Once my mediaUpload method is called, I want to make the database entry with the appropriate values received through this array. I am just stuck at how to access it. Any help would be appreciated.
Thanks!
Use the getClientOriginalName() method. (Sorry for the short answer, but that's pretty much all there is to it.)
$name = $file[0]->getClientOriginalName();
The UploadedFile object has different accessor methods you can use to get all the different pieces of information you need. They're all there in the API docs I linked.

Yii and PHPUnit Autoloaders conflict

This question is related to an earlier question I had, but I learned a bit more now, and that question became too messy, so I start a new question here.
I am trying to use Yii and PHPUnit at the same time, and try to get the Autoloading working. I found an Autoloader here that I am using for PHPUnit. I read here that I should register the Yii autoloader last.
With the following bootstrap file:
<?php
require_once(__DIR__.'/../tests/AutoLoader.php');
Toolbox\Testing\AutoLoader::registerDirectory(__DIR__.'/../protected');
require_once(__DIR__.'/../yii-1.1.14.f0fee9/yii.php');
print_r(spl_autoload_functions());
?>
I see that the autoloaders are in place in the right order:
[1] => Array
(
[0] => Toolbox\Testing\AutoLoader
[1] => loadClass
)
[2] => Array
(
[0] => YiiBase
[1] => autoload
)
However, I still can't import the class I need. It goes wrong in YiiBase in the function autoload in the line include($className.'.php');, which tries to include a class name without a path attached to it.
Why does Yii try to load a class without a full path to the file needed?
How can I make this combination to work?

Zend Rest Server is not reading protected object variables

I am new to zend framework and I am working on REST web service API. While my model output is protected object variables Zend_Rest_Server(). It is unable to handle that and throw an error.
Array
(
[0] => Webservices_Models_Token Object
(
[_id:protected] => 1
[_token_key:protected] => 123
[_mapper:protected] =>
)
)
While why model is returning above result, I am getting error. I got this result from zend model mapper.
Oh Dear!
You seem to have arrived here by error, or the page you are seeking
has been removed and can no longer be displayed.
Perhaps you are looking for a page that has been moved during the
recent improvements to our website. An error occurred ** This needs to
be altered before go live Exception information:
Message: Invalid Character Error
Bellow output works fine and generating xml from $this->_server = new Zend_Rest_Server()
Array
(
[0] => stdClass Object
(
[id] => 1
[token_key] => 123
)
)
Can anyone tell me whats the problem?
How can I solve it?
This is did the trick for me. toArray()
$select = $this->getDbTable()->select()->where($where);
return $this->getDbTable()->fetchAll($select)->toArray();
Thanks.

CakePHP On Last step of ACL setup

I know there are several threads asking similar questions - but I havent found any real answers to solve this specific syntax error, I have followed the tutorial over and over again and still end up with these error - I know its looking for a missing node in relationship between the ACL and ACO - ugh, please help
Warning (512): DbAcl::check() - Failed ARO/ACO node lookup in permissions check. Node references:
Aro: Array
(
[User] => Array
(
[id] => 1
[username] => admin
[group_id] => 1
[created] => 2012-06-15 14:20:44
[modified] => 2012-06-15 14:20:44
)
)
Aco: controllers/Pages/display [CORE/Cake/Controller/Component/Acl/DbAcl.php, line 79]
It looks like your app can't find the aco record for the display action of the Pages controller. The quickest way to ensure you have all your aco's loaded into your db is use the AclExtras Plugin, this will scan through your controller files and add all actions to your aco table.

Can print_r $_SESSION object, but cannot access it?

First, a bit of background info. I'm working on a multi-site project, 3 cities each with their own installation of a custom CMS type thing. The client doesn't want to pay out for more than one SSL certificate, and despite my suggestion won't even look into a wildcard SSL. My job is to create a unified payment gateway that sits on domain.com/paypment.php, the sites themselves sit on subdomains e.g cityx.domain.com.
Now, on the original payment page the code accesses an object created by the CMS called $_SESSION['member_obj'], and at some point in the code detects the user's account level using:
if ($_SESSION['member_obj']->data['account_level']==1)
So, this works all fine and dandy without any problems, but as soon as I use the same object on my script it breaks. I would like to point out that my code is sitting on the root domain, and the working code on a sub - I have made the $_SESSION variables available to all subdomains using:
ini_set('session.cookie_domain', '.domain.com');
Now, the odd part, the object exists but when I try to use the ->data['account_level'] method, it dies on me. I know the object exists becasue I can print_r the object and I get the following:
__PHP_Incomplete_Class Object
(
[__PHP_Incomplete_Class_Name] => Member
[table] => members
[data] => Array
(
[id] => 1689
[title] => mr
[firstname] => Testy
[surname] => McTesters
[age_range] => 18-24
[gender] => male
[email] => xxxxxx
[username] => xxxxxx
[password] => xxxxxx
[account_level] => 1
[joined] => 1326290317
[password_reset] =>
[password_reset_date] => 01/01/1970
[last_active] => 1326881531
[status] => active
[contact] => 0
[third_party] => 0
[notifications] => 0
[terms] => 1
[dummy] => 0
[override_account] => 0
[premium_request] => 1
[password_reset_date_original] => 1970-01-01
)
followed by a load of other stuff.
So, could anyone be kind enough to explain to me how an object can exist, but not be accessible?
Could it - __PHP_Incomplete_Class - be because you forgot to include the class definition for the class that encapsulates this data?
You will have to load the class Member prior to starting the session.
Check so your session.auto_start is set to off in your php.ini.

Categories