문제

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.

도움이 되었습니까?

해결책

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

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top