简体   繁体   English

在Silex中创建新的服务提供商

[英]Creating new service providers in Silex

I've been using Silex for a while now without any issues. 我使用Silex已有一段时间了,没有任何问题。 I've been able to add new services, create basic sites, etc. Unfortunately I'm stuck now on creating a new service from scratch... I'm not too sure what I'm doing wrong and I figured a nudge in the right direction would be useful right about now. 我已经能够添加新服务,创建基本站点等。不幸的是,我现在一直坚持从头开始创建新服务...我不太确定自己做错了什么,因此想出了一点办法正确的方向现在会很有用。

I have a basic structure like this: 我有这样的基本结构:

cache
resources
src
  -app.php
  -autoload.php
  -config.php
  -controllers.php
  -etc
vendor
  -assetic
  -Company
    -src
      -Postback.php <-- The classes I need to load
  -silex
  -etc
views
web

So, in app.php : 因此,在app.php

use SilexExtension\CompanyPostbackServiceProvider;

$app->register(new CompanyPostbackServiceProvider(), array(
    'company.class_path' => __DIR__ . '/../vendor/Company/src'
));

in src/autoload.php : src/autoload.php

$loader->registerNamespaces(array(
    'Symfony'           => array(__DIR__.'/../vendor/silex/vendor', __DIR__.'/../vendor'),
    'Silex'             => __DIR__.'/../vendor/silex/src',
    'SilexExtension'    => __DIR__.'/../vendor/Silex-extentions/src',
    'Assetic'           => __DIR__.'/../vendor/assetic/src',
    'Company'           => __DIR__.'/../vendor/Company/src'
));

in silex/vendor/Silex-extensions/src/SilexExtension/CompanyPostbackServiceProvider.php : silex/vendor/Silex-extensions/src/SilexExtension/CompanyPostbackServiceProvider.php

namespace SilexExtension;

use Silex\Application;
use Silex\ServiceProviderInterface;

class CompanyPostbackServiceProvider implements ServiceProviderInterface
{
    public function register(Application $app)
    {
        if ( isset( $app['company.class_path'] ) )
        {
            $app['autoloader']->registerNamespace(
                'Company', $app['company.class_path']
            );
        }
    }
}

I've tried several variations of this, but calling Postback from controller.php results in no classes being found, and declaring the class methods of CompanyPostbackServiceProvider results in just the register function which belongs to Silex\\Application. 我已经尝试了几种方法,但是从controller.php调用Postback导致没有找到任何类,而声明CompanyPostbackServiceProvider的类方法仅导致了属于Silex \\ Application的register函数。

Any ideas? 有任何想法吗? I know I'm doing something stupid, but for some reason it just isn't clicking. 我知道我在做一些愚蠢的事情,但由于某种原因,它只是没有点击。

Thanks! 谢谢!

First, you double-register the Company namespace, that may cause errors, may not - it's better to remove the redundancy anyway. 首先,您双重注册Company名称空间,这可能会导致错误,但可能不会—最好还是删除冗余。

Second, it is not a good practice to edit anything under vendors, like you added a new class under Silex-Extensions. 其次,在供应商下编辑任何内容不是一个好习惯,就像您在Silex-Extensions下添加了一个新类一样。 I usually put my app-related stuff in /app (bootstrap.php, config.php, appname.php) and classes, providers in /src. 我通常将与应用程序相关的内容放在/ app(bootstrap.php,config.php,appname.php)中,将类和提供程序放在/ src中。 In this case you provider goes in /src/Company/Provider/FooProvider.php . 在这种情况下,您的提供商位于/src/Company/Provider/FooProvider.php

Third, all your provider does is register an autoload - you can do it in your bootstrap just fine, no reason to create a provider. 第三,您的提供程序所做的只是注册一个自动加载-您可以在引导程序中进行自动加载,没有理由创建提供程序。 It is needed if you create a service - meaning you go through the process of instantiating a class and assigning it to an index in $app (see pretty much any provider that comes with silex). 如果您创建服务,则需要它-意味着您将实例化一个类并将其分配给$app的索引(请参见几乎所有silex附带的提供程序)。

And last, you question mentions you try to use Postback in controllers.php, but that's not enough information. 最后,您提到的问题是您尝试在controllers.php中使用Postback,但这还不够。 Did you add a use statement for it? 您是否为此添加了use声明?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM