laravel how to debug a RESTful controller - php

I have this controller
class APIV1CustomerController extends BaseController{
public function getIndex(){
return "super new look";
}
public function postRegister(){
return "you have entered "+Input::get('fullName')+" as fullName parameter and "+Input::get('mobileNumber')+" as mobileNumber parameter";
}
}
I am trying to call the register function from IOS, I have some problems with the returing data. Thus, I need to debug that function.
when I return just simple test like return "test", it works well from IOS. but when I return the code I showed to you, some problem accrues, I can solve the problem myself from the IOS side but I need to debug the postRegister to check some issues.
when I was using J2EE web service, I can simple print some data to the console in my eClipse that is why I am asking if there is any way to do that in laravel.
Thanks
I am using php storm IDE

This might be useful: https://github.com/mlanin/laravel-api-debugger
It's a laravel package specifically built for assisting with APIs.

this might be irrelevant, but i learned a lot from this article when debugging api and jquery back then. http://www.openlogic.com/wazi/bid/188084/jQuery-and-Ajax-Error-Detection-and-Handling.
the first step is to see if there's any functions in your IOS which will notify you when you have an error after the http call, so that you know you actually are getting an error, ex. 404, 500 etc. Because if you are getting 200, most likely you don't need more advanced debug tool other than print.
server log is another friend which is very helpful in these white screen case, it'll tell you the exact error when 500 error happens. Ex. last error I got on my server was telling me a class name is not found, although it runs fine in my local.

Related

how to test my login page using phpunit in codeigniter?

I am new in phpUnit,my project is in codeigniter and i am using netbeans IDE 8.2 ,my os is windows10,i am trying to test my web site ,Now i have problem that how to test my login page?
i have tried this below code:
class Welcome_test extends TestCase{
public function addDataProvider(){
return array(array('email'=>'abc#gmail.com','pass'=>'cdef'));
}
/*** #dataProvider addDataProvider*/
public function test_login(){
$output = $this->post('Login/submitlogin');
$this->assertContains('<span class="title">Product List</span>',$output);
}
}
i was expecting result should pass but unfortunately i am getting this error
Welcome_test::test_login()
PHP Fatal error occured.
PHP Fatal error(s) occured, test results can be incomplete!
Review Output window for full output.'
Login page or any page so far can be tested using functional or acceptance testing using codeception.
unit testing helps us know if we are getting right assertions to our databases providing right data but they do not help us properly test the nature and functionality of page.
so I suggest you to use acceptance tests for this purpose.

How to use firefox developer tools to log server side messages from PHP

I am trying to switch firefox developer tools for server side debugging because firebug is no longer working with firePHP.
Checked the documentation I found this information:
Firebug extensions like FirePHP allow to log server-side messages to
the Firebug console. This functionality is already integrated into the
DevTools using the ChromeLogger protocol and doesn't require any
extensions to be installed.
I integrated chrome logger to my PHP script tested with Chrome and made sure it is working. But on Firefox Dev Tools nothing appears on the console. I checked the headers for X-ChromeLogger-Data. Encoded data is passed successfully.
Any one have an idea for solution?
For reference developer.mozilla.org/en-US/docs/Tools/Web_Console/Console_messages#Server
Tested with Firefox Developer Edition 56.0b3 and ChromePhp 4.1.0 (Chrome logger php script)
EDIT: There is something strange. There 2 different Developer Tools, One opens with F12 and there are no server tab, and the other opens via Tools>Web Developer menu
Server Tab displays nothing about chrome logger
Screen Shots are here:
As of 2017, firebug and hence firephp has been disabled.
I wrote some little modifications to the chromephp tool to allow seamless migration from firephp to chromephp for debuging via the console.
This article explains in clear easy steps
https://medium.com/#kudehinbuoluwaponle/migrate-from-firephp-to-chromephp-in-5-minutes-without-breaking-existing-code-e4afd1b28c5c
As an addition to the details in the article, you don't need to switch to chrome browser, You can also view the logs via the server tab of the firefox web console
With Firefox 57, a.k.a. Firefox Quantum, it looks like server logs from ChromePhp no longer work.
QuantumPHP is an alternative. With this tool, the server log and JavaScript log both appear under "console".
https://github.com/frankforte/quantumphp
Further to Kudehinbu's work, and my own work, i.e refactoring the QuantumPHP class provided https://github.com/frankforte/quantumphp, to give developers a more seamless approach and migration process from FirePHP, I may also add that unlike FirePHP, the client-side rendering will not go over a laconic [object Object] when an object is part of the arguments to the info(), warn() or error() method.
To develop an object exhaustively, like FirePHP did, you may want to transform $args using print_r() or var_export(), either before the call to an output method of the QuantumPHP class, or better, as a private/protected transformer within the class itself.
protected function resolveObjectArgs(array &$args)
{
array_walk($args, function(&$value, $key) {
if (is_array($value)) {
$value = print_r($value, true);
}
else if(is_object($value)) {
$value = var_export($value, true);
}
else return;
});
}
Thus calling this transformer within an output method:
public function info()
{
$args = func_get_args();
$this->resolveObjectArgs($args); // <== this is the line to add to the existing code
return $this->_log(self::INFO, $args);
}
Note that following my refactoring, info() is now public and no more public static, since I decided to use the object context.
Finally, taking advantage of the public context, you may want to add a destructor:
public function __destruct()
{
$this->send();
}
thus saving from explicitly calling the send method systematically after your PHP script's last call to a QuantumPHP method.
Example of client-side use:
$QPHP = QuantumPHP::getInstance();
$Obj = new MyOwnObject();
$QPHP->info($Obj); // will eventually output a detailed structure of your object
// send() gets called magically at the end of the page!

Capture user info in Sentry

I have just recently started using Sentry on my Laravel 5.1 application.
I see in there docs, and getting started stuff, a reference to capturing user information.
The example they give of passing this in, looks like this:
Raven.setUserContext({
email: 'foo#example.com'
});
Having followed their setup instructions for Laravel, I see no reference for where this could go or any reference in the documentation for how to set this up in a config file or anything for Laravel.
Any ideas on how I could set this up to send user information? Users will always be logged in when using my application.
I realise this question is three months old, so apologies if you’ve already found the answer.
I had the same requirements (log user as part of exceptions in Sentry) so did a little digging myself.
Raven.setUserContext() seems to be a function specific to the JavaScript SDK. The PHP SDK has a set_user_data($id, $email, $data) method on the Raven client that you can use somewhere in your application (most likely in your exception handler before you actually send the exception to Sentry).
Something like this should work:
public function report(Exception $e)
{
// Will only enter if statement if request has a user
if ($user = request()->user()) {
app('sentry')->set_user_data($user->getAuthIdentifier(), $user->email);
}
app('sentry')->captureException($e);
return parent::report($e);
}

Can't extend php class

I'm trying to move a php/mysql web application to a new server. The application runs fine on multiple other servers, just not the one the client wants it on. Problem 1 is I can't get errors to display on screen or in an error log. (I've posted a separate question about this). I'm hoping if I can get error to display I'll have more to go on, but what I know so far is it fails when trying to extend a module. I stripped down the class to just this:
class Module_Organization extends LmsModule {
function Module_Organization($module_name = '') {
die('Made it into the function');
}
}
Nothing happens. But if I change it to:
class Module_Organization {
function Module_Organization($module_name = '') {
die('Made it into the function');
}
}
then it does execute the die statement. So it seems that the extend portion is tripping it up. But I don't think it's the code because I know this exact code works fine on other 5 other servers. So I'm wondering if there is any server configuration that could prevent php from extending a class.
Thank you in advance for your help.
I compared the php.ini file to a server where it is working to make them match and it is now working. I'm not sure which changed fixed it though. Thank you.

How to design error reporting in PHP

How should I write error reporting modules in PHP?
Say, I want to write a function in PHP: 'bool isDuplicateEmail($email)'.
In that function, I want to check if the $email is already present in the database.
It will return 'true', if exists. Else 'false'.
Now, the query execution can also fail, In that time I want to report 'Internal Error' to the user.
The function should not die with typical mysql error: die(mysql_error(). My web app has two interfaces: browser and email(You can perform certain actions by sending an email).
In both cases it should report error in good aesthetic.
Do I really have to use exception handling for this?
Can anyone point me to some good PHP project where I can learn how to design robust PHP web-app?
In my PHP projects, I have tried several different tacts. I've come to the following solution which seems to work well for me:
First, any major PHP application I write has some sort of central singleton that manages application-level data and behaviors. The "Application" object. I mention that here because I use this object to collect generated feedback from every other module. The rendering module can query the application object for the feedback it deems should be displayed to the user.
On a lower-level, every class is derived from some base class that contains error management methods. For example an "AddError(code,string,global)" and "GetErrors()" and "ClearErrors". The "AddError" method does two things: stores a local copy of that error in an instance-specific array for that object and (optionally) notifies the application object of this error ("global" is a boolean) which then stores that error for future use in rendering.
So now here's how it works in practice:
Note that 'Object' defines the following methods: AddError ClearErrors GetErrorCodes GetErrorsAsStrings GetErrorCount and maybe HasError for convenience
// $GLOBALS['app'] = new Application();
class MyObject extends Object
{
/**
* #return bool Returns false if failed
*/
public function DoThing()
{
$this->ClearErrors();
if ([something succeeded])
{
return true;
}
else
{
$this->AddError(ERR_OP_FAILED,"Thing could not be done");
return false;
}
}
}
$ob = new MyObject();
if ($ob->DoThing())
{
echo 'Success.';
}
else
{
// Right now, i may not really care *why* it didn't work (the user
// may want to know about the problem, though (see below).
$ob->TrySomethingElse();
}
// ...LATER ON IN THE RENDERING MODULE
echo implode('<br/>',$GLOBALS['app']->GetErrorsAsStrings());
The reason I like this is because:
I hate exceptions because I personally believe they make code more convoluted that it needs to be
Sometimes you just need to know that a function succeeded or failed and not exactly what went wrong
A lot of times you don't need a specific error code but you need a specific error string and you don't want to create an error code for every single possible error condition. Sometimes you really just want to use an "opfailed" code but go into some detail for the user's sake in the string itself. This allows for that flexibility
Having two error collection locations (the local level for use by the calling algorithm and global level for use by rendering modules for telling the user about them) has really worked for me to give each functional area exactly what it needs to get things done.
Using MVC, i always use some sort of default error/exception handler, where actions with exceptions (and no own error-/exceptionhandling) will be caught.
There you could decide to answer via email or browser-response, and it will always have the same look :)
I'd use a framework like Zend Framework that has a thorough exception handling mechanism built all through it.
Look into exception handling and error handling in the php manual. Also read the comments at the bottom, very useful.
There's aslo a method explained in those page how to convert PHP errors into exceptions, so you only deal with exceptions (for the most part).

Categories