0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

【EC-CUBE】XMLを出力したい【4系】

Posted at

経緯

EC-CUBE4系の案件にてXMLを出力する仕様だったので対応。

#手順
Controllerは普通に書きますが、アノテーションに「defaults={"_format"="xml"}」を定義します。
XMLフォーマットは画面同様twigにて記載します。
試しに指定したidのCustomerデータを表示するようにしてみます

app/Customize/XmlController.php

class XmlController extends AbstractController {

    /**
     * @Route("/xml/customer/{id}", name="xml_customer", defaults={"_format"="xml"}, requirements={"id" = "\d+"})
     * @Template("Xml/customer.twig")
     */
     public function customer(Request $request, Customer $Customer) {
         return ['Customer' => $Customer];
     }

}
app/template/default/Xml/customer.twig
<customer>
  <id>{{ Customer.id }}</id>
  <name>{{ Customer.name01 }} {{ Customer.name02 }}</name>
  <kana>{{ Customer.kana01 }} {{ Customer.kana02 }}</kana>
</customer>

#補足
標準出力はUTF-8です。
昨今は問題ないと思いますが、やっぱり環境的にShift-JIS(Win Server向け)で出力したいよねって言う場合もあると思いますが、その場合はControllerでmb_convert_encodingしてしまいます。

app/Customize/XmlController.php

class XmlController extends AbstractController {

    /**
     * @Route("/xml/customer/{id}", name="xml_customer", defaults={"_format"="xml"}, requirements={"id" = "\d+"})
     * @Template("Xml/customer.twig")
     */
     public function customer(Request $request, Customer $Customer) {
         $res = $this->render('Xml/customer.twig', ['Customer' => $Customer]);
         $content = mb_convert_encoding($res->getContent(), 'SJIS-Win', mb_internal_encoding());
         $response->setContent($content);
         return $response;
     }

}
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?