初学者在symfony2开发中使用service

首先什么是service , 日常php应用中 , 例如开发一个社交网站 , 会使用DBO session 邮件功能 日志功能 oauth验证 以及第三方平台接入的api , 甚至图片添加水印剪裁等操作 凡是涉及到参数固定的一些组件( 如果有多种参数的话我们可以写多个service ) 都可以设置成服务 这样达到一次初始化 然后到处都能快速使用的目的 复杂组件的复用可以为我们的提高程序的性能 , 本文just for初学者


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

第一步 创建服务 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中的服务名

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

作者:mot原文地址:https://segmentfault.com/a/1190000000403705

%s 个评论

要回复文章请先登录注册