positibe / cmf-routing-extra-bundle
Symfony Positibe CmfRoutingExtra Bundle
Requires
- doctrine/orm: ~2.5
- gedmo/doctrine-extensions: ~2.4
- symfony-cmf/routing-auto-bundle: ~2.0
- symfony-cmf/routing-bundle: ~2.0
- symfony/framework-bundle: ~2.8|~3.0
This package is not auto-updated.
Last update: 2024-09-23 08:20:20 UTC
README
PositibeCmfRoutingExtraBundle 为 Symfony CmfRoutingBundle 添加了 Doctrine ORM 支持,以便在 ORM 数据库上存储路由
安装
要安装此捆绑包,只需添加依赖项捆绑包
php composer.phar require positibe/cmf-routing-extra-bundle
接下来,确保在您的应用程序内核中启用捆绑包
<?php
// app/AppKernel.php
public function registerBundles()
{
return array(
// ...
new Symfony\Cmf\Bundle\RoutingBundle\CmfRoutingBundle(),
new Symfony\Cmf\Bundle\RoutingAutoBundle\CmfRoutingAutoBundle(),
new Positibe\Bundle\CmfRoutingExtraBundle\PositibeCmfRoutingExtraBundle(),
// ...
);
}
配置
将配置复制到您的配置包中
# config/packages/positibe_routing
parameters:
# locales: [es, en, fr] # Maybe you already have it configured
cmf_routing:
chain:
routers_by_id:
cmf_routing.dynamic_router: 200
router.default: 100
dynamic:
enabled: true
uri_filter_regexp: "#^(?!(/css/|/js/|/admin/|/security/|/frontend/|/backend/|/public/)).*$#"
persistence:
orm:
route_class: 'Positibe\Bundle\CmfRoutingExtraBundle\Entity\AutoRoute'
# route_provider_service_id: positibe_routing.route.provider
generic_controller: PositibeCmfRoutingExtraBundle:GenericContent:index
cmf_routing_auto:
adapter: positibe_doctrine_orm
注意:此捆绑包使用 GedmoDoctrineExtension 的 timestampable、sluggable、translatable 和 sortable 扩展。请确保您已启用此扩展的监听器。您还可以使用 StofDoctrineExtensionBundle。
请记住更新模式
php app/console doctrine:schema:update --force
使用
具有路由的实体必须实现 Symfony\Cmf\Component\Routing\RouteReferrersInterface
。
将 Positibe\Bundle\CmfRoutingExtraBundle\Entity\AutoRoute
和所需方法添加到任何您想要关联关系的实体中
<?php
// src/AppBundle/Entity/Post.php
namespace AppBundle\Entity;
use Doctrine\Common\Collections\ArrayCollection;
use Symfony\Cmf\Component\Routing\RouteObjectInterface;
use Symfony\Cmf\Component\Routing\RouteReferrersInterface;
use Doctrine\ORM\Mapping as ORM;
/**
*
* @ORM\Table(name="app_post")
* @ORM\Entity(repositoryClass="AppBundle\Entity\PostRepository")
*/
class Post implements RouteReferrersInterface {
/**
* @var ArrayCollection|RouteObjectInterface[]
*
* @ORM\ManyToMany(targetEntity="Positibe\Bundle\CmfRoutingExtraBundle\Entity\AutoRoute", orphanRemoval=TRUE, cascade="all")
* @ORM\JoinTable(name="app_post_routes")
*/
protected $routes;
public function __construct()
{
$this->routes = new ArrayCollection();
}
/**
* @return ArrayCollection|\Symfony\Cmf\Component\Routing\RouteObjectInterface[]
*/
public function getRoutes()
{
return $this->routes;
}
/**
* @param ArrayCollection|\Symfony\Cmf\Component\Routing\RouteObjectInterface[] $routes
*/
public function setRoutes($routes)
{
$this->routes = $routes;
}
/**
* Add a route to the collection.
*
* @param \Symfony\Component\Routing\Route $route
* @return $this
*/
public function addRoute($route)
{
$this->routes[] = $route;
return $this;
}
/**
* Remove a route from the collection.
*
* @param \Symfony\Component\Routing\Route $route
*/
public function removeRoute($route)
{
$this->routes->removeElement($route);
}
}
提示:您可以使用 Positibe\Bundle\CmfRoutingExtraBundle\Entity\HasRoutesTrait
来简化 RouteReferrerInterface 方法和映射的实现。这将创建一个多对多关系,而无需做任何事情。
<?php
// src/AppBundle/Entity/Post.php
namespace AppBundle\Entity;
use Doctrine\Common\Collections\ArrayCollection;
use Positibe\Bundle\CmfRoutingExtraBundle\Entity\HasRoutesTrait;
use Doctrine\ORM\Mapping as ORM;
/**
*
* @ORM\Table(name="app_post")
* @ORM\Entity(repositoryClass="AppBundle\Entity\PostRepository")
*/
class Post implements RouteReferrersInterface {
use HasRoutesTrait;
public function __construct()
{
$this->routes = new ArrayCollection();
}
}
创建路由
$post = new Post(); //Class that implement `Symfony\Cmf\Component\Routing\RouteReferrersInterface`
$post->setTitle('You're awesome'); //Fill datas
$manager->persist($post);
$manager->flush(); //Flush to be able to take the id of the `$post`
$contentRepository = $this->container->get('cmf_routing.content_repository');
$route = new AutoRoute(); //Class of `Positibe\Bundle\CmfRoutingExtraBundle\Entity\AutoRoute`
$route->setStaticPrefix('/you-are-awesome'); //Set the permalink of post instance
$route->setDefault(RouteObjectInterface::CONTENT_ID, $contentRepository->getContentId($post)); this set ``FQN:id`` into ``content_id``
$route->setContent($post);
$post->addRoute($route);
$em->persist($post);
$em->flush();
自定义路由的内容
如果您的内容实现 Positibe\Bundle\CmfRoutingExtraBundle\Model\CustomRouteInterface
,您可以使用选定的控制器更新所有路由,而无需逐个更新。
[yaml]
# app/config/config.yml
positibe_cmf_routing_extra:
controllers:
homepage:
_controller: [FrameworkBundle:Template:template, {template: "index.html.twig"}]
default:
_controller: [AppBundle:Default:index, {}]
您可以通过 positibe_cmf_routing_extra.route_factory
访问此配置。
创建自动路由
见 auto_routing.md
。
有关更多信息,请参阅 Symfony Cmf Routing Bundle 文档