如何在加载配置后添加动态 ColdBox 路由?

How can you add dynamic ColdBox routes after configuration loads?

我想根据数据库中的数据创建一些路线 table。我假设这需要在框架初始化后完成,所以我到目前为止所做的是创建一个运行在以下位置的新拦截器:afterConfigurationLoad.

我的拦截器从我的模型接收服务,该服务将查询创建路由所需的数据。但是,此时我不确定如何添加路线。当我尝试直接调用 addRoute() 时,出现错误 Variable ADDROUTE is undefined.,所以我猜 addRoute() 在框架 Supertype 中不存在。

这是我的拦截器的一些示例代码:

component {

     property name="myService" inject="myService"; // injects my model which will get some data

     /**
     * afterConfigurationLoad
     * Runs after the framework configucation loads
     */
     void function afterConfigurationLoad( event, interceptData, buffer, rc, prc ){

         // get some data which will be converted into routes
         var myDataList = myService.list();

         // loop through the data
         for ( var data in myDataList ) {

             // add our route. NOTE: I'll also want to populate the prc scope with a value
             addRoute( pattern="/#data.slug#", handler="data", action="index" );

         }

     }

 }

编辑 1:使用 ColdBox v5.1.1

更新一: 布拉德的回答让我走上了正确的轨道。我无法在我的拦截器中注入路由器,因为它产生了一个错误。但是,我能够使用 getInstance( "router@coldbox" ) 从函数中获取路由器的实例,然后根据需要调用 route() 方法来创建我的路由。

重要提示: 默认情况下,路由附加到路由 table。如果你想让它们工作,你可能需要在路由前面加上。

这是可用的代码的更新版本:

解决方案一:

component {

    property name="myService" inject="myService"; // injects my model which will get some data


    /**
    * afterConfigurationLoad
    * Runs after the framework configucation loads
    */
    void function afterConfigurationLoad( event, interceptData, buffer, rc, prc ){

        // instance the router
        var router = getInstance( "router@coldbox" );

        // get some data which will be converted into routes
        var myDataList = myService.list();

        // loop through the data
        for ( var data in myDataList ) {

            // prepend our route
            router.route( "/#data.slug#" ).prcAppend( { id : #data.id# } ).to( "data.index" ).prepend();

        }

    }

}

此外,可能有一种更简单的方法来解决这个问题,只需将模型服务注入“config/router.cfc”配置文件并以这种方式添加任何动态路由。

方案二:

component {

    property name="myService" inject="myService"; // inject the model

    function configure() {

        setFullRewrites( true );

        // get the data I need from the model for dynamic routes
        var myDataList = myService.list();

        // loop through the data
        for ( var data in myDataList ) {

            // add the dynamic route
            router.route( "/#data.slug#" ).prcAppend( { id : #data.id# } ).to( "data.index" );

        }

        route( ":handler/:action?" ).end();
    }

}

方案三:

事实证明,当框架初始化时,首先加载拦截器,因此并非所有依赖项都可用。 Brad 建议在 属性 中使用 provider 命名空间,这也应该是一个 acceptable 解决方案。

component {

    property name="myService" inject="myService"; // injects my model which will get some data
    property name="router" inject="provider:router@coldbox";


    /**
    * afterConfigurationLoad
    * Runs after the framework configucation loads
    */
    void function afterConfigurationLoad( event, interceptData, buffer, rc, prc ){

        // get some data which will be converted into routes
        var myDataList = myService.list();

        // loop through the data
        for ( var data in myDataList ) {

            // prepend our route
            router.route( "/#data.slug#" ).prcAppend( { id : #data.id# } ).to( "data.index" ).prepend();

        }

    }

}

注入 ColdBox 路由器

property name='router' inject='router@coldbox';

并调用 API 文档中详述的方法:

http://apidocs.ortussolutions.com/coldbox/5.2.0/index.html?coldbox/system/web/routing/Router.html

addRoute() 方法是该 CFC 的一部分。