The Slim Framework support forum has moved to This Tender forum is no longer maintained or monitored.

Class Registry

raphael's Avatar


19 Apr, 2012 10:18 AM

Hey Josh & Slimmer's ;)

I'm having kind of a riddle here …
I am using several vendor API's like TextMagic, Amazon S3 etc. pp. inside my application. Also, there's a lot of current and future classes from PHP and my of my own I'm using and going to use …

Right now I'm going with the simple solution of accessing those classes:
I'm storing instances I only need once application wide, inside the app's config via $app->concfig("blah.blah",new $class);. This isn't the best solution and a singleton pattern supporting class registry stored inside the $app's config would be one step forward, but I now stuck with the easy solution to keep on coding.

I'm only askin if I already miss something like a clever use of $_ENV,
or maybe Slim functionality I have overread … ?

I'd like to manage all my classes and make them available via $app on every occasion I need to access them. The config looked like the easy way for now, but I know I can't stick with that.

Maybe somebody has an idea on that?
I already thought about extending slim, but that would mean there's more hassle when I'm going to update it. I'm looking for some kind of interface like for middleware and such … maybe it's possible and I'm just not seeing it?

Kind regards btw :)

  1. Support Staff 1 Posted by Andrew Smith on 19 Apr, 2012 11:46 AM

    Andrew Smith's Avatar

    Why not just pass them in as a object with their own namespace into $app? since $app is available project wide, for managing external api's I would check if they are available on Packagist and use Composer, Slim is Composer ready, I just need to do a writeup on how to use it, which I will post later on today.

  2. 2 Posted by raphael on 19 Apr, 2012 12:03 PM

    raphael's Avatar

    Well if you mean something other than passing it into $app->config, then please explain. I'm not sure I get it.

    Regarding Packagist and Composer:
    I'm not familiar with it. Just read about it and I'm not sure that's what I want to use. My project uses a simple "copy to host and setup" process and I'm trying to get around installing apps and packages over console …
    Maybe it's a stupid way of doing things, but I want to try to make it as simple as possible to install a new instance of the project …

  3. 3 Posted by avalak on 24 Apr, 2012 02:40 PM

    avalak's Avatar

    Pimple is a simple Dependency Injection Container for PHP 5.3

    composer.json "pimple/pimple": "dev-master",

  4. 4 Posted by raphael on 24 Apr, 2012 06:48 PM

    raphael's Avatar

    Pimple sounds great !
    I still need a reference to it, so passing it in to Slim is still a requirement …

    And regarding use of composer … I don't know if I'm going to use it, but it sounds like it could help. Pimple though I really like, definitively something I include!
    I'm not usually asking the stupid questions anymore, but could somebody explain me some kind of best practise on how to pass Slim an object? I'd still use $app->config("pimple",new Pimple());, but is that really the only and best way? I think I either gave you guys the wrong question, or I don't get the answer ;)

  5. Support Staff 5 Posted by Josh Lockhart on 24 Apr, 2012 09:13 PM

    Josh Lockhart's Avatar

    Dependency injection with Slim is a work in progress. Currently, you can assign an object into the app's configuration and retrieve it later with $app->config('foo'). You can also pass objects into Route callback anonymous functions with the use keyword.

    I've toyed with the idea of letting the Slim class implement ArrayAccess and have that interface act as sugar on top of the existing config() method. So instead of:

    $app->config('foo', 'bar');

    You could do:

    $app['foo'] = 'bar';

    It's just an idea at this point. Any other suggestions are certainly welcome.

  6. 6 Posted by raphael on 24 Apr, 2012 09:49 PM

    raphael's Avatar

    Well … the ArrayAccess interface would certainly spare some typing time on accessing data, but I like $app->config('foo') because of it's self-descriptiveness.

    Your thorough use of fluent interfaces is great and I wouldn't change a bit on that ! In my opinion, making it readable and clear what you're accessing sometimes surpasses saved typing time on shortcuts …

    Also I would even go as far suggesting a second method for maintaining global access on user classes and vendor APIs. I'm thinking $app->registry('foo'); or $app->pool('foo'); here.
    The Pimple suggestion above was really great. They have done a neat job on offering a container for classes and global user preferences.
    I think merging Pimple into Slim would be a great improvement. Or maybe including an alternative method and functionality like Pimple does …

    Letting users save their own namespaced preferences in config like $app->config('user.preference','foo'); is great, but decoupling user configurations and external classes from Slim / Slim-Config (or at least having the possibility) would be better … or at least forcing custom namespaces … I could imagine being able to loop over $app->registry(''); and return only my preferences and classes so I could serialize it or do whatever else I want. This is just random thinking and maybe I'm going into the wrong direction here …
    Also, I don't know what you where planning for your plugin interface, so maybe you've got already something similar going.

    I just wish there was something else then $app->config where I have to rely on namespaced scopes while also sharing the config's benefits of global access with other slim settings I may not want access to (or I might not even change at runtime).

    However, keeping Slim simple is more important. My suggestions and ideas may even belong to those 20% out of the 80% more important ;)

  7. Andrew Smith closed this discussion on 04 Oct, 2012 04:20 PM.

Comments are currently closed for this discussion. You can start a new one.

Keyboard shortcuts


? Show this help
ESC Blurs the current field

Comment Form

r Focus the comment reply box
^ + ↩ Submit the comment

You can use Command ⌘ instead of Control ^ on Mac