回应您对@Crisp 的回答的评论,以及对于未来的谷歌员工,我将解释如何为路由做类似的事情。
通常您会希望创建一个自定义路由器,该路由器可以将 URL 与数据库中的页面相匹配,类似于标准的 Segment 路由器。为此,您必须实现Zend\Mvc\Router\RouteInterface 接口。例如:
namespace Application\Router;
use Zend\Mvc\Router\RouteInterface;
use Application\Model\CMSTable;
class CmsRoute implements RouteInterface, ServiceLocatorAwareInterface
{
protected $table;
// Service locator injection code
public function getCmsTable()
{
// Retrieve the table from the service manager
}
public function match(Request $request)
{
// Match the request on some route field, etc.
}
public function assemble(array $params = array(), array $options = array())
{
// Assemble a URL based on the given parameters (e.g. page ID).
}
public static function factory($options = array())
{
// Construct a new route, based on the options.
}
}
然后您可以将此路由注册为模块配置中RoutePluginManager 的可调用对象:
'route_manager' => array(
'invokables' => array(
'Cms' => 'Application\Router\CmsRoute'
),
),
然后,您可以创建一个类型为Cms 的新路线(就像创建任何其他路线一样)。路由插件管理器会创建你的路由实例,并且由于CmsRoute 实现了ServiceLocatorAwareInterface,插件管理器会将自己注入到路由中。反过来,插件管理器设置了主服务管理器,这样你就可以从那里获取数据库表!
当然,您可以在页面 ID 上进行匹配,但如果您有分层结构,最好在您的 URL 中反映这一点。因此,我建议将 route 字段添加到数据库架构并匹配它,从树根开始并向下处理。