douye2111 2012-01-24 23:23
浏览 53
已采纳

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

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:

use SilexExtension\CompanyPostbackServiceProvider;

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

in 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:

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.

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

Thanks!

  • 写回答

1条回答 默认 最新

  • dongxi1680 2012-01-26 10:09
    关注

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

    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.

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

    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?

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 对于这个问题的代码运行
  • ¥50 三种调度算法报错 有实例
  • ¥15 关于#python#的问题,请各位专家解答!
  • ¥200 询问:python实现大地主题正反算的程序设计,有偿
  • ¥15 smptlib使用465端口发送邮件失败
  • ¥200 总是报错,能帮助用python实现程序实现高斯正反算吗?有偿
  • ¥15 对于squad数据集的基于bert模型的微调
  • ¥15 为什么我运行这个网络会出现以下报错?CRNN神经网络
  • ¥20 steam下载游戏占用内存
  • ¥15 CST保存项目时失败