我对 Laravel Service provider 不太熟悉,我有一个问题。
示例:我有三个实现 ProfilerInterface 的类 SystemProfiler、SurveyProfiler 和 OfferProfiler。而且我还有 ProfilerService 类,它在构造函数中注入(inject) ProfilerInterface。我需要通过注入(inject)每个分析器来创建不同的 ProfilerService 服务。
分析器服务:
class ProfilerService {
$this->profiler;
function __construct(ProfilerInterface $profiler) {
$this->profiler = profiler;
}
}
我知道如何在 symfony2 框架中做到这一点:
system_profiler:
class: App\MyBundle\Profiles\SystemProfiler
survey_profiler:
class: App\MyBundle\Profiles\SurveyProfiler
offer_profiler:
class: App\MyBundle\Profiles\OfferProfiler
system_profile_service:
class: App\MyBundle\Services\ProfilerService
arguments:
- system_profiler
survey_profile_service:
class: App\MyBundle\Services\ProfilerService
arguments:
- survey_profiler
offer_profile_service:
class: App\MyBundle\Services\ProfilerService
arguments:
- offer_profiler
然后用 ProfilerService 实现的别名调用 $this->container->get()
但 Laravel 文档说“如果类不依赖于任何接口(interface),则无需将它们绑定(bind)到容器中。”。并且 ProfilerService 不依赖于接口(interface)。所以我可以像这样将每个分析器绑定(bind)到接口(interface):
$this->app->bind('App\MyBundle\Contracts\ProfilerInterface','App\MyBundle\Profiles\SystemProfiler');
或
$this->app->bind('App\MyBundle\Contracts\ProfilerInterface','App\MyBundle\Profiles\SurveyProfiler');
或
$this->app->bind('App\MyBundle\Contracts\ProfilerInterface','App\MyBundle\Profiles\OfferProfiler');
但是我应该如何绑定(bind)应该将哪些 Profiler 注入(inject) ProfilerService 以及何时???
如果有任何帮助和解释,我将不胜感激
最佳答案
这里是(read the docs):
// ServiceProvider
public function register()
{
// Simple binding
$this->app->bind('some_service.one', \App\ImplOne::class);
$this->app->bind('some_service.two', \App\ImplTwo::class);
// Aliasing interface - container will inject some_service.one
// whenever interface is required...
$this->app->alias('some_service.one', \App\SomeInterface::class);
// ...except for the Contextual Binding:
$this->app->when(\App\DependantTwo::class)
->needs(\App\SomeInterface::class)
->give('some_service.two');
}
$ php artisan tinker
// Aliases
>>> app('some_service.one')
=> App\ImplOne {#669}
>>> app('some_service.two')
=> App\ImplTwo {#671}
// Aliased interface
>>> app('App\SomeInterface')
=> App\ImplOne {#677}
>>> app('App\DependantOne')->dependency
=> App\ImplOne {#677}
// Contextual
>>> app('App\DependantTwo')->dependency
=> App\ImplOne {#676}
鉴于此设置:
namespace App;
class ImplOne implements SomeInterface {}
class ImplTwo implements SomeInterface {}
class DependantOne
{
public function __construct(SomeInterface $dependency)
{
$this->dependency = $dependency;
}
}
class DependantTwo
{
public function __construct(SomeInterface $dependency)
{
$this->dependency = $dependency;
}
}
关于php - Laravel 服务提供者说明,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38999550/
我正在尝试使用ruby和Savon来使用网络服务。测试服务为http://www.webservicex.net/WS/WSDetails.aspx?WSID=9&CATID=2require'rubygems'require'savon'client=Savon::Client.new"http://www.webservicex.net/stockquote.asmx?WSDL"client.get_quotedo|soap|soap.body={:symbol=>"AAPL"}end返回SOAP异常。检查soap信封,在我看来soap请求没有正确的命名空间。任何人都可以建议我
我想安装一个带有一些身份验证的私有(private)Rubygem服务器。我希望能够使用公共(public)Ubuntu服务器托管内部gem。我读到了http://docs.rubygems.org/read/chapter/18.但是那个没有身份验证-如我所见。然后我读到了https://github.com/cwninja/geminabox.但是当我使用基本身份验证(他们在他们的Wiki中有)时,它会提示从我的服务器获取源。所以。如何制作带有身份验证的私有(private)Rubygem服务器?这是不可能的吗?谢谢。编辑:Geminabox问题。我尝试“捆绑”以安装新的gem..
最近,当我启动我的Rails服务器时,我收到了一长串警告。虽然它不影响我的应用程序,但我想知道如何解决这些警告。我的估计是imagemagick以某种方式被调用了两次?当我在警告前后检查我的git日志时。我想知道如何解决这个问题。-bcrypt-ruby(3.1.2)-better_errors(1.0.1)+bcrypt(3.1.7)+bcrypt-ruby(3.1.5)-bcrypt(>=3.1.3)+better_errors(1.1.0)bcrypt和imagemagick有关系吗?/Users/rbchris/.rbenv/versions/2.0.0-p247/lib/ru
在Rails4.0.2中,我使用s3_direct_upload和aws-sdkgems直接为s3存储桶上传文件。在开发环境中它工作正常,但在生产环境中它会抛出如下错误,ActionView::Template::Error(noimplicitconversionofnilintoString)在View中,create_cv_url,:id=>"s3_uploader",:key=>"cv_uploads/{unique_id}/${filename}",:key_starts_with=>"cv_uploads/",:callback_param=>"cv[direct_uplo
我想在Ruby中创建一个用于开发目的的极其简单的Web服务器(不,不想使用现成的解决方案)。代码如下:#!/usr/bin/rubyrequire'socket'server=TCPServer.new('127.0.0.1',8080)whileconnection=server.acceptheaders=[]length=0whileline=connection.getsheaders想法是从命令行运行这个脚本,提供另一个脚本,它将在其标准输入上获取请求,并在其标准输出上返回完整的响应。到目前为止一切顺利,但事实证明这真的很脆弱,因为它在第二个请求上中断并出现错误:/usr/b
您如何在Rails中的实时服务器上进行有效调试,无论是在测试版/生产服务器上?我试过直接在服务器上修改文件,然后重启应用,但是修改好像没有生效,或者需要很长时间(缓存?)我也试过在本地做“脚本/服务器生产”,但是那很慢另一种选择是编码和部署,但效率很低。有人对他们如何有效地做到这一点有任何见解吗? 最佳答案 我会回答你的问题,即使我不同意这种热修补服务器代码的方式:)首先,你真的确定你已经重启了服务器吗?您可以通过跟踪日志文件来检查它。您更改的代码显示的View可能会被缓存。缓存页面位于tmp/cache文件夹下。您可以尝试手动删除
转自:spring.profiles.active和spring.profiles.include的使用及区别说明下文笔者讲述spring.profiles.active和spring.profiles.include的区别简介说明,如下所示我们都知道,在日常开发中,开发|测试|生产环境都拥有不同的配置信息如:jdbc地址、ip、端口等此时为了避免每次都修改全部信息,我们则可以采用以上的属性处理此类异常spring.profiles.active属性例:配置文件,可使用以下方式定义application-${profile}.properties开发环境配置文件:application-dev
require"socket"server="irc.rizon.net"port="6667"nick="RubyIRCBot"channel="#0x40"s=TCPSocket.open(server,port)s.print("USERTesting",0)s.print("NICK#{nick}",0)s.print("JOIN#{channel}",0)这个IRC机器人没有连接到IRC服务器,我做错了什么? 最佳答案 失败并显示此消息::irc.shakeababy.net461*USER:Notenoughparame
我有一个使用PDFKit呈现网页的pdf版本的Rails应用程序。我使用Thin作为开发服务器。问题是当我处于开发模式时。当我使用“bundleexecrailss”启动我的服务器并尝试呈现任何PDF时,整个过程会陷入僵局,因为当您呈现PDF时,会向服务器请求一些额外的资源,如图像和css,看起来只有一个线程.如何配置Rails开发服务器以运行多个工作线程?非常感谢。 最佳答案 我找到的最简单的解决方案是unicorn.geminstallunicorn创建一个unicorn.conf:worker_processes3然后使用它:
关于如何使用git设置类似Dropbox的服务,您有什么建议吗?您认为git是解决此问题的合适工具吗?我在考虑使用git+rush解决方案,你觉得怎么样? 最佳答案 检查这个开源项目:https://github.com/hbons/SparkleShare来自项目的自述文件:Howdoesitwork?SparkleSharecreatesaspecialfolderonyourcomputer.Youcanaddremotelyhostedfolders(or"projects")tothisfolder.Theseprojec