dro44817 2017-04-15 11:12
浏览 105

Yii2 REST URL使用slugs代替view-slug方法中的id

I want to declare a method actionViewSlug($slug) in ScholarshipController in Yii2 REST api, My method is showing Not Found that too not in REST manner i.e. JSON.

Here is my Url Config for ScholarshipController

[
                    'class' => 'yiiest\UrlRule',
                    'controller' => ['scholarship'],
                    'extraPatterns' => [
                            'POST filters' => 'filters',
                            'GET {slug}' => 'view-slug',
                    ],
                    'tokens' => [
                            '{slug}' => '<slug>'
                    ],


            ],

This is behaviors() function in ScholarshipController

public function behaviors()
  {
        return [
                [
                    'class' => 'yii\filters\ContentNegotiator',
                    'only' => ['view', 'index', 'filters', 'view-slug'],  // in a controller
                    // if in a module, use the following IDs for user actions
                    // 'only' => ['user/view', 'user/index']
                    'formats' => [
                        'application/json' => Response::FORMAT_JSON,
                    ],
                ],
                'corsFilter' => [
                    'class' => \yii\filters\Cors::className(),
                    'cors' => [
                        'Origin' => ['*'],
                        'Access-Control-Request-Method' => ['GET', 'POST', 'PUT', 'PATCH', 'DELETE', 'HEAD', 'OPTIONS'],
                        'Access-Control-Request-Headers' => ['*'],
                        'Access-Control-Allow-Credentials' => null,
                        'Access-Control-Max-Age' => 86400, 
                        'Access-Control-Expose-Headers' => [],
                    ],

                ],

        ];

  }
  • 写回答

1条回答 默认 最新

  • duanfanta6741 2017-05-17 06:37
    关注

    Try this URL rule may it should help you. But befor proceeding with this rule I want to clear you if you are using a module for rest API you must need to define your module ID too in rule either using prefix or direct

    [
        'class' => 'yiiest\UrlRule',
        'controller' => '<moduleID>/scholarships',        
        'tokens' => [
            '{slug}' => '<slug>',
        ],
        'extraPatterns' => [
            'GET,HEAD {slug}' => 'view-slug',
        ]
    ],
    

    and where is youe actionViewSlug($slug); It is good if you share your controller code too. It's simple to use slug with different actions. One more thing here I would like to know why you are defining viewSlug action? Why you are not using the existing view action with slug? Thanks

    评论

报告相同问题?

悬赏问题

  • ¥15 要给毕业设计添加扫码登录的功能!!有偿
  • ¥15 kafka 分区副本增加会导致消息丢失或者不可用吗?
  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥15 stable diffusion
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件