symfony3.x 注冊自定義服務
建立service類
symfony_test/src/AppBundle/Serverice/UserService.php
<?php
namespace AppBundle\Serverice;
class UserService{
public static function aaa(){
return ;
}
public function b(){
return 'b';
}
}
symfony_test 為項目名稱
編輯配置檔案
symfony_test/app/config/services.yml
# Learn more about services, parameters and containers at
# https://symfony.com/doc/current/service_container.html
parameters:
#parameter_name: value
memcached.servers:
- { host: .., port: }
services:
userservice:
class: AppBundle\Serverice\UserService
memcached:
class: Memcached
calls:
- [ addServers, [ %memcached.servers% ]]
控制器中使用
/*
* Service 使用方式一:
*/
$userService = $this->get('userservice');
$a = $userService::aaa();
$b = $userService->b();