A good practice on how to debug a Symfony2 controller - php

I have an Symfony2 project and from time to time, I have PHP errors in my controller. In order to find out where is the error, I would like to debug the PHP code in the controller.
What do you think is the best practice to debug a controller in Symfony2?

The error page tells you many usefull things:
The error message - What is exactly going wrong? (and something: how can I solve this?)
The line number - On which line in the controller are things going wrong?
The exception name - Hover over the name to see the fully qualified namespace. It tells you on which component something is going wrong.
The trace - You can see a full trace path which tells you much usefull things: On which class/method is the exception thrown? Which methods are executed first?
Another good practise is to create tests for your controller, both functional and unit tests. Learn more about them in the documentation.
If you need more help, it is better to specify what error you get.

Related

Laravel: What is the best practice in testing controller

I'm using laravel 4 and trying to write a test for controller with a mock up of the business logic which pass some fake data.
Then I encounter errors in rendering view due to missing of variables.
First of all, I didn't expect that the view will be rendered, so I didn't care to pass the exact data structure that is needed.
Should I pass the correct data structure to the view or is there a way to stop view from rendering in the test? Which is the better practice and why?
Moreover, after attempting to imitate data structure passing by business logic, there is still error cause by permission of user.
The result temp file of the view has been generated by web user (www-data) and I don't have permission to modify it.
I know, I can just chmod 777, but this is clearly not a good practice, if I have to do that for every time I want to run the test.
Thanks in advance
I suppose your controllers are not messed up. Every logic should be injected through constructor or resolved from the IOC. Mock external classes and bind mocked instances on your test setUp. Facades already have support for Mockery. You can use XXX::shouldReceive().
Also, I suggest you to take a look at Illuminate\Foundation\Testing\TestCase. It has helper methods like assertViewHas, assertSessionHas, assertRedirectedTo etc. With these, testing your controller will be really easy.
After all, best thing to do for you would be reading "Laravel Testing Decoded" by Jeffrey Way. It'a short and amazing book. https://leanpub.com/laravel-testing-decoded
i had a similar problem with the view breaking up while testing. in my case it was a foreach loop, that didn't get the right structure, so i changed my mock to return an empty array, this made the test pass. so this is a possible solution for you i think, though it doesn't mock or just forget rendering the view itself.

How can I find out what controller method was called in a Laravel4 NotFoundHttpException?

I am new to Laravel4 and I find it very annoying. One of my frustrations is that when I get a NotFoundHTTPException it does not tell me which Action was trying to be called! Because of this, I don't know if I have a problem with my routes.php file, whether for form's Form::open(array('url'=>'postUpload') location is bad or anything. With my previous experience with Zend the error would tell you exactly which action was trying to be called so I could troubleshoot exactly where the problem is. I feel with Laravel, I have to make changes to so many files (routes.php, blade.php, config.php, or Controller files) just to get to the bottom of which file is the culprit.
So my question is: Is there a way I can extend, or show the action that the application attempted to call on the NotFoundHttpException screen? I'm not posting any code here because it won't help the situation. I need to be able to tell which action the application is trying to call.
Thanks!!
Yeah, this is not cool at all, I agree and also have some troubles with it, when it only shows at the log and has no other comments, no Whoops message, for example.
But... Not Found HTTP = route not found. The problem is in your routes, so take a look at the QUERY_STRING in the Whoops message, it should tell your wich one is broken.
When a controller action is not found, you'll receive a 'Controller method not found'.

Zend Framework 2

I'm a big enthusiast about new Zend Framework. I had done one project in the previous version so far and I decided to learn new one which contains a really hard parts to understand for me.
I've watched webinars and youtube videos, also I read the documentation and I'm trying to understand this framework simply by read the code but there are few things I just can't figure out.
There is a lot information about EventManager in webinars but since I'm lack at English I'm just not able to understand 100% of the speech.
I know that event manager manage events which is quite obvious but I don't really know how to use it: in one of the webinars they provide some simple examples but they don't explain where to put this code, is it module.php file? Or some other place if you can show me some practical example with explanation so i can see whats the point.
Next thing is that mysterious $e passed as an argument to most of the functions in Module.php which after is used like this: $e->getApplication or something I just wonder what this $e stands for? It is instance of what? And next thing is how it is passed "automatically" to these functions?
Another problem: in configuration files which are specified for each module called module.config.php there is a lot of things. I got the point of routes I've understood them but I can't figure out what are the invokables and factories. It is also explained in one of the webinars as follow: invokables are paths to classes, factories are callable functions or classes (cant remember). The point is OK, that make sense in some theoretical talk but please provide me some examples with in depth explanation, where those factories and invokables come from and other stuff that I need to know.
The other thing is that there is a lot of configuration possibilities in those configuration files. Where can I find some informations about what "keys"=>"values" are possible in those files?
That's all for now but there are still unanswered questions i would like to ask. If someone is able to help me, I would really appreciate to correspond via mail with this person.
Ok one problem is solved. The one about mysterious &e so if somene is intersted in here is a solution:
This $e variable instance depends on what function we use in module.php file:
case 1: init() -> &e is an instance of module manager
case 2: onBootstrap() -> &e is an instance of MvcEvent
(if there are other cases i havent found please let us know)
The thing is that those methods are called when event has been triggered so mysterious &e is passed to those functions by listenerers which are listening if these functions appears in our code (this is my simple logic of that so please dont hate me)
There are still other questions to answer.. as soon as ill figure out some senseful answer i will let you know
Within stock ZF2 code, $e is always an instance of Zend\EventManager\EventManagerInterface. Usually it is either an Zend\EventManager\Event or an Zend\Mvc\MvcEvent.
A class' EventManager triggers an event which results in listener callbacks being called. Each listener callback is passed an instance of an Event which then has some useful methods, notably getTarget() and getParams(). Other instances of EventManagerInterface usually have more specialised methods. MvcEvent in particular has methods related to the Mvc component, such as getApplication(), getRouter(), getRequest() and getResponse().
You can find more information about tghe EventManager, application configuration and MvcEvent in the "Using Zend Framework 2" book: http://leanpub.com/using-zend-framework-2

zend error handling for Web Services

I have a question regarding error handling in Zend. I am fairly new to zend frame work.
I am new on this project that i am working on and the previous developers didn't handle service errors and the application is fairly large so I am trying to figure out an easy way to handle all the errors the service returns, and even handle the errors when service fails.
so when ever there is an error we need to alert the user that something is wrong and show the error.
now since i will be getting that in Model, how do I handle this in elegant way so that there is not much rework to be done.
Can i create a common class and extend it? I also need to alert the user in case of any error.
I want a better way because I have more than 150 controller files and about more than 100 model files.
Thanks
For a ZF app that's using Zend_Application, you should ensure that your application.ini file has this in it:
resources.frontController.throwExceptions = false
Then any Exception that is thrown will be trapped and the ErrorController's errorAction() method will be called. This gives you a centralised place to handle errors.

check for error

I have my own AppCotroller and using the beforeRender method to make changes to $this->viewPath based on the desired output format.
Is there a way I can check if Cake is currently outputting an error message? If I change the viewPath and then it's displaying an error (like can't load model, etc) it will error on the error :)
By the time Cake is displaying the error it should already be too late to do something about it. Not quite sure why you'd get an error about missing models when you change the viewPath, I hope that was just an example.
You might have some luck overriding or extending the ErrorHandler to intercept errors, but I wouldn't recommend doing that. Errors don't exist to be hidden, they're there to tell you something.
Creating a custom View might be a good idea depending on what you want to do (see the MediaView as an example of an alternative view).
The best thing to do though should be to avoid triggering errors by only allowing certain, predefined views to be set or making sure that a certain view file exists before trying to invoke it.

Categories