slimphp / Slim

Slim is a PHP micro framework that helps you quickly write simple yet powerful web applications and APIs.
http://slimframework.com
MIT License
11.98k stars 1.95k forks source link

RFC: On Slim3, change the Pimple callable separator from ':' to '.' #1181

Closed mariano closed 9 years ago

mariano commented 9 years ago

Currently Slim 3.0 allows specifying a Pimple key when defining route callbacks, in the form: key.callback, as per this line, which I'm including below:

if (is_string($callable) && preg_match('!^([^\:]+)\:([a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*)$!', $callable, $matches)) {

Now this is dangerously close to a regular class method callback, specified via Class::method. A typo is just a keystroke away.

I would suggest changing the Pimple based callback to include . as separator, instead of :. Furthermore I would change the above call for a small (yet relevant) performance optimization (below I'm still using : as separator, but this would change to . if the proposal is accepted):

if (is_string($callable) && strpos($callable, ':') !== false && preg_match('!^([^\:]+)\:([a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*)$!', $callable, $matches)) {

Thoughts @akrabat ?

mariano commented 9 years ago

@akrabat I forgot to mention, that if this proposal is accepted I'll get the PR ready, with updated tests

JoeBengalen commented 9 years ago

I think I do prefer the : separator. I've seen the dot being used as some sort of namespacing/nested array notation. I would like to be able to define the controller as controller.Foo:home, where controller is used to group all controllers together.

Aside from that, like you said the : looks like the default ::, which make sense to me as it used for the same purpose: defining a callable.

mnapoli commented 9 years ago

I concur that the . is sometimes used for "namespaces" in container entry keys, e.g. module.user.manage_controller.

I think in Laravel or Lumen they use the @ symbol.

mariano commented 9 years ago

I'd be +1 for using @ On Apr 21, 2015 6:02 PM, "Matthieu Napoli" notifications@github.com wrote:

I concur that the . is sometimes used for "namespaces" in container entry keys, e.g. module.user.manage_controller.

I think in Laravel or Lumen they use the @ symbol.

— Reply to this email directly or view it on GitHub https://github.com/slimphp/Slim/issues/1181#issuecomment-94942817.

mnapoli commented 9 years ago

By the way why is it necessary to use a different separator? Here is the solution I've ended up using in Silly:

Please note the difference between:

- `Foo::staticMethod` (`is_callable() === true`) the container is not involved, it's a static method
- `Foo::method` (`is_callable() === false`) will use the container

The advantage with that is that it's very similar to actual PHP callables, and it supports arbritrary container entry ID (e.g. module.user.controller) as well as class names, which is awesome when using a container that supports autowiring (no need to configure anything).

geggleto commented 9 years ago

Is it possible to surface this as a configurable option? Then everyone can use whatever they want. I see a lot of back-and-forth on this issue.

silentworks commented 9 years ago

I don't think this should not be configurable. I think this was decided a year and a half ago as to the :, you can read more about it here #652

I think if we were to change this now, we would be inclined to the @ symbol but not the :: as it would cause confusion as mentioned in the previous discussion.

On that note I am going to close this one.