ssa / ssa-bundle
用于将 ssa 框架集成到 Symfony 中的 ssa-bundle
Requires
- ssa/core: 1.0.*
This package is not auto-updated.
Last update: 2022-08-01 16:47:07 UTC
README
Ssa 是一个用于在 JavaScript 代码中访问 PHP 服务的框架。本项目是在 Symfony 2 上的 ssa 集成。 Ssa 项目
安装
Ssa bundle 安装非常简单,你只需添加 composer 依赖项。
composer.json
"require": { "ssa/ssa-bundle" : "dev-master", }
如果 composer 说 "ssa/core" 未找到,请将此行添加到你的 composer 文件中: composer.json
"require": { "ssa/core" : "dev-master", }
在内核中添加捆绑包
new Ssa\SsaBundle\SsaBundle()
在你的 routing.yml 中添加 ssa 路由
routing.yml
_ssa: resource: "@SsaBundle/Resources/config/routing.yml"
之后,你可以在 config.yml 中注册你的服务
config.yml
ssa : services : # serviceName is the ssa service name # service : is the symfony serivce serviceName : {service : 'ssa.test.service'} # or you can just export any methods of this service serviceName2 : {service : 'ssa.test.service', methods : ['methodToImport']} # if your service is not a symfony service you can use the class attribute serviceName3 : {class : '\Path\To\Your\Class', methods : ['methodToImport']}
然后你可以简单地使用 assetic 导入 JavaScript 服务,如下所示
file.html.twig
{% javascripts 'ssa:serviceName' 'ssa:serviceName1' 'ssa:serviceName2' %} <script type="text/javascript" src="{{asset_url }}"></script> {% endjavascripts %}
你现在可以在你的 JavaScript 文件中简单地调用你的 PHP 服务
serviceName1 .methodToImport('param1', {objectProp : 'ObjectValue', objectProp2 : 'ObjectValue2'}) .done(function(data){ console.log(data); });
支持
Ssa 允许在 JavaScript 代码中调用 PHP 服务。在 Symfony 中,Ssa 添加了一个参数解析器。它可以将 JSON 对象转换为你的 doctrine 实体。示例:Entity
Product : - id - name - price
数据库
Id | Name | Price
1 | Foo | 10.0
ProductService.php
class ProductService { private $em; public function __construct(EntityManagerInterface $em) { $this->em = $em; } public function getProduct(Product $p) { return $p; } public function updateProduct(Product $p) { $this->em->persist($p); $this->em->flush(); return $p; } }
JavaScript 调用
productService.getProduct({id : 1}).done(function(data){ // data.id = 1 // data.name = "Foo" // data.price = 10.0 }); productService.updateProduct({name : 'Bar', price : 15}).done(function(data){ // data.id = AutoGenerated value // data.name = 'Bar' // data.price = 15 }); productService.updateProduct({id : 1, price : 11.5}).done(function(data){ // data.id = 1 // data.name = 'Foo' // data.price = 11.5 });
定制
你可以使用你自己的类或更改参数来自定义框架。
参数
你可以在 config.yml 中更改任何参数
config.yml
ssa : configuration : # the debug configuration, default is the symfony configuration debug : true | false # the cache configuration, by default there are no cache, the cachemode is not mandatory with symfony cacheMode : file | apc | memcache # the cache directory if cacheMode is file. default it's the kernel.cache_dir cacheDirectory : # the host for memcache cacheMode memcacheHost : # the port for memcache cacheMode memcachePort : # path to ssa js file, if you have your own ssa js implementation. Path begin in web directory ssaJsFile :
如果你想为你的类使用特定的解析器,你可以添加你自己的解析器: Ssa 文档
config.yml
ssa : parameterResolver : primitive : - YourFirstPrimitiveResolver - YoutSecondPrimitiveResolver object : - YourFirstObjectResolver - YourSecondObjectResolver
实现
你可以更改 ssa 实现,添加 ssa 参数解析器,更改路由生成器。
路由管理器
路由用于生成调用 PHP 服务的 URL,默认使用 ssa_run_service 路由。你有两种更改路由生成的方式。第一种是更改使用的路由名称
service.yml
parameters : ssa.runner.route : 'your_own_ssa_route_name'
或者你可以完全更改用于生成路由的类,你的类必须实现 ssa\converter\UrlFactory 接口,你的构造函数必须有两个参数 Symfony\Component\Routing\RouterInterface $router 和 $routeName。要更改使用的类,你必须更改 ssa.urlFactory.class 参数
service.yml
parameters : ssa.runner.route : Your\Own\UrlFactory
参数解析器
如果你想使用你自己的参数解析器,你可以设置使用的类。参数解析器用于将 GET 或 POST 参数转换为 PHP 对象。你的参数解析器需要扩展 ssa\runner\resolver\impl\DefaultParameterResolver 或实现 ssa\runner\resolver\ParameterResolver。你必须更改参数 ssa.parameterResolver.class 以使用你自己的 parameterResolver。
service.yml
parameters : ssa.parameterResolver.class : Your\own\resolver