doulianxing4015 2013-01-15 11:57
浏览 51
已采纳

Yii:禁用对Controller / Action的直接访问,仅在路由到时有效

I would like to disable direct access to a Controller/Action in the Yii Framework, for example login/get, and only be accessible if routed to through CUrlManager.

In my configuration I am routing requests for login to login/get and login/post depending on the HTTP method:

'urlManager' => array(
    'urlFormat' => 'path',
    'showScriptName' => false,
    'caseSensitive' => false,
    'rules' => array(
        // Rewrite GET requests of "login" to "login/get", and POST requests to "login/post".
        array('login/post', 'pattern' => 'login', 'caseSensitive' => false, 'verb' => 'POST'),
        array('login/get',  'pattern' => 'login', 'caseSensitive' => false, 'verb' => 'GET'),
    ),
),

Is there any configuration setting to disallow requests to login/get and login/post, or would the best way be to route those requests to somewhere such as error/404? For example:

array('error/404',  'pattern' => 'login/(get|post)(/.*)?')
  • 写回答

1条回答 默认 最新

  • du4373 2013-01-18 07:54
    关注

    Well your own solutions works just fine, the UrlManager requests rules from top to bottom, so it will try the GET/POST first

    so the full answer would be

    'rules' => array(
        // Rewrite GET requests of "login" to "login/get", and POST requests to "login/post".
        array('login/post', 'pattern' => 'login', 'caseSensitive' => false, 'verb' => 'POST'),
        array('login/get',  'pattern' => 'login', 'caseSensitive' => false, 'verb' => 'GET'),
        'login/post'=>'error/404',
        'login/get'=>'error/404',
    ),
    

    you can probably just use the rule you gave with regex

    array('error/404',  'pattern' => 'login/(get|post)(/.*)?'
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100
  • ¥15 关于#hadoop#的问题
  • ¥15 (标签-Python|关键词-socket)
  • ¥15 keil里为什么main.c定义的函数在it.c调用不了
  • ¥50 切换TabTip键盘的输入法
  • ¥15 可否在不同线程中调用封装数据库操作的类
  • ¥15 微带串馈天线阵列每个阵元宽度计算
  • ¥15 keil的map文件中Image component sizes各项意思
  • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏