[英]Laravel Extend Vendor Class
I tried extending an Illuminate Class Translator 我试过扩展一个Illuminate Class Translator
I created a class and extended to translator then I added this line to my RepositoryServiceProvider 我创建了一个类并扩展到了翻译器,然后我将这一行添加到了我的RepositoryServiceProvider中
$this->app->bind(\Illuminate\Translation\Translator::class, \App\Repositories\Translation\TranslationTranslator::class);
But its not working 但它不起作用
what am I doing wrong? 我究竟做错了什么?
the class as follows 课程如下
<?php
namespace App\Repositories\Translation;
use Countable;
use Illuminate\Support\Arr;
use Illuminate\Support\Str;
use Illuminate\Support\Collection;
use Illuminate\Support\Traits\Macroable;
use Illuminate\Support\NamespacedItemResolver;
use Symfony\Component\Translation\MessageSelector;
use Symfony\Component\Translation\TranslatorInterface;
class TranslationTranslator extends \Illuminate\Translation\Translator
{
/**
* Parse a key into namespace, group, and item.
*
* @param string $key
* @return array
*/
public function parseKey($key)
{
\Log::info('got in');
die;
$segments = parent::parseKey($key);
if (is_null($segments[0])) {
$segments[0] = @explode('.', $segments[2])[0];
}
if ($segments[1] == 'translatable') {
$segments[1] = @explode('.', $segments[2])[0] . '_' . @explode('.', $segments[2])[1];
}
return $segments;
}
}
UPDATE UPDATE
Apparently the Translator class has a constructor 显然,Translator类有一个构造函数
public function __construct(LoaderInterface $loader, $locale)
{
$this->loader = $loader;
$this->locale = $locale;
}
so my binding has to pass by the interface.. which cannot be instantiated 所以我的绑定必须通过接口传递..无法实例化
public function boot()
{
$app = $this->app;
$this->app->bind(\Illuminate\Translation\Translator::class, function() use ($app){
return $app->make(\App\Repositories\Translation\TranslationTranslator::class);
});
}
and getting this error 并得到这个错误
Illuminate\\Contracts\\Container\\BindingResolutionException with message 'Target [Illuminate\\Translation\\LoaderInterface] is not instantiable while building [App\\Repositories\\Translation\\TranslationTranslator].' 在构建[App \\ Repositories \\ Translation \\ TranslationTranslator]时,Illlicaate \\ Contracts \\ Container \\ BindingResolutionException消息'Target [Illuminate \\ Translation \\ LoaderInterface]不可实例化。
You can use closure to resolve the classes 您可以使用闭包来解析类
$this->app->bind(\Illuminate\Translation\Translator::class, function(){
return new \App\Repositories\Translation\TranslationTranslator;
});
Secondly translator is binded with laravel in using translator
alias. 其次,翻译器使用translator
别名与laravel绑定。
You can also override it. 你也可以覆盖它。
$this->app->bind('translator', function(){
return new \App\Repositories\Translation\TranslationTranslator;
})
This worked for me 这对我有用
$app = $this->app;
$loader = $app['translation.loader'];
$locale = $app['config']['app.locale'];
$this->app->bind('translator', function() use ($loader, $locale){
return new \App\Repositories\Translation\TranslationTranslator($loader, $locale);
});
I hope this help you 我希望这对你有帮助
Try changing it to this: 尝试将其更改为:
$this->app->instance(\Illuminate\Translation\Translator::class, \App\Repositories\Translation\TranslationTranslator::class);
That should then change the instance of the interface. 然后应该更改接口的实例。
UPDATE UPDATE
if you are just trying to add a new method, the Translator class is Macroable. 如果您只是尝试添加新方法,则Translator类是Macroable。 So you can do the following 所以你可以做到以下几点
Translator::macro('parseKey', function ($key) {
\Log::info('got in');
die;
$segments = parent::parseKey($key);
if (is_null($segments[0])) {
$segments[0] = @explode('.', $segments[2])[0];
}
if ($segments[1] == 'translatable') {
$segments[1] = @explode('.', $segments[2])[0] . '_' . @explode('.', $segments[2])[1];
}
return $segments;
});
You would then be able to call your method as you normally would. 然后,您就可以像往常一样调用方法。 For example: 例如:
app(Translator::class)->parseKey($key);
Check below example 请查看以下示例
namespace App\Repositories\Translation;
use Illuminate\Translation\Translator;
class TranslationTranslator extends Translator
{
public function get()
{
....
}
}
No need to anything. 不需要任何东西。 you only need to add new functions or override base class functions. 您只需要添加新函数或覆盖基类函数。 Then you can use this class as simple other classes. 然后,您可以将此类用作简单的其他类。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.