「Php/Symfony/Symfony2/サービス作成」の版間の差分

提供: 初心者エンジニアの簡易メモ
ナビゲーションに移動 検索に移動
編集の要約なし
編集の要約なし
 
(同じ利用者による、間の2版が非表示)
5行目: 5行目:
  class SampleexampleService
  class SampleexampleService
  {
  {
     public function __construct(Container $container) {
     public function __construct(Container $container)
    {
         $this->container = $container;
         $this->container = $container;
         // $request = $this->container->get('request');
         // $request = $this->container->get('request');
         // echo $request->query->get('param1');
         // echo $request->query->get('param1');
        // $this->em                      = $container->get('doctrine.orm.entity_manager');
        // $this->translator              = $container->get('translator');
        // $this->validator              = $container->get('validator');
        // $this->logger                  = $container->get('logger');
     }
     }
     public function exec() {
     public function exec()
    {
         echo "exec!!";
         echo "exec!!";
     }
     }
43行目: 49行目:
-src/Acme/HelloBundle/Resources/views/Serviceexample/index.html.twig
-src/Acme/HelloBundle/Resources/views/Serviceexample/index.html.twig
  serviceexample !
  serviceexample !
==参考==
http://qiita.com/lethe2211/items/1288e78db7a7dd261f81
http://docs.symfony.gr.jp/symfony2/book/service_container.html

2017年1月16日 (月) 02:52時点における最新版

サンプルサービス作成

-src/Acme/HelloBundle/Services/SampleexampleService.php

namespace Acme\HelloBundle\Services;
use Symfony\Component\DependencyInjection\ContainerInterface as Container;
class SampleexampleService
{
   public function __construct(Container $container)
   {
       $this->container = $container;
       // $request = $this->container->get('request');
       // echo $request->query->get('param1');
       // $this->em                      = $container->get('doctrine.orm.entity_manager');
       // $this->translator              = $container->get('translator');
       // $this->validator               = $container->get('validator');
       // $this->logger                  = $container->get('logger');
   }
   public function exec()
   {
       echo "exec!!";
   }
}

サービスyml登録

-src/Acme/HelloBundle/Resources/config/services.yml

services:
   acme_hello.sampleexample:
       class:        Acme\HelloBundle\Services\SampleexampleService
       arguments:    [@service_container]

-app/config/config.yml

imports:
   - { resource: "@AcmeHelloBundle/Resources/config/services.yml" }

コントローラから呼び出し

-src/Acme/HelloBundle/Controller/ServiceexampleController.php

namespace Acme\HelloBundle\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
class ServiceexampleController extends Controller
{
   public function indexAction()
   {
       $service = $this->container->get('acme_hello.sampleexample');
       $service->exec();
       return $this->render('AcmeHelloBundle:Serviceexample:index.html.twig');
   }
}

twigを一応記述

-src/Acme/HelloBundle/Resources/views/Serviceexample/index.html.twig

serviceexample !

参考

http://qiita.com/lethe2211/items/1288e78db7a7dd261f81

http://docs.symfony.gr.jp/symfony2/book/service_container.html