raindrop/routing-bundle

Symfony Raindrop 路由Bundle

安装: 2 000

依赖者: 1

建议者: 0

安全: 0

星标: 6

关注者: 6

分支: 3

开放问题: 1

类型:symfony-bundle

5.0.2 2022-11-10 09:38 UTC

README

Build Status

此Bundle基于symfony-cmf的路由扩展Bundle(可在https://github.com/symfony-cmf/RoutingBundle找到)。如果您想使用PHPCR ODM,symfony cmf路由扩展Bundle提供了更多路由选项。这是ORM简化版本。

它用链式路由器替换了symfony路由器,将标准路由器和动态路由器附加到它(如配置中指定)。这允许您将路由保存到数据库并继续使用symfony标准路由。

可以通过配置中指定的优先级键对附加到链式路由器的所有路由器进行排序。

此Bundle还提供了将路由链接到任何其他实体的选项,当控制器方法被调用时,将作为参数提供此对象。

安装:

首先将依赖项添加到您的 composer.json 文件中

"require": {
    ...
    "raindrop/routing-bundle": "dev-master"
},

然后使用以下命令安装Bundle

php composer.phar update

在您的应用程序内核中启用Bundle

<?php
// app/AppKernel.php

public function registerBundles()
{
    $bundles = array(
        // ...
        new Raindrop\RoutingBundle\RaindropRoutingBundle(),
    );
}

现在Bundle已被启用。

配置:

将以下行添加到 app/config/config.yml

doctrine:
  dbal:
    types:
      json: Sonata\Doctrine\Types\JsonType

raindrop_routing:
  chain:
    routers_by_id:
      router.default: 100
      raindrop_routing.dynamic_router: 10
    replace_symfony_router: true

这将指示配置断开symfony路由器,附加链式路由器,并附加symfony标准路由器和动态路由器。

动态路由器使用Doctrine ORM从/到数据库检索和存储路由。

使用:

将路由绑定到控制器

$route = new Route;
$route->setName('my_route');
$route->setPath('/path/to/my/route');
$route->setController('AcmeDemoBundle:Default:index');

带有 /path/to/my/route 的GET请求现在指向AcmeDemoBundle:Default:index

在twig模板中

{{ url('my_route') }}

这将解析数据库中定义的名称到URL。

将路由绑定到控制器作为服务

$route = new Route;
$route->setName('my_route');
$route->setPath('/path/to/my/route');
$route->setController('my_service.generic_controller:indexAction');

将路由绑定到控制器和目标对象

在这种情况下,控制器必须实现以下签名的方法。参数名称 'content' 是强制性的。

class myController {

    public function indexAction($content) 
    {
        // your code here
    }
}

调用时将传递内容到控制器。

$route = new Route;
$route->setName('my_route');
$route->setPath('/path/to/my/route');
$route->setController('AcmeDemoBundle:Default:index');
$route->setRouteContent('Acme\DemoBundle\Entity\SampleEntity:id:1');

当引用字段是 'id' 时,可以省略如下。

$route->setRouteContent('Acme\DemoBundle\Entity\SampleEntity::1');

这将检索具有id 1的Acme\DemoBundle\Entity\SampleEntity实体记录。

您也可以使用其他数据库字段引用对象

$route->setRouteContent('Acme\DemoBundle\Entity\SampleEntity:entity_id:1');

或使用setter方法

$route->setContent($object); // sets routeContent to 'Path\To\Entity:id:1'

$route->setContent($object, 'entity_id'); // sets routeContent 'Path\To\Entity:entity_id:1'

要绑定一个集合,请使用以下语法

$route->setRouteContent('Acme\DemoBundle\Entity\Route:locale(s):en');

所有具有locale 'en'的Acme\DemoBundle\Entity\Route都将被返回。

要使用setter传递一个数组和其他参数

$route->setContent($array, $field, $value);

$value参数可以是隐式的:$route->setContent($array, $field);。它将从第一个数组元素中检索,以及类类型。

将路由绑定到其他路由

要获得此结果,将重定向路由指向目标路由,并设置重定向控制器(或执行此任务的另一个控制器)。可选地设置301状态码(默认是302)并设置'permanent'属性。

$route = new Route;
$route->setName('my_route');
$route->setPath('/path/to/my/route');
$route->setPermanent(); // this for 301 status
$route->setController('raindrop_routing.generic_controller:redirectRouteAction');
$route->setRouteContent('Raindrop\RoutingBundle\Entity\Route:id:2');

$route->setContent($anotherRoute); // instead of $route->setRouteContent('…');

将路由绑定到外部URI

目标实体必须实现Raindrop\RoutingBundle\Routing\Base\ExternalRouteInterface方法。

$externalRoute = new ExternalRoute();
$externalRoute->setUri('http://www.domain.com/page/');
$externalRoute->setPermanent(); // this is for 301 status

$route = new Route;
$route->setName('my_route');
$route->setPath('/path/to/my/route');
$route->setController('RaindropRoutingBundle:Default:RedirectRoute');
$route->setContent($externalRoute);

限制

路由路径的最大长度为255个字符。