在Silex中创建新的服务提供商 [英] Creating new service providers in Silex

查看:73
本文介绍了在Silex中创建新的服务提供商的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用Silex已有一段时间了,没有任何问题.我已经能够添加新服务,创建基本站点等.不幸的是,我现在一直坚持从头开始创建新服务...我不太确定自己在做什么错,我想出了一个办法.正确的方向现在会很有用.

I've been using Silex for a while now without any issues. 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.

我有这样的基本结构:

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

因此,在app.php中:

use SilexExtension\CompanyPostbackServiceProvider;

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

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'
));

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']
            );
        }
    }
}

我已经尝试了几种方法,但是从controller.php调用Postback导致未找到任何类,而声明CompanyPostbackServiceProvider的类方法仅导致了属于Silex \ Application的register函数.

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.

有什么想法吗?我知道我在做一些愚蠢的事情,但是由于某种原因,它只是没有点击.

Any ideas? I know I'm doing something stupid, but for some reason it just isn't clicking.

谢谢!

推荐答案

首先,您双重注册Company名称空间,这可能会导致错误,但可能不会-最好还是删除冗余.

First, you double-register the Company namespace, that may cause errors, may not - it's better to remove the redundancy anyway.

第二,在供应商下进行编辑不是一个好习惯,就像您在Silex-Extensions下添加了一个新类一样.我通常将与应用程序相关的内容放在/app(bootstrap.php,config.php,appname.php)中,并将类和提供程序放在/src中.在这种情况下,您的提供商将进入/src/Company/Provider/FooProvider.php.

Second, it is not a good practice to edit anything under vendors, like you added a new class under Silex-Extensions. I usually put my app-related stuff in /app (bootstrap.php, config.php, appname.php) and classes, providers in /src. In this case you provider goes in /src/Company/Provider/FooProvider.php.

第三,您的提供程序所做的只是注册一个自动加载-您可以在引导程序中进行自动加载,而无需创建提供程序.如果您创建服务就需要它-这意味着您要完成实例化类并将其分配给$app中的索引的过程(请参见几乎与silex一起提供的任何提供程序).

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).

最后,您提出的问题是您尝试在controllers.php中使用回发​​,但这还不够.您是否为此添加了use语句?

And last, you question mentions you try to use Postback in controllers.php, but that's not enough information. Did you add a use statement for it?

这篇关于在Silex中创建新的服务提供商的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆