【问题标题】:Routing requests to module将请求路由到模块
【发布时间】:2011-07-11 20:37:19
【问题描述】:

您好,我正在寻找有关将请求路由到 Yii 框架中的自定义模块的最佳方式的一些信息。

我正在为一个项目实现一个 RestFul Api,并希望有某种方法可以简单地将所有请求路由到 api/ 到其余 api 所在的模块。比这更好的是有人如何将 api 请求路由到自定义 UrlManager 类,该类扩展模块中的 CUrlManager,然后处理路由。所以对 mydomain/api/user/model 的请求实际上会被模块中的 UrlManager 组件延迟和处理,而其他请求,即 mydoamin.com/client/create 将由普通的 yii 应用程序简单地处理。据我所知这是不可能的!!

所以我会满足于在我的配置中定义一个 url 管理器类来捕获这样的 api 路由

class UrlManager extends CUrlManager
{
    protected function processRules()
    {
        if(!isset($_GET['r']))
        {           
            $this->setUrlFormat('path');
            $this->showScriptName=false;
            $this->rules=array(

                //Api Rest Patterns
                array('api/list', 'pattern'=>'^api/user/<model:\w+>', 'verb'=>'GET'),
                array('api/view', 'pattern'=>'^api/user/<model:\w+>/<id:\d+>', 'verb'=>'GET'),
                array('api/update', 'pattern'=>'^api/user/<model:\w+>/<id:\d+>', 'verb'=>'PUT'),
                array('api/delete', 'pattern'=>'^api/user/<model:\w+>/<id:\d+>', 'verb'=>'DELETE'),
                array('api/add', 'pattern'=>'^api/user/<model:\w+>', 'verb'=>'POST'),
                array('api/test', 'pattern'=>'^api/user/test/<model:\w+>'),
                array('api/login', 'pattern'=>'^api/user/<model:\w+>/login'),
                array('api/logout', 'pattern'=>'^api/user/<model:\w+>/logout'),


                // Other controllers
                '<controller:\w+>/<action:\w+>/<id:\d+>'=>'<controller>/<action>',
                '<controller:\w+>/<action:\w+>'=>'<controller>/<action>',                
                '<action:\w+>'=>'site/<action>',
            );
        }
        return parent::processRules();
    }
}

然后我的问题是,我如何将请求路由到模块中的控制器?即模块/RestApi/controller/UserApiController.php

array('ModuleController/action' , pattern=>'api/user/<model>' , 'verb'=>'GET)

我想过这个

'controllerMap' => array(
    'api'=>'application.modules.RestApi.components.ApiManager',
),

但我很确定我需要两个访问点,一个用于处理 api 上的管理任务,一个用于用户,所以目前我的控制器结构如下所示:

  • RestApiController
  • UserApiController(扩展 RestApiController)
  • AdminApiController(扩展 RestApiController)

那么,如果有一种方法可以将动作动态路由到可能有效的子控制器?希望我没有在这里混淆,真的希望你们中的 Yii 大师可以帮助解决这个问题!

提前致谢

【问题讨论】:

    标签: php yii url-routing


    【解决方案1】:

    我可能误解了你的问题(如果我理解了,请纠正我),但你不能只使用普通的 urlmanager 和像这样配置的“正常”规则:

    'rules' = array(
        //Api Rest Patterns
        array('restApi/userApi/list', 'pattern'=>'^api/user/<model:\w+>', 'verb'=>'GET'),
        array('restApi/userApi/view', 'pattern'=>'^api/user/<model:\w+>/<id:\d+>', 'verb'=>'GET'),
        array('restApi/userApi/update', 'pattern'=>'^api/user/<model:\w+>/<id:\d+>', 'verb'=>'PUT'),
        array('restApi/userApi/delete', 'pattern'=>'^api/user/<model:\w+>/<id:\d+>', 'verb'=>'DELETE'),
        array('restApi/userApi/add', 'pattern'=>'^api/user/<model:\w+>', 'verb'=>'POST'),
        array('restApi/userApi/test', 'pattern'=>'^api/user/test/<model:\w+>'),
        array('restApi/userApi/login', 'pattern'=>'^api/user/<model:\w+>/login'),
        array('restApi/userApi/logout', 'pattern'=>'^api/user/<model:\w+>/logout'),
    
    
        // Other controllers
        '<controller:\w+>/<action:\w+>/<id:\d+>'=>'<controller>/<action>',
        '<controller:\w+>/<action:\w+>'=>'<controller>/<action>',                
        '<action:\w+>'=>'site/<action>',
    )
    

    我很确定这样的事情应该可以工作(虽然不完全确定 camelCasing)。模块操作的路由是'&lt;moduleid&gt;/&lt;controllerid&gt;/&lt;actionid&gt;',我肯定知道,因为我不止一次使用它。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-05-20
      • 2011-11-17
      • 1970-01-01
      • 1970-01-01
      • 2022-06-30
      • 2020-04-29
      相关资源
      最近更新 更多