doulan8330 2015-09-07 00:55
浏览 28

如何在Symfony RoutingBundle(PHPCR)中创建具有自定义路径的路由?

I'm currently researching Symfony CMF and PHPCR for a project I recently started. What I'm currently trying to figure out is how to create a Route and save it into the database. As far as I understand, I must use Symfony\Cmf\Bundle\RoutingBundle\Doctrine\Phpcr\Route and persist the element into the database. This works fine, but automatically generates a route path, which is not what I want. What I need to do is generate a custom route which links to a specific controller. Here is my code:

$em = $this->get('doctrine_phpcr.odm.document_manager');
$parent = $em->find(null, '/cms/routes');

$route = new \Symfony\Cmf\Bundle\RoutingBundle\Doctrine\Phpcr\Route();
$route->setParentDocument($parent);
$route->setName('my_route_name');
$route->setDefault('_controller', 'AppBaseBundle:Frontend/Users:index');

$em->persist($route);
$em->flush();

If i execute this code, the generated route will be /cms/routes/my_route_name. From what I can see, you could use $route->setPath('/testing');, but that generates the following exception:

Can not determine the prefix. Either this is a new, unpersisted document or the listener that calls setPrefix is not set up correctly.

Does anybody have any ideas how to solve this?

  • 写回答

2条回答 默认 最新

  • dongrang2186 2015-09-07 16:34
    关注

    I managed to find a way to overcome this issue. Because in my project I also have the RouteAutoBundle, I created a class which extends \Symfony\Cmf\Bundle\RoutingBundle\Doctrine\Phpcr\Route. Inside that class I added:

    /**
     * @PHPCR\Document(referenceable=true)
     */
    class MenuRoute extends Route
    {
    
        protected $url;
    
        /**
         * Get $this->url
         *
         * @return mixed
         */
        public function getUrl() {
            return $this->url;
        }
    
        /**
         * Set $this->url
         *
         * @param mixed $url
         */
        public function setUrl($url) {
            $this->url = $url;
        }
    }
    

    After that I added this to cmf_routing_auto.yml:

    App\MenuBundle\Document\MenuRoute:
        uri_schema: /{getUrl}
        token_providers:
            getUrl: [content_method, { method: getUrl }]
    

    So now one would just create an instance of MenuRoute (just like when using Route) and call the method setUrl($your_url) passing the desired url.

    If anybody finds a better way, I'm opened to suggestions.

    评论

报告相同问题?

悬赏问题

  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器