Question

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!

Was it helpful?

Solution

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?

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top