初學者在symfony2開發中使用service

例子裏用一個很是簡單的寫日誌的服務來演示php

第一步 建立服務 LogServicehtml

在當前bundle目錄下建立Service目錄 而後建立LogService.php文件 我這裏是src/Milk/CoffeeBundle/Service/LogService.php
數組

<?php
/**
 * Created by PhpStorm.
 * User: mot
 * Date: 14-2-6
 * Time: 上午6:50
 */

namespace Milk\CoffeeBundle\Service;


class LogService {

    protected $log;
    protected $config;
    protected $file;
    protected $line;

    public function __construct( $config)
    {
        $this->config = $config;
    }

    public function exception_path($file, $line)
    {
        $this->file = $file;
        $this->line = $line;
        return $this;
    }

    public function writeLog( $log)
    {
        if( FALSE == file_exists( $this->config['log_path']))
        {
          file_put_contents( $this->config['log_path'] , '');
        }

        $this->log = "[".date('Y-m-d h:i:s')."] - ".$this->file.":line ".$this->line." Exception : " .$log. "\r\n";
        return $this;
    }

    public function flush()
    {
        file_put_contents( $this->config['log_path'] , $this->log  , FILE_APPEND );
        return $this;
    }

    public function readLog()
    {
        $log = file_get_contents( $this->config['log_path']);

        if( ! $log)
            return FALSE;

        $log = explode("\r\n" , $log);
        $log = implode('<br/>' , $log);

        return $log;
    }
}

這是我定義的一個小log類  功能比較簡單 exception_path記錄日誌發生位置  writeLog修改日誌內容 flush把日誌寫入文件 readLog讀取日誌文件this

第二步 添加定義跟參數 在當前bundle下面的Resources/config/services.yml
spa

個人services.yml位置是 src/Milk/CoffeeBundle/Resources/config/service.yml日誌

parameters:
    milk_coffee.log.class: Milk\CoffeeBundle\Service\LogService
    milk_coffee.log.config:
        log_path: c:/file_system.log
services:

    milk_coffee.log:
        class: %milk_coffee.log.class%
        arguments: [%milk_coffee.log.config%]

這裏解釋一下 code

parameters:
    milk_coffee.log.class: Milk\CoffeeBundle\Service\LogService
    milk_coffee.log.config:
        log_path: c:/file_system.log

這是咱們要用的參數 例如咱們要寫log 須要提供log文件的位置 這裏提供了文件的位置orm

c:/file_system.log

在yml解析以後
xml

    milk_coffee.log.config:
        log_path: c:/file_system.log

這個格式就是一個php數組 array( 'log_path' => 'c:/file_system.log' );htm


這裏是咱們定義服務class的位置

services:

    milk_coffee.log:
        class: %milk_coffee.log.class%
        arguments: [%milk_coffee.log.config%]

"%"中的變量是咱們上面定義的參數%

%milk_coffee.log.class% 即對應 "Milk\CoffeeBundle\Service\LogService"

%milk_coffee.log.config% 即對應 "log_path: c:/file_system.log"

這樣服務就定義好了


第三步 在咱們的邏輯代碼中使用服務

<?php

namespace Milk\CoffeeBundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Response;


class DefaultController extends Controller
{
    public function indexAction($name)
    {
       // if( FALSE === $this->get('milk_coffee.log')->readLog())

            $this->get('milk_coffee.log')
                 ->exception_path(__FILE__,__LINE__)
                 ->writeLog('First log')
                 ->flush();

        return new Response(  $this->get('milk_coffee.log')->readLog() , 200);
        //return $this->render('MilkCoffeeBundle:Default:index.html.twig', array('name' => $name));
    }
}

這裏用 $this->get('milk_coffee.log') 就得到了咱們LogService的實例 省去了 new LogService( array() ) 這種過程 並且在當前bundle的邏輯代碼中隨處可用

milk_coffee.log是咱們定義在Resources/config/service.yml中的服務名

訪問這個控制器的路由地址 就能夠看到效果了

相關文章
相關標籤/搜索