初学者在symfony2开发中使用service

例子里用一个非常简单的写日志的服务来演示

第一步 创建服务 LogService

在当前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读取日志文件

第二步 添加定义跟参数 在当前bundle下面的Resources/config/services.yml

我的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%]

这里解释一下 

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

这是我们要用的参数 例如我们要写log 需要提供log文件的位置 这里提供了文件的位置

c:/file_system.log

在yml解析之后

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

这个格式就是一个php数组 array( 'log_path' => 'c:/file_system.log' );


这里是我们定义服务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中的服务名


访问这个控制器的路由地址 就可以看到效果了

转载于:https://my.oschina.net/imot/blog/197365

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值