yasiekz / router-bundle
提供对象生成URL地址的功能,而不是提供路由名称和路由参数
Requires
- symfony/finder: >=2.4
- symfony/framework-bundle: >=2.4
Requires (Dev)
- jakub-onderka/php-parallel-lint: ~0.8
- phpunit/phpunit: ~4.4
This package is not auto-updated.
Last update: 2024-09-28 17:07:21 UTC
README
提供对象生成URL地址的功能,而不是提供路由名称和路由参数
安装
在AppKernel.php中添加Bundle
$bundles = array(
new Yasiekz\RouterBundle\YasiekzRouterBundle(),
)
Bundle自动覆盖默认的symfony2路由服务。
附加配置
不需要任何附加配置。
我应该使用什么接口?
我们提供了两个接口。RoutableCmsInterface在有多个路由的对象(例如,在CMS系统中,可能需要为编辑、删除对象提供不同的路由)时非常有用。RoutableFrontInterface适用于网站,当对象只有一个路由时,但一个对象可能根据对象所属的类别等因素有多个路由。RoutableMultiFrontStrategy是这两个接口的组合。
使用
RoutableCmsInterface
use Yasiekz\RouterBundle\Service\RoutableCmsInterface;
class YourClass implements RoutableCmsIterface
{
public function getPossibleRoutes()
{
// method should return an array of aviable routes as below
return array(
'destination1' => 'routingName1',
'destination2' => 'routingName2'
);
}
public function getRouterParameters($routeName, $destination = null);
{
// method should return parameters that is necessary to create routing depend on $destination parameter:
return array(
'id' => $this->getId()
);
}
}
URL生成与Symfony2默认方式相同。
从控制器
$object = new YourClass();
$url = $this->generateUrl($object, 'edit');
上面的例子为对象 $object 的 'edit' 目标生成间接地址
从twig
{{ path(object, 'edit') }}
RoutableFrontInterface
使用
use Yasiekz\RouterBundle\Service\RoutableFrontInterface;
class YourClass implements RoutableFrontIterface
{
public function getRouteName()
{
// method should return routeName for given object
return 'yourclass_detail';
}
public function getRouterParameters($routeName, $destination = null);
{
// method should return parameters that is necessary to create routing depend on $routeName parameter:
return array(
'id' => $this->getId()
);
}
}
URL生成与symfony2默认方式相同。
从控制器
$object = new YourClass();
$paramers = array(); // here might be additional params which be marged to routing
$url = $this->generateUrl($object, $parameters);
上面的例子生成了指向对象 $object 的间接地址,没有传递任何额外的参数。只会使用类 YourClass 的 getRouterParameters() 方法中的参数。
从Twig
{{ path(object, { 'param1': value1, 'param2': value2 }) }}
RoutableMultiFrontInterface
使用
use Yasiekz\RouterBundle\Service\RoutableMultiFrontInterface;
class YourClass implements RoutableMultiFrontInterface
{
const DESTINATION_ARTICLE = 'article';
public function getRouteName($parameters = array(), $destination = null)
{
// method should return routeName for given object and parameters or destination
if ($destination == self::DESTINATION_ARTICLE) {
return 'yourclass_detail';
}
return 'yourclass_default';
}
public function getRouterParameters($routeName, $destination = null);
{
// method should return parameters that is necessary to create routing depend on $routeName parameter:
return array(
'id' => $this->getId()
);
}
}
URL生成与symfony2默认方式相同。
从控制器
$object = new YourClass();
$parameters = array('destination' => 'article'); // here might be additional params which be marged to routing
$url = $this->generateUrl($object, $parameters);
上面的例子生成了带有目标参数的指向对象 $object 的间接地址,并将此参数与类 YourClass 的 getRouterParameters() 方法合并。
从Twig
{{ path(object, { 'destination': 'article', 'param1': value1, 'param2': value2 }) }}
重要
一个类无法同时实现所有接口。
贡献
我们非常鼓励您参与开发。条款与symfony2的https://symfony.com.cn/doc/current/contributing/code/patches.html相同。