how different between $_GET and $request->getParameter() - frameworks

I'm currently studing symfony framework.
and I could not found the answer of how different between $_GET and $request->getParameter().
I can understand the $request->getPrameter() can be used for,
if(isset($_GET['test'])){
$test = $_GET['test'];
}else{
$test = 'Unknown';
}
to
$request->getParameter('test','Unknown');
and anything else? I was expect it filter XSS but I think it doesn't.
For me, $_GET way is much easier, but I feel like I should use the $request->getParameter()
So, I'd like to know exactly how diffrence.
Thanks! :)

Use:
$request['parameter']
This is equivalent to $request->getParameter('parameter', null).
Note that $request->getParameter differs from $_GET in that it returns all parameter types. $request->getGetParameter is equivalent to $_GET.

If you access a request parameter like:
$request->getParameter('parameter');
it can be the value of $_GET['parameter'] or $_POST['parameter'] as well. It is useful as normally you don't care whether the value is coming through post or get method.

You should infact be using $request->getGetParameter('parameter') if you're specifically after a get parameters.
$request->getGetParameter('parameter') is the equivalent of $_REQUEST['parameter'] which may not result in the desired behaviour.
Also worth noting that the sfWebRequest object is available in your views via $sf_request i.e. $sf_request->getGetParameter('parameter')

Related

How to determine if two instances have the same type, in Dart?

Let's say I get two instances in my code and I don't know their types. How to check it?
If in Java, I can use this code:
a.getClass() == b.getClass()
But in Dart, I can't find similar methods. Although there is the dart:mirrors providing reflect(instance) function, which may let me do it, but I'm not sure if that's a correct solution since it looks complicated.
a.runtimeType == b.runtimeType
I think dart:mirrors (reflection) API helps you. Look at this page :
http://blog.dartwatch.com/2012/06/dartmirrors-reflection-api-is-on-way.html
Also you can look this question(with runtime solution)
How do I get the qualified name from a Type instance, in Dart?
if you want to compare a and b you can use
if(a.runtimeType == b.runtimeType);
but if you want to confirm that a is the type you want you need to do this
if(a.runtimeType.toString()=="DivElement");//a is a div for instance
because runtimeType's value is not a string

Logging syntax for Play Framework 2 in Scala

This is a really silly question, but how can you do convenient formatting of log strings in Play Framework 2 (and in Scala?).
I've googled but its very difficult to find an example, essentially most links are talking about configuring Logback in the first place which I've done fine.
I'm basically trying to find the best stylistic way to do something like:
if(Logger.isDebugEnabled)
Logger.debug("Modified: Id = '" + real_session_id + "', Modified = " + modified.toString)
Coming from a C# background (and log4net) I'd assume you could do something like:
if(Logger.isDebugEnabled)
Logger.debug("Modified: Id = '{0}', Modified = {1}", real_session_id, modified.toString)
But I can't see how this would work with the trait the way it is defined. I've also seen vague references to how you might be able to avoid checking Logger.isDebugEnabled by using a lazy evaluative syntax like:
Logger.debug("Modified: Id = ${real_session_id}, Modified = ${modified.toString}")
That uses Scala macros - but again, that doesn't work and I can find very little information about it.
Am I missing something really blatant here?
The framework used for logging is logback. When you type : Logger.debug, the isDebugEnabled is already implicitly checked.
For the syntax of logging, use the Scala string interpolation.
Logger.debug(s"Modified: Id = '$real_session_id', Modified = $modified.toString")
Why not just use the standard String interpolation capabilities of the language/stdlib? http://docs.scala-lang.org/overviews/core/string-interpolation.html
I apologise if I've missed something crucial about your question.
As to avoiding the if (Logger.isDebugEnabled) check, if the logging framework is not providing some sort of lazy evaluation scheme for arguments passed into it, I would just first consider defining my own wrappers:
object MyLazyLogger {
def debug(msg: => Any) =
if (Logger.isDebugEnabled) Logger.debug(msg)
}
Also, I don't think the way in which you interpolate stuff into the string has anything to do with not evaluating the arguments to debug() if logging is disabled—if debug() declares that it eager-evaluates any arguments passed into it, there's no way that I can see you can change to lazy evaluation at the call site by just using a "special form" of string interpolation. (I'd be happy if anyone proved me wrong here and taught me something new :))
Disclosure: I'm not familiar with Play (yet), so I'm just taking a shot at a general approach here.

Variable in CDATA in Scala

Is there a way to put a variable to be expanded in a cdata section in scala
val reason = <reason><![CDATA[ {failedReason} ]]></reason>
It could be even simplier:
val reason = <reason>{scala.xml.PCData(failedReason)}</reason>
I am not sure if you can get that through native XML support, but you could do something like:
scala.xml.XML.loadString("<reason><![CDATA[%s]]></reason>".format(failedReason))
You lose some of the compile-time validations that way, but it should give you am xml element with the data which you are looking for. Since it returns a scala.xml.Elem, you can also embed the result in a larger XML structure.
EDIT
After thinking about this a bit more, the following may be a beter (and less fragile) way to do this. It restricts the free-text portion to only the CDATA, minimizing the potential for unbalanced expressions.
<reason>{ scala.xml.Unparsed("<![CDATA[%s]]>".format(failedReason)) }</reason>

Simplify CoffeeScript statement

I am trying to handle a simple case where i could be getting an object, or a dictionary. So i am either going to get a object like:
obj.fields.nick
or its going to be a dictionary like
obj['nick']
I was wondering if there was a simpler way to do the following:
value = (eval("obj.fields." + field[1]) if obj?.fields ) ? eval("obj['#{field[1]}']")
I was hoping to do something like:
value = (obj?.fields?."#{field[1]}" ) ? eval("obj['#{field[1]}']")
But if that worked I wouldn't be writing this post...
I am basically looking for a way to execute a string as part of the if
value = obj.fields?[field] ? obj[field]
# or
value = (obj.fields ? obj)[field]
This is the same as
if obj.fields?
obj.fields[field]
else
obj[field]
There is absolutely no need for eval.
The string interpolation construct ("Equals four: #{2+2}") is something that is handled by the coffeescript compiler, and will therefore not work inside an eval. But assuming the naming of the stuff inside the string does not change, you could easily rewrite it, so that eval("obj['#{field[1]}']") becomes eval("obj['"+field[1]+"']"). Assuming I got your question right of course.

Getting UTF-8 Request Parameter Strings in mod_perl2

I'm using mod_perl2 for a website and use CGI::Apache2::Wrapper to get the request parameters for the page (e.g. post data). I've noticed that the string the $req->param("parameter") function returns is not UTF-8. If I use the string as-is I can end up with garbled results, so I need to decode it using Encode::decode_utf8(). Is there anyway to either get the parameters already decoded into UTF-8 strings or loop through the parameters and safely decode them?
To get the parameters already decoded, we would need to override the behaviour of the underlying class Apache2::Request from libapreq2, thus losing its XS speed advantage. But that is not even straightforward possible, as unfortunately we are sabotaged by the CGI::Apache2::Wrapper constructor:
unless (defined $r and ref($r) and ref($r) eq 'Apache2::RequestRec') {
This is wrong OO programming, it should say
… $r->isa('Apache2::RequestRec')
or perhaps forego class names altogether and just test for behaviour (… $r->can('param')).
I say, with those obstacles, it's not worth it. I recommend to keep your existing solution that decodes parameters explicitly. It's clear enough.
To loop over the request parameters, simply do not pass an argument to the param method and you get a list of the names. This is documented (1, 2), please read more carefully.