久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

Yii2使用駝峰命名的形式訪問控制器的示例代碼

這篇文章主要介紹了Yii2使用駝峰命名的形式訪問控制器的實(shí)現(xiàn)方法,非常不錯,具有參考借鑒價(jià)值,需要的朋友可以參考下

yii2在使用的時(shí)候,訪問控制器的時(shí)候,如果控制器的名稱是駝峰命名法,那訪問的url中要改成橫線的形式。例如:

public function actionRoomUpdate()
{
//
}
//訪問的時(shí)候就要www.test.com/room-update這樣訪問

最近在做某渠道的直連的時(shí)候,他們提供的文檔上明確指出接口的形式:

Yii2使用駝峰命名的形式訪問控制器的示例代碼

剛開始以為YII2中肯定有這樣的設(shè)置,然后就去google了下,發(fā)現(xiàn)都說不行,自己去看了下,果然,框架里面直接是寫死的:(源碼)\vendor\yiisoft\yii2\base\Controller.php

Yii2使用駝峰命名的形式訪問控制器的示例代碼

/**
  * Creates an action based on the given action ID.
  * The method first checks if the action ID has been declared in [[actions()]]. If so,
  * it will use the configuration declared there to create the action object.
  * If not, it will look for a controller method whose name is in the format of `actionXyz`
  * where `Xyz` stands for the action ID. If found, an [[InlineAction]] representing that
  * method will be created and returned.
  * @param string $id the action ID.
  * @return Action the newly created action instance. Null if the ID doesn't resolve into any action.
  */
 public function createAction($id)
 {
  if ($id === '') {
   $id = $this->defaultAction;
  }
  $actionMap = $this->actions();
  if (isset($actionMap[$id])) {
   return Yii::createObject($actionMap[$id], [$id, $this]);
  } elseif (preg_match('/^[a-z0-9\\-_]+$/', $id) && strpos($id, '--') === false && trim($id, '-') === $id) {
   $methodName = 'action' . str_replace(' ', '', ucwords(implode(' ', explode('-', $id))));
   if (method_exists($this, $methodName)) {
    $method = new \ReflectionMethod($this, $methodName);
    if ($method->isPublic() && $method->getName() === $methodName) {
     return new InlineAction($id, $this, $methodName);
    }
   }
  }
  return null;
 }

這點(diǎn)有點(diǎn)low,不過問題倒不大,這個代碼很容易理解,我們發(fā)現(xiàn),其實(shí)如果在這個源碼的基礎(chǔ)上再加上一個else就可以搞定,但是還是不建議直接改源碼。

由于我們的項(xiàng)目用的事yii2的advanced版本,并且里面有多個項(xiàng)目,還要保證其他項(xiàng)目使用正常(也就是個別的控制器才需要使用駝峰命名的方式訪問),這也容易:

我們可以寫個components處理:\common\components\zController.php

<?php
/**
 * Created by PhpStorm.
 * User: Steven
 * Date: 2017/10/26
 * Time: 16:50
 */
namespace common\components;
use \yii\base\Controller;
use yii\base\InlineAction;
class zController extends Controller //這里需要繼承自\yii\base\Controller
{
 /**
  * Author:Steven
  * Desc:重寫路由,處理訪問控制器支持駝峰命名法
  * @param string $id
  * @return null|object|InlineAction
  */
 public function createAction($id)
 {
  if ($id === '') {
   $id = $this->defaultAction;
  }
  $actionMap = $this->actions();
  if (isset($actionMap[$id])) {
   return \Yii::createObject($actionMap[$id], [$id, $this]);
  } elseif (preg_match('/^[a-z0-9\\-_]+$/', $id) && strpos($id, '--') === false && trim($id, '-') === $id) {
   $methodName = 'action' . str_replace(' ', '', ucwords(implode(' ', explode('-', $id))));
   if (method_exists($this, $methodName)) {
    $method = new \ReflectionMethod($this, $methodName);
    if ($method->isPublic() && $method->getName() === $methodName) {
     return new InlineAction($id, $this, $methodName);
    }
   }
  } else {
   $methodName = 'action' . $id;
   if (method_exists($this, $methodName)) {
    $method = new \ReflectionMethod($this, $methodName);
    if ($method->isPublic() && $method->getName() === $methodName) {
     return new InlineAction($id, $this, $methodName);
    }
   }
  }
  return null;
 }
}

ok ,這就可以支持使用駝峰形式訪問了,當(dāng)然這個的形式很多,也可以寫成一個控制器,然后其它控制器繼承這個控制器就行了,但是原理是一樣的

如果使用?  是需要用駝峰命名形式訪問的控制器中,繼承下這個zController就可以了,

<?php
/**
 * Created by PhpStorm.
 * User: Steven
 * Date: 2017/10/18
 * Time: 15:57
 */
namespace backend\modules\hotel\controllers;
use yii\filters\AccessControl;
use yii\filters\ContentNegotiator;
use yii\web\Response;
use common\components\zController;
class QunarController extends zController
{
 public $enableCsrfValidation = false;
 public function behaviors()
 {
  $behaviors = parent::behaviors();
  unset($behaviors['authenticator']);
  $behaviors['corsFilter'] = [
   'class' => \yii\filters\Cors::className(),
   'cors' => [ // restrict access to
    'Access-Control-Request-Method' => ['*'], // Allow only POST and PUT methods
    'Access-Control-Request-Headers' => ['*'], // Allow only headers 'X-Wsse'
    'Access-Control-Allow-Credentials' => true, // Allow OPTIONS caching
    'Access-Control-Max-Age' => 3600, // Allow the X-Pagination-Current-Page header to be exposed to the browser.
    'Access-Control-Expose-Headers' => ['X-Pagination-Current-Page'],
   ],
  ];
  //配置ContentNegotiator支持JSON和XML響應(yīng)格式
  /*$behaviors['contentNegotiator'] = [
   'class' => ContentNegotiator::className(), 'formats' => [
    'application/xml' => Response::FORMAT_XML
   ]
  ];*/
  $behaviors['access'] = [
   'class' => AccessControl::className(),
   'rules' => [
    [
     'ips' => ['119.254.26.*', //去哪兒IP訪問白名單
      '127.0.0.1','106.14.56.77','180.168.4.58' //蜘蛛及本地IP訪問白名單
     ], 'allow' => true,
    ],
   ],
  ];
  return $behaviors;
 }
}
?>

示例:

/**
  * Author:Steven
  * Desc:酒店靜態(tài)數(shù)據(jù)接口
  */
 public function actiongetFullHotelInfo()
 {
 }

訪問的時(shí)候url為www.test.com/getFullHotelInfo

總結(jié)

【網(wǎng)站聲明】本站除付費(fèi)源碼經(jīng)過測試外,其他素材未做測試,不保證完整性,網(wǎng)站上部分源碼僅限學(xué)習(xí)交流,請勿用于商業(yè)用途。如損害你的權(quán)益請聯(lián)系客服QQ:2655101040 給予處理,謝謝支持。

相關(guān)文檔推薦

Yii2的rule用于對模型屬性進(jìn)行驗(yàn)證,scenario用戶定義不同場景下需要驗(yàn)證的模型,下面這篇文章主要給大家介紹了關(guān)于Yii2中場景(scenario)和驗(yàn)證規(guī)則(rule)的相關(guān)資料,文中通過示例代碼介
本篇文章主要介紹了淺談使用 Yii2 AssetBundle 中 $publishOptions 的正確姿勢,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
這篇文章主要介紹了Yii2之組件的注冊與創(chuàng)建的實(shí)現(xiàn)方法,非常不錯,具有參考借鑒價(jià)值,需要的朋友可以參考下
下面小編就為大家?guī)硪黄猋ii2使用駝峰命名的形式訪問控制器(實(shí)例講解)。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
這篇文章主要介紹了Yii2.0實(shí)現(xiàn)生成二維碼功能,結(jié)合實(shí)例形式分析了Yii2.0框架生成二維碼所涉及的相關(guān)擴(kuò)展引入步驟與使用方法,需要的朋友可以參考下
這篇文章主要介紹了Yii2框架實(shí)現(xiàn)登錄、退出及自動登錄功能的方法,結(jié)合實(shí)例形式詳細(xì)分析了Yii2框架實(shí)現(xiàn)登錄、退出及自動登錄功能的原理、實(shí)現(xiàn)方法與相關(guān)操作注意事項(xiàng),需要的朋友可
主站蜘蛛池模板: 国产免费又色又爽又黄在线观看 | 美女视频网站久久 | 男女在线网站 | 欧美精品久久久久久 | 成人区精品一区二区婷婷 | 久久99蜜桃综合影院免费观看 | 亚洲国产成人精品在线 | 亚洲精品久久久一区二区三区 | 久久久亚洲一区 | 伊人伊人 | 国产精品96久久久久久 | 亚洲欧洲成人av每日更新 | 91精品国产综合久久婷婷香蕉 | 91高清视频在线观看 | 亚洲喷水 | 精久久| 色吊丝在线 | 51ⅴ精品国产91久久久久久 | 久久国产精99精产国高潮 | 91影院在线观看 | 久久高清精品 | 男人天堂网址 | 国产亚洲精品美女久久久久久久久久 | 天天看天天操 | 久久精品成人 | 91精品国产综合久久精品 | 中文字幕一区在线观看视频 | 国产一区二区 | 国产精品无码专区在线观看 | 亚洲国产片 | 亚洲精品二三区 | 男女羞羞视频大全 | 亚洲国产成人av好男人在线观看 | 成人精品久久 | 日韩精品一 | 中文一区二区 | 91精品国产综合久久久久久丝袜 | 99视频免费播放 | 久久久高清 | 天堂在线91| 午夜视频免费在线 |