dsfadsa08911 2015-06-16 05:52
浏览 55
已采纳

Yii 1:UrlManager不会在模块控制器中调用正确的操作

i am creating an API module in an application and i have to set some rules in urlManager, however when i set a single rule and test it if it's working, it calls the index action instead of the desired action.

in Controller

<?php     
Class ProjectsController extends Controller
{
    // Do nothing on this request
    public function actionIndex()
    {
        // this is being echoed even if this action is not being requested
        echo 'test';
    }

    /*
    *   Retrieve all projects
    */
    public function actionAll()
    {
        $projects = Project::model()->findAllApi();

        echo CJSON::encode($projects);
    }

    public function actionView($id)
    {
        echo 'asd';
    }
}

urlManager in config/main.php

'urlManager'=>array(
            'urlFormat'=>'path',
            'showScriptName'=>false,
            'urlSuffix'=>'.php',
            'rules'=>array(
                '<module:\w+>/<controller:\w+>/<id:\d+>'=>'<module>/<controller>/view',
                '<controller:\w+>/<id:\d+>'=>'<controller>/view',
                '<controller:\w+>/<action:\w+>/<id:\d+>'=>'<controller>/<action>',
                '<controller:\w+>/<action:\w+>'=>'<controller>/<action>',
            ),
        ),

ApiModule.php

<?php

class ApiModule extends CWebModule
{
    public function init()
    {
        // this method is called when the module is being created
        // you may place code here to customize the module or the application

        // import the module-level models and components
        $this->setImport(array(
            'api.models.*',
            'api.components.*',
        ));
    }

    public function beforeControllerAction($controller, $action)
    {
        if(parent::beforeControllerAction($controller, $action))
        {
            // this method is called before any module controller action is performed
            // you may place customized code here
            return true;
        }
        else
            return false;
    }
}

so if i request http://localhost/<application>/api/projects/2 it calls index action instead of view. how to fix this?

  • 写回答

1条回答 默认 最新

  • dongnai6973 2015-06-16 07:19
    关注

    Everything is correct in your code. I didn't find any error in your code! However try to remove '<module:\w+>/<controller:\w+>/<id:\d+>'=>'<module>/<controller>/view' rule and replace api/projects/<id:\d+> => api/projects/view. This could be helpful.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 怎么在stm32门禁成品上增加记录功能
  • ¥15 Source insight编写代码后使用CCS5.2版本import之后,代码跳到注释行里面
  • ¥50 NT4.0系统 STOP:0X0000007B
  • ¥15 想问一下stata17中这段代码哪里有问题呀
  • ¥15 flink cdc无法实时同步mysql数据
  • ¥100 有人会搭建GPT-J-6B框架吗?有偿
  • ¥15 求差集那个函数有问题,有无佬可以解决
  • ¥15 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 解riccati方程组