【发布时间】:2018-12-14 11:18:17
【问题描述】:
我经常在包含 oder 猜测正确的界面时遇到困难 包括一项服务。例如,这是获取路由器的推荐方式。
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
class SomeService
{
private $router;
public function __construct(UrlGeneratorInterface $router)
{
$this->router = $router;
}
}
我想,我在这里得到的是“vendor\symfony\routing\Router.php”。 但是Router类实现了RouterInterface、RequestMatcherInterface。 不是 UrlGeneratorInterface!
我不明白这背后的逻辑。 感谢您的帮助。
【问题讨论】:
-
这可能是卷起袖子深入研究源代码的最佳选择。生成器接口提供了一个 generate 方法,它是最常用的。您的 $router 变量确实应该称为 $generator。问题是,RouterInterface 扩展了 UrlGeneratorInterface 和 UrlMatcherInterface。所以它既可以生成又可以匹配。我倾向于使用 RouterInterface 只是因为拥有一个路由器对象似乎更容易理解。尽管我很少需要匹配功能。
标签: php symfony namespaces