定制 positibe/cmf-routing-extra-bundle 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

positibe/cmf-routing-extra-bundle

Composer 安装命令:

composer require positibe/cmf-routing-extra-bundle

包简介

Symfony Positibe CmfRoutingExtra Bundle

README 文档

README

The PositibeCmfRoutingExtraBundle add Doctrine ORM support for Symfony CmfRoutingBundle to store routing on orm databses

Installation

To install the bundle just add the dependent bundles:

php composer.phar require positibe/cmf-routing-extra-bundle

Next, be sure to enable the bundles in your application kernel:

<?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(),

        // ...
    );
}

Configuration

Copy the configuration in your configurations packages:

# 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

Caution:: This bundle use the timestampable, sluggable, translatable and sortable extension of GedmoDoctrineExtension. Be sure that you have the listeners for this extensions enable. You can also to use StofDoctrineExtensionBundle.

Remember to update the schema:

php app/console doctrine:schema:update --force

Using

An entity that has routes must implement Symfony\Cmf\Component\Routing\RouteReferrersInterface.

Add to any entity you want the relation with Positibe\Bundle\CmfRoutingExtraBundle\Entity\AutoRoute and the needed methods:

<?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);
    }
}

Tip: You can use Positibe\Bundle\CmfRoutingExtraBundle\Entity\HasRoutesTrait to simplify the implementation of RouteReferrerInterface methods and mapping. This create a many to many relation without doing nothing more.

<?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();
    }
}

Creating routes

$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();

Content with Custom Routing

If your content implement Positibe\Bundle\CmfRoutingExtraBundle\Model\CustomRouteInterface, you can update all your routes with the selected controller, without the need of do it on each one.

[yaml]
# app/config/config.yml
positibe_cmf_routing_extra:
    controllers:
        homepage:
            _controller: [FrameworkBundle:Template:template, {template: "index.html.twig"}]
        default:
            _controller: [AppBundle:Default:index, {}]

You have the access to this config through positibe_cmf_routing_extra.route_factory.

Creating automatic routes

See on auto_routing.md.

For more information see the Symfony Cmf Routing Bundle Documentation

统计信息

  • 总下载量: 15
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 1
  • 点击次数: 5
  • 依赖项目数: 1
  • 推荐数: 0

GitHub 信息

  • Stars: 1
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2017-03-23

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固