How can I modify the response formatting itself? - yii2

In an action I need to responde with some XML. I use Response::FORMAT_XML for that, which works fine.
// In a controller:
public static function actionFetchData() {
Yii::$app->response->format = Response::FORMAT_XML;
return [
'a' => 'b',
['c', 'd'],
'e' => ['f', 'g']
];
}
Result in Browser:
<?xml version="1.0" encoding="UTF-8"?>
<response>
<a>b</a>
<item>
<item>c</item>
<item>d</item>
</item>
<e>
<item>f</item>
<item>g</item>
</e>
</response>
However, I'd like to change the root tags name from response to data. This should be possible since XmlResponseFormatter, which gets used to render the XML, has the property rootTag. How can I do that?
Or in general: How can I change the settings of a formatter (also JSON or whatever)?

If you want to change format specific to particular action then use :.
Yii::$app->response->format = Response::FORMAT_XML;
Yii::$app->response->formatters = [
'xml' => [
'class' => 'yii\web\XmlResponseFormatter',
'rootTag' => 'data',
],
];
return [
'a' => 'b',
['c', 'd'],
'e' => ['f', 'g']
];

One way is to create an own formatter object for XML. Reason: In Yii::$app->response the formmatter is not present within the action - it will be created later, when the response gets rendered, what it too late to modify it. But we can create a new formatter and set it as the formatter for XML. This is a valid option.
public static function actionMetaInfo($docId) {
$formatter = new XmlResponseFormatter([
'rootTag' => 'data',
'itemTag' => 'unnamed',
]);
Yii::$app->response->formatters[Response::FORMAT_XML] = $formatter;
Yii::$app->response->format = Response::FORMAT_XML;
return [
'a' => 'b',
['c', 'd'],
'e' => ['f', 'g']
];
}
Output now:
<?xml version="1.0" encoding="UTF-8"?>
<data>
<a>b</a>
<unnamed>
<unnamed>c</unnamed>
<unnamed>d</unnamed>
</unnamed>
<e>
<unnamed>f</unnamed>
<unnamed>g</unnamed>
</e>
</data>
Here I changed the itemTag as well. This way, we can also modify other Formatter's properties (e.g. also in JsonResponseFormatter).

If you want to modify XML response formatter for all your application, you should simply add this to your configuration file :
'components' => [
'response' => [
'formatters' => [
'xml' => [
'class' => 'yii\web\XmlResponseFormatter',
'rootTag' => 'data',
],
],
],
],

Related

How to use faker in yii2 in advance template

I am generating random data for project and for unit testing. So i want to use faker. But unable to how to use faker in yii2?
set this in your main config:
return [
'controllerMap' => [
'fixture' => [
'class' => 'yii\faker\FixtureController',
'templatePath' => '#common/tests/templates/fixtures',
'fixtureDataPath' => '#common/tests/fixtures/data',
],
// ...
],
// ...
];
use it like this:
$faker = Faker\Factory::create();
// generate data by accessing properties
echo $faker->name;
// 'Lucy Cechtelar';
echo $faker->address;
// "426 Jordy Lodge
// Cartwrightshire, SC 88120-6700"
echo $faker->text;
for more info check faker basic usage
and yii2 guide

zend expressive custom configuration key

How to properly create a custom configuration key in zend expressive
I tried to create a file custom-config.php in the config/autoload directory but the key is not read by the container
my custom-config.php looks like this
<?php
[
'customkey' => [
'value1' => '1',
'value2' => '2',
],
];
I think you a missing a return statement.
Try with
<?php
return [
'customkey' => [
'value1' => '1',
'value2' => '2',
],
];
Besides missing return statement, as marcosh pointed out, I think additional problem is the filename itself.
It should be something like custom-config.local.php or custom-config.global.php.
Configuration files are loaded in a specific order. First global.php, then *.global.php, local.php and finally *.local.php. This way local settings overwrite global settings.
Settings shared between servers go into *.global.php, sensitive data and local settings in *.local.php. Local config files are ignored by git.
The default loading behavior is set in config/config.php if you want to change this.
Your custom config could look like this:
<?php // config/autoload/custom-config.global.php
return [
'dependencies' => [
'invokables' => [
// ...
],
'factories' => [
// ...
],
],
// Prefered format
'vendor' => [
'package' => [
'key' => 'value',
]
],
// Custom package
'custom_package' => [
'value1' => '1',
'value2' => '2',
],
];

How to parse this JSON object/string?

I am trying to parse the JSON written # http://a0.awsstatic.com/pricing/1/ec2/sles-od.min.js
Here is a quick snippet from above link:
{vers:0.01,config:{rate:"perhr",valueColumns:["vCPU","ECU","memoryGiB","storageGB","sles"],currencies:["USD"],regions:[{region:"us-east",instanceTypes:[{type:"generalCurrentGen",sizes:[{size:"t2.micro",vCPU:"1",ECU:"variable",
...
...
...
...
Please visit the aforementioned link to see the complete JSON.
As seen above, none of the keys of above JSON have Double Quotes around them.
This leads to malformed JSON string and my JSON parser is failing at it. I also tried putting this JSON in http://www.jsoneditoronline.org/ and it fails as well.
Now, this is the same link which is used by Amazon to display various prices of their EC2 instance. So I think I am missing something here. My Googling led me to believe that above thing is not JSON and is instead JSONP.. I don't understand what is that.
Could you help me understand how to parse this JSON. BTW, I am doing this work in perl using JSON Module.
Some background:
Amazon Web Services does not have an API to get Pricing info programmatically. Hence I am parsing these links which is what amazon is doing while displaying pricing information here. Besides, I am not from programming space and perl is all I know.
Like you said JSONP or "JSON with padding" can't be parsed by json parser because it is not json (it is a different format). But it is actually a json with the prefix (padding)
The padding is typically the name of a callback function that wraps json.
In this case, its default callback names 'callback' and we can do a bit hackiest way by using Regular Expression to capture json that is wrapped by 'callback()' like this
s/callback\((.*)\);$/$1/s;
Also, if you would like to use JSON library, you can enable allow_barekey which means you don't need those quotes around those keys.
Below is my working code. I use LWP::Simple to get the content for the given and Data::Dump to print the isolated data structure.
use strict;
use warnings;
use LWP::Simple;
use JSON;
my $jsonp = get("http://a0.awsstatic.com/pricing/1/ec2/sles-od.min.js")
or die "Couldn't get url";
( my $json = $jsonp ) =~ s/callback\((.*)\);$/$1/s; #grap the json from $jsonp and store in $json variable
my $hash = JSON->new->allow_barekey->decode ( $json );
use Data::Dump;
dd $hash;
Outputs:
{
config => {
currencies => ["USD"],
rate => "perhr",
regions => [
{
instanceTypes => [
{
sizes => [
{
ECU => "variable",
memoryGiB => 1,
size => "t2.micro",
storageGB => "ebsonly",
valueColumns => [{ name => "os", prices => { USD => 0.023 } }],
vCPU => 1,
},
{
ECU => "variable",
memoryGiB => 2,
size => "t2.small",
storageGB => "ebsonly",
valueColumns => [{ name => "os", prices => { USD => 0.056 } }],
vCPU => 1,
},
{
ECU => "variable",
memoryGiB => 4,
size => "t2.medium",
storageGB => "ebsonly",
valueColumns => [{ name => "os", prices => { USD => 0.152 } }],
vCPU => 2,
},
{
ECU => 3,
memoryGiB => 3.75,
size => "m3.medium",
storageGB => "1 x 4 SSD",
valueColumns => [{ name => "os", prices => { USD => "0.170" } }],
vCPU => 1,
},
....
As said in comments above, it is not JSON so it can't be parsed by JSON parser... But for an quick & (very)dirty work, you can try the JSON::DWIW module.
The next code:
use 5.014;
use warnings;
use WWW::Mechanize;
use Data::Dump;
use JSON::DWIW;
my $mech = WWW::Mechanize->new();
my $jsonstr = $mech->get('http://a0.awsstatic.com/pricing/1/ec2/sles-od.min.js')->content;
($jsonstr) = $jsonstr =~ /callback\((.*)\)/s;
my $json_obj = JSON::DWIW->new;
my $data = $json_obj->from_json( $jsonstr );
dd $data;
prints a structure what maybe is what you want, e.g.:
{
config => {
currencies => ["USD"],
rate => "perhr",
regions => [
{
instanceTypes => [
{
sizes => [
{
ECU => "variable",
memoryGiB => 1,
size => "t2.micro",
storageGB => "ebsonly",
valueColumns => [{ name => "os", prices => { USD => 0.023 } }],
vCPU => 1,
},
{

Perl Catalyst - Couldn't render template..........not found

The error I am getting in the development server:
[info] *** Request 2 (0.000/s) [681] [Thu Dec 12 21:05:39 2013] ***
[debug] Path is "homescreen"
[debug] "GET" request for "homescreen" from "192.168.1.100"
[debug] Rendering template "homescreen/homescreen.tt2"
[error] Couldn't render template "homescreen/homescreen.tt2: file error - homescreen/homescreen.tt2: not found"
[error] Couldn't render template "homescreen/homescreen.tt2: file error - homescreen/homescreen.tt2: not found"
[debug] Response Code: 500; Content-Type: text/html; charset=utf-8; Content-Length: 14312
[info] Request took 0.033915s (29.485/s)
.------------------------------------------------------------+-----------.
| Action | Time |
+------------------------------------------------------------+-----------+
| /homescreen | 0.000341s |
| /end | 0.014055s |
| -> Myproject::View::HTML->process | 0.013049s |
'------------------------------------------------------------+-----------'
What I am doing:
I have the following Controller/Homescreen.pm:
package Myproject::Controller::Homescreen;
use strict;
use warnings;
use parent 'Catalyst::Controller';
use Data::Dumper;
use JSON;
__PACKAGE__->config->{namespace} = '';
sub homescreen :Path('/homescreen') :Args(0) {
my ( $self, $c ) = #_;
print STDERR "IN THE HOMESCREEN ACTION\n";
$c->stash({template => 'homescreen/homescreen.tt2',
title => 'Home Screen'
});
}
I have the following View/HTML.pm:
package Myproject::View::HTML;
use Moose;
use namespace::autoclean;
extends 'Catalyst::View::TT';
__PACKAGE__->config({
#Changed default TT extension to TT2
TEMPLATE_EXTENSION => '.tt2',
render_die => 1,
});
I have the following lib/Myproject.pm:
__PACKAGE__->config(
name => 'Myproject',
# Disable deprecated behavior needed by old applications
disable_component_resolution_regex_fallback => 1,
#enable_catalyst_header => 1, # Send X-Catalyst header
);
__PACKAGE__->config(
#Configure the view
'View::HMTL' => {
#Set the location for TT files
INCLUDE_PATH => [
__PACKAGE__->path_to( 'root', 'src' ),
],
},
);
# Start the application
__PACKAGE__->setup();
I then have a root/src/homescreen/homescreen.tt2 withing my Catalyst directory that contains all my html code (eventually it will use the template toolkit,but at the moment it is purely html and javscript code which I know is fine).
The error I get on the application page in my browser is:
Couldn't render template "homescreen/homescreen.tt2: file error - homescreen/homescreen.tt2: not found"
I have tried using DEBUG => 'undef' in my HTML.pm View to help with debugging, but I don't seem to get any extra output.
There is probably something very obvious I am overlooking but I cannot work out what it is.
Update
I have just noticed the following in the Config section of my browser debug screen:
Config
do {
my $a = {
"Action::RenderView" => {
ignore_classes => [
"DBIx::Class::ResultSource::Table",
"DBIx::Class::ResultSourceHandle",
"DateTime",
],
scrubber_func => sub { ... },
},
"disable_component_resolution_regex_fallback" => 1,
"home" => "/home/fred/Myproject",
"name" => "Myproject",
"Plugin::ConfigLoader" => {},
"Plugin::Static::Simple" => {
debug => 1,
dirs => [],
ignore_dirs => [],
ignore_extensions => ["tmpl", "tt", "tt2", "html", "xhtml"], <---- IS THIS SIGNIFICANT AT ALL?
include_path => [
bless({
dirs => ["", "home", "fred", "Myproject", "root"],
file_spec_class => undef,
volume => "",
}, "Path::Class::Dir"),
],
mime_types => {},
mime_types_obj => bless({}, "MIME::Types"),
no_logs => 1,
},
"root" => 'fix',
"stacktrace" => { context => 3, verbose => 0 },
"static" => 'fix',
"View::HMTL" => {
INCLUDE_PATH => [
bless({
dirs => ["", "home", "fred", "Myproject", "root", "src"],
file_spec_class => undef,
volume => "",
}, "Path::Class::Dir"),
],
},
};
$a->{"root"} = $a->{"Plugin::Static::Simple"}{include_path}[0];
$a->{"static"} = $a->{"Plugin::Static::Simple"};
$a;
}
I take it this means it is ignoring my template file because it has the .tt2 file extension?
However, I am not setting this ignore_extensions attribute anywhere in my Catalyst project? Is this the cause of my problem or something totally unrelated?
It looks like your configuration isn't taking effect. Try putting your template in root/homescreen/homescreen.tt2 instead of root/src/homescreen/homescreen.tt2, and Catalyst finds it.
Ahh, you have a typo in your lib/Myproject.pm:
__PACKAGE__->config(
#Configure the view
'View::HMTL' => {
Try 'View::HTML' instead (notice you have HMTL - wrong spelling).

How do I get JSON output in PERL without \n but in a readable format?

I need to get a readable JSON object from a PERL script but it's not in a readable format.
This is the code that produces the JSON.
while (my ($orderID, $possessorName, $itemDescription, $customerPickUpTime, $customerDropOffTime, $paymentAmount, $originAddress1, $originAddress2, $originNeighborhood, $originZipCode, $destinationAddress1, $destinationAddress2, $destinationNeighborhood, $destinationZipCode) = $sth->fetchrow_array)
{
%data = (orderID => $orderID, possessorName => $possessorName, itemDescription => $itemDescription, customerPickUpTime => $customerPickUpTime, customerDropOffTime => $customerDropOffTime, paymentAmount => $paymentAmount, originAddress1 => $originAddress1, originAddress2 => $originAddress2, originNeighborhood => $originNeighborhood, originZipCode => $originZipCode, destinationAddress1 => $destinationAddress1, destinationAddress2 => $destinationAddress2, destinationNeighborhood => $destinationNeighborhood, destinationZipCode => $destinationZipCode);
$json_obj = JSON->new->allow_nonref;
my $json_text = $json_obj->pretty->encode(\%data);
$query_results{"job$index"} = {"data" => $json_text};
$index++;
}
return $json_obj->pretty->encode(\%query_results, {ascii => 1, pretty => 1});
Everything works except when I look into the file (here's the printing line):
open (resultsFile, ">", "json_file.txt") || die "This doesn't work.";
print resultsFile "Results: \n\n $results";
The results are as follows:
{
"job3" : {
"data" : "{\n \"originAddress1\" : \"101 East 105th Street\",\n \"destinationZipCode\" : \"10128\",\n \"destinationNeighborhood\" : \"Upper East Side\",\n \"customerDropOffTime\" : \"2013-01-22 23:41:37\",\n \"originAddress2\" : \"\",\n \"paymentAmount\" : \"19.00\",\n \"customerPickUpTime\" : \"2013-01-22 22:56:37\",\n \"itemDescription\" : \"body\",\n \"destinationAddress1\" : \"180 East 93rd Street\",\n \"destinationAddress2\" : \"\",\n \"possessorName\" : \"Lisa Howard\",\n \"originZipCode\" : \"10029\",\n \"originNeighborhood\" : \"East Harlem\",\n \"orderID\" : \"723\"\n}\n"
},
The JSON object is formed correctly but the \n is the problem. It's not outputting with an actual newline. That's the issue.
This is because, $json_text is a string and not a hash. If you want to encode the whole thing as JSON, you must create an appropriate data structure
$query_results{"job$index"} = {"data" => \%data};
and give that as a whole to encode.