最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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

 更新時間:2017年10月30日 10:12:54   作者:Steven*  
這篇文章主要介紹了Yii2使用駝峰命名的形式訪問控制器的實(shí)現(xiàn)方法,非常不錯,具有參考借鑒價(jià)值,需要的朋友可以參考下

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

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

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

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

/**
  * 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()
 {
 }

訪問的時候url為www.test.com/getFullHotelInfo

總結(jié)

以上所述是小編給大家介紹的Yii2使用駝峰命名的形式訪問控制器,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復(fù)大家的。在此也非常感謝大家對腳本之家網(wǎng)站的支持!

相關(guān)文章

  • ecshop添加菜單及權(quán)限分配問題

    ecshop添加菜單及權(quán)限分配問題

    ECShop是一款B2C獨(dú)立網(wǎng)店系統(tǒng),適合企業(yè)及個人快速構(gòu)建個性化網(wǎng)上商店。這篇文章主要介紹了ecshop添加菜單及權(quán)限分配,需要的朋友可以參考下
    2017-11-11
  • wordpress網(wǎng)站轉(zhuǎn)移到本地運(yùn)行測試的方法

    wordpress網(wǎng)站轉(zhuǎn)移到本地運(yùn)行測試的方法

    這篇文章主要為大家詳細(xì)介紹了wordpress網(wǎng)站轉(zhuǎn)移到本地運(yùn)行測試的方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2017-03-03
  • 基于PHP異步執(zhí)行的常用方式詳解

    基于PHP異步執(zhí)行的常用方式詳解

    本篇文章是對PHP異步執(zhí)行的常用方式進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下
    2013-06-06
  • xml在joomla表單中的應(yīng)用詳解分享

    xml在joomla表單中的應(yīng)用詳解分享

    xml在joomla中應(yīng)用廣泛,從安裝文件配置到模版,模塊,組件,插件中都有應(yīng)用
    2012-07-07
  • 簡單PHP會話(session)說明介紹

    簡單PHP會話(session)說明介紹

    下面小編就為大家?guī)硪黄唵蜳HP會話(session)說明介紹。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2016-08-08
  • CodeIgniter框架數(shù)據(jù)庫基本操作示例

    CodeIgniter框架數(shù)據(jù)庫基本操作示例

    這篇文章主要介紹了CodeIgniter框架數(shù)據(jù)庫基本操作,結(jié)合實(shí)例形式分析了CodeIgniter框架針對mysql數(shù)據(jù)庫的配置、用戶注冊、信息查詢、修改及刪除等基本操作技巧,需要的朋友可以參考下
    2018-05-05
  • php數(shù)組去重復(fù)數(shù)據(jù)示例

    php數(shù)組去重復(fù)數(shù)據(jù)示例

    這篇文章主要介紹了php數(shù)組去重復(fù)數(shù)據(jù)示例,有時候獲得的php數(shù)組中總是出現(xiàn)value重復(fù)的,使用下面的方法就可以去掉重復(fù)數(shù)據(jù)
    2014-02-02
  • 用PHP寫的一個冒泡排序法的函數(shù)簡單實(shí)例

    用PHP寫的一個冒泡排序法的函數(shù)簡單實(shí)例

    下面小編就為大家?guī)硪黄肞HP寫的一個冒泡排序法的函數(shù)簡單實(shí)例。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2016-05-05
  • YII Framework框架教程之安全方案詳解

    YII Framework框架教程之安全方案詳解

    這篇文章主要介紹了YII Framework框架教程之安全方案,結(jié)合實(shí)例形式詳細(xì)分析了針對跨站腳本攻擊,跨站請求偽造攻擊及Cookie攻擊的防范技巧,需要的朋友可以參考下
    2016-03-03
  • PHP之深入學(xué)習(xí)Yii2緩存Cache組件詳細(xì)講解

    PHP之深入學(xué)習(xí)Yii2緩存Cache組件詳細(xì)講解

    這篇文章主要介紹了PHP之深入學(xué)習(xí)Yii2緩存Cache組件詳細(xì)講解,本篇文章通過簡要的案例,講解了該項(xiàng)技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下
    2021-07-07

最新評論

泰州市| 台江县| 喀喇| 德令哈市| 公主岭市| 连州市| 马鞍山市| 安平县| 义马市| 防城港市| 板桥市| 锡林郭勒盟| 博乐市| 敦化市| 南丰县| 华安县| 正阳县| 湖北省| 瑞金市| 句容市| 黄梅县| 绥芬河市| 盐源县| 桂林市| 九龙坡区| 来安县| 晋江市| 华宁县| 新邵县| 堆龙德庆县| 称多县| 西华县| 察隅县| 乐平市| 贵州省| 南汇区| 天峨县| 育儿| 永昌县| 会宁县| 靖江市|