Frage

I created a trait and tried to use it but I face the error that the application object doesn't have the method.

Call to undefined method Silex\Application::phrase()

Trying to use

use \Language\LanguageTrait
$app->phrase() 

And this is the language trait.

namespace Language;

use Silex\Application;

trait LanguageTrait
    {
        public function phrase ($phrase, $replacements = array())
        {
            $language = $this['language'];

            return $language->phrase($phrase, $replacements);
        }
    }

I have tried with other built in traits like the URLServiceProvider trait but I get the same error so I assume I am using it wrong.

War es hilfreich?

Lösung

It sounds like you are using the default Silex\Application class. The documentation isn't clear that you have to actually create a custom application that extends the default then initialize that. For example:

class CustomApplication extends Silex\Application {
    use YourTrait;
}
$app = new CustomApplication();

Hope this helps. You can find more information on traits over at http://silex.sensiolabs.org/doc/usage.html#traits

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top