亚洲国产日韩欧美一区二区三区,精品亚洲国产成人av在线,国产99视频精品免视看7,99国产精品久久久久久久成人热,欧美日韩亚洲国产综合乱

目錄
驗(yàn)證數(shù)據(jù)
示例
輸出
Http - 客戶端
HTTP GET 方法
HTTP POST 方法
首頁(yè) 后端開(kāi)發(fā) php教程 CakePHP 創(chuàng)建驗(yàn)證器

CakePHP 創(chuàng)建驗(yàn)證器

Sep 10, 2024 pm 05:26 PM
php cakephp PHP framework

可以通過(guò)在控制器中添加以下兩行來(lái)創(chuàng)建驗(yàn)證器。

use Cake\Validation\Validator;
$validator = new Validator();

驗(yàn)證數(shù)據(jù)

一旦我們創(chuàng)建了驗(yàn)證器,我們就可以使用驗(yàn)證器對(duì)象來(lái)驗(yàn)證數(shù)據(jù)。以下代碼說(shuō)明了我們?nèi)绾悟?yàn)證登錄網(wǎng)頁(yè)的數(shù)據(jù)。

$validator->notEmpty('username', 'We need username.')->add(
   'username', 'validFormat', ['rule' => 'email','message' => 'E-mail must be valid']);

$validator->notEmpty('password', 'We need password.');
$errors = $validator->errors($this->request->data());

使用 $validator 對(duì)象,我們首先調(diào)用 notEmpty() 方法,這將確保用戶名不能為空。之后,我們鏈接了 add() 方法來(lái)添加一個(gè)正確的電子郵件格式驗(yàn)證。

之后,我們使用 notEmpty() 方法添加了對(duì)密碼字段的驗(yàn)證,這將確認(rèn)密碼字段不能為空。

示例

在 config/routes.php 文件中進(jìn)行更改,如以下程序所示。

config/routes.php

<?php
use Cake\Http\Middleware\CsrfProtectionMiddleware;
use Cake\Routing\Route\DashedRoute;
use Cake\Routing\RouteBuilder;
$routes->setRouteClass(DashedRoute::class);
$routes->scope('/', function (RouteBuilder $builder) {
   $builder->registerMiddleware('csrf', new CsrfProtectionMiddleware([
      'httpOnly' => true,
   ]));
   $builder->applyMiddleware('csrf');
   //$builder->connect('/pages',['controller'=>'Pages','action'=>'display', 'home']);
   $builder->connect('validation',['controller'=>'Valids','action'=>'index']);
   $builder->fallbacks();
});

src/Controller/ValidsController.php 創(chuàng)建一個(gè) ValidsController.php 文件。 將以下代碼復(fù)制到控制器文件中。

src/Controller/ValidsController.php

<?php
   namespace App\Controller;
   use App\Controller\AppController;
   use Cake\Validation\Validator;
   class ValidsController extends AppController{
      public function index(){
         $validator = new Validator();
         $validator->notEmpty('username', 'We need username.')->add(
            'username', 'validFormat', ['rule' => 'email','message' => 'E-mail must be valid']);
         $validator->notEmpty('password', 'We need password.');
         $errors = $validator->errors($this->request->getData());
         $this->set('errors',$errors);
      }
   }
?>

src/Template 處創(chuàng)建一個(gè)目錄 Valids 并在該目錄下創(chuàng)建一個(gè) View 文件,名為 index.php。 復(fù)制以下代碼位于該文件中。

src/Template/Valids/index.php

<?php
   if($errors) {
      foreach($errors as $error)
      foreach($error as $msg)
      echo '<font color="red">'.$msg.'</font><br>';
   } else {
      echo "No errors.";
   }
   echo $this->Form->create(NULL,array('url'=>'/validation'));
   echo $this->Form->control('username');
   echo $this->Form->control('password');
   echo $this->Form->button('Submit');
   echo $this->Form->end();
?>

通過(guò)訪問(wèn)以下 URL 執(zhí)行上述示例 -

http://localhost/cakephp4/validation

輸出

點(diǎn)擊提交按鈕,無(wú)需輸入任何內(nèi)容。您將收到以下輸出。

Click PHP

Http - 客戶端

http 客戶端可用于發(fā)出 GET、POST、PUT 等請(qǐng)求

要使用 http 客戶端,請(qǐng)?zhí)砑右韵聝?nèi)容 -

use Cake\Http\Client;

讓我們通過(guò)示例來(lái)了解 HTTP 客戶端的工作原理。

HTTP GET 方法

要從給定的http url獲取數(shù)據(jù),您可以執(zhí)行以下操作 -

$response = $http->get('https://jsonplaceholder.typicode.com/users');

如果您需要傳遞一些查詢參數(shù),可以按如下方式傳遞 -

$response = $http->get('https://jsonplaceholder.typicode.com/users', ["id", 1]);

要獲得回復(fù),您可以執(zhí)行以下操作 -

對(duì)于普通文本數(shù)據(jù)?

$response->getBody();

對(duì)于Json -

$response->getJson();

對(duì)于 Xml ?

$response->getXml()

示例

在 config/routes.php 文件中進(jìn)行更改,如以下程序所示。

config/routes.php

<?php
use Cake\Http\Middleware\CsrfProtectionMiddleware;
use Cake\Routing\Route\DashedRoute;
use Cake\Routing\RouteBuilder;
$routes->setRouteClass(DashedRoute::class);
$routes->scope('/', function (RouteBuilder $builder) {
   $builder->registerMiddleware('csrf', new CsrfProtectionMiddleware([
      'httpOnly' => true,
   ]));
   $builder->applyMiddleware('csrf');
   //$builder->connect('/pages',['controller'=>'Pages','action'=>'display', 'home']);
   $builder->connect('getData',['controller'=>'Requests','action'=>'index']);
   $builder->fallbacks();
});

src/Controller/RequestsController.php 創(chuàng)建一個(gè) RequestsController.php 文件。 將以下代碼復(fù)制到控制器文件中。

src/Controller/RequestsController.php

<?php
   namespace App\Controller;
   use App\Controller\AppController;
   use Cake\Http\Client;
   class RequestsController extends AppController{
      public function index(){
         $http = new Client();
         $response = $http->get('https://jsonplaceholder.typicode.com/users');
         $stream = $response->getJson();
         $this->set('response',$stream);
      }
   }
?>

src/Template 處創(chuàng)建一個(gè)目錄 Requests 并在該目錄下創(chuàng)建一個(gè) View 文件,名為 index.php。 復(fù)制以下代碼位于該文件中。

src/Template/Requests/index.php

<h3>All Users from url : https://jsonplaceholder.typicode.com/users</h3>
<?php
   if($response) {
      foreach($response as $res => $val) {
         echo '<font color="gray">Name: '.$val["name"].' Email -'.$val["email"].'</font><br>';
      }
   }
?>

通過(guò)訪問(wèn)以下 URL 執(zhí)行上述示例 -

http://localhost/cakephp4/getData

輸出

點(diǎn)擊提交按鈕,無(wú)需輸入任何內(nèi)容。您將收到以下輸出。

Users URL

HTTP POST 方法

要使用 post,您需要調(diào)用 $http 客戶端,如下所示 -

$response = $http->post('yoururl', data);

讓我們看一個(gè)例子。

示例

在 config/routes.php 文件中進(jìn)行更改,如以下程序所示。

config/routes.php

<?php
use Cake\Http\Middleware\CsrfProtectionMiddleware;
use Cake\Routing\Route\DashedRoute;
use Cake\Routing\RouteBuilder;
$routes->setRouteClass(DashedRoute::class);
$routes->scope('/', function (RouteBuilder $builder) {
   $builder->registerMiddleware('csrf', new CsrfProtectionMiddleware([
      'httpOnly' => true,
   ]));
   $builder->applyMiddleware('csrf');
   //$builder->connect('/pages',['controller'=>'Pages','action'=>'display', 'home']);
   $builder->connect('postData',['controller'=>'Requests','action'=>'index']);
   $builder->fallbacks();
});

src/Controller/RequestsController.php 創(chuàng)建 RequestsController.php 文件。 將以下代碼復(fù)制到控制器文件中。如果已創(chuàng)建,請(qǐng)忽略。

src/Controller/RequestsController.php

<?php
   namespace App\Controller;
   use App\Controller\AppController;
   use Cake\Http\Client;
   class RequestsController extends AppController{
      public function index(){
         $http = new Client();
         $response = $http->post('https://postman-echo.com/post', [
            'name'=> 'ABC',
            'email' => 'xyz@gmail.com'
         ]);
      }
   }
?>

src/Template處創(chuàng)建一個(gè)目錄Requests,并在該目錄下創(chuàng)建一個(gè)名為index.php的View文件。將以下代碼復(fù)制到該文件中。

src/Template/Requests/index.php

<h3>Testing Post Method</h3>

通過(guò)訪問(wèn)以下 URL 執(zhí)行上述示例 -

http://localhost/cakephp4/postData

輸出

下面給出的是代碼的輸出 -

Post Method

同樣,你可以嘗試PUT方法。

$http = new Client();
$response = $http->put('https://postman-echo.com/post', [
   'name'=> 'ABC',
   'email' => 'xyz@gmail.com'
]);

以上是CakePHP 創(chuàng)建驗(yàn)證器的詳細(xì)內(nèi)容。更多信息請(qǐng)關(guān)注PHP中文網(wǎng)其他相關(guān)文章!

本站聲明
本文內(nèi)容由網(wǎng)友自發(fā)貢獻(xiàn),版權(quán)歸原作者所有,本站不承擔(dān)相應(yīng)法律責(zé)任。如您發(fā)現(xiàn)有涉嫌抄襲侵權(quán)的內(nèi)容,請(qǐng)聯(lián)系admin@php.cn

熱AI工具

Undress AI Tool

Undress AI Tool

免費(fèi)脫衣服圖片

Undresser.AI Undress

Undresser.AI Undress

人工智能驅(qū)動(dòng)的應(yīng)用程序,用于創(chuàng)建逼真的裸體照片

AI Clothes Remover

AI Clothes Remover

用于從照片中去除衣服的在線人工智能工具。

Clothoff.io

Clothoff.io

AI脫衣機(jī)

Video Face Swap

Video Face Swap

使用我們完全免費(fèi)的人工智能換臉工具輕松在任何視頻中換臉!

熱工具

記事本++7.3.1

記事本++7.3.1

好用且免費(fèi)的代碼編輯器

SublimeText3漢化版

SublimeText3漢化版

中文版,非常好用

禪工作室 13.0.1

禪工作室 13.0.1

功能強(qiáng)大的PHP集成開(kāi)發(fā)環(huán)境

Dreamweaver CS6

Dreamweaver CS6

視覺(jué)化網(wǎng)頁(yè)開(kāi)發(fā)工具

SublimeText3 Mac版

SublimeText3 Mac版

神級(jí)代碼編輯軟件(SublimeText3)

如何用JavaScript判斷兩個(gè)數(shù)組是否相等? 如何用JavaScript判斷兩個(gè)數(shù)組是否相等? May 23, 2025 pm 10:51 PM

JavaScript中判斷兩個(gè)數(shù)組是否相等需要使用自定義函數(shù),因?yàn)闆](méi)有內(nèi)置方法。1)基本實(shí)現(xiàn)通過(guò)比較長(zhǎng)度和元素,但不能處理對(duì)象和數(shù)組。2)遞歸深度比較能處理嵌套結(jié)構(gòu),但需特別處理NaN。3)還需考慮函數(shù)、日期等特殊類型,需進(jìn)一步優(yōu)化和測(cè)試。

PHP中如何驗(yàn)證社保號(hào)字符串? PHP中如何驗(yàn)證社保號(hào)字符串? May 23, 2025 pm 08:21 PM

社保號(hào)驗(yàn)證在PHP中通過(guò)正則表達(dá)式和簡(jiǎn)單邏輯實(shí)現(xiàn)。1)使用正則表達(dá)式清理輸入,去除非數(shù)字字符。2)檢查字符串長(zhǎng)度是否為18位。3)計(jì)算并驗(yàn)證校驗(yàn)位,確保與輸入的最后一位匹配。

如何在閉包中正確處理this指向? 如何在閉包中正確處理this指向? May 21, 2025 pm 09:15 PM

在JavaScript閉包中正確處理this指向的方法有:1.使用箭頭函數(shù),2.使用bind方法,3.使用變量保存this。這些方法能確保內(nèi)部函數(shù)的this正確指向外部函數(shù)的上下文。

怎樣用JavaScript實(shí)現(xiàn)數(shù)據(jù)加密? 怎樣用JavaScript實(shí)現(xiàn)數(shù)據(jù)加密? May 23, 2025 pm 11:12 PM

使用JavaScript實(shí)現(xiàn)數(shù)據(jù)加密可以使用Crypto-JS庫(kù)。1.安裝并引入Crypto-JS庫(kù)。2.使用AES算法進(jìn)行加密和解密,確保使用相同的密鑰。3.注意密鑰的安全存儲(chǔ)和傳輸,推薦使用CBC模式和環(huán)境變量存儲(chǔ)密鑰。4.在高性能需求時(shí),考慮使用WebWorkers。5.處理非ASCII字符時(shí),需指定編碼方式。

PHP中如何定義構(gòu)造函數(shù)? PHP中如何定義構(gòu)造函數(shù)? May 23, 2025 pm 08:27 PM

在PHP中,構(gòu)造函數(shù)通過(guò)\_\_construct魔術(shù)方法定義。1)在類中定義\_\_construct方法,它會(huì)在對(duì)象實(shí)例化時(shí)自動(dòng)調(diào)用,用于初始化對(duì)象屬性。2)構(gòu)造函數(shù)可以接受任意數(shù)量的參數(shù),靈活初始化對(duì)象。3)在子類中定義構(gòu)造函數(shù)時(shí),需要調(diào)用parent::\_\_construct()確保父類構(gòu)造函數(shù)執(zhí)行。4)通過(guò)可選參數(shù)和條件判斷,構(gòu)造函數(shù)可以模擬重載效果。5)構(gòu)造函數(shù)應(yīng)簡(jiǎn)潔,只做必要初始化,避免復(fù)雜邏輯或I/O操作。

在PhpStudy上部署Joomla網(wǎng)站的詳細(xì)步驟 在PhpStudy上部署Joomla網(wǎng)站的詳細(xì)步驟 May 16, 2025 pm 08:00 PM

在PhpStudy上部署Joomla網(wǎng)站的步驟包括:1)配置PhpStudy,確保Apache和MySQL服務(wù)運(yùn)行并檢查PHP版本兼容性;2)從Joomla官網(wǎng)下載并解壓到PhpStudy的網(wǎng)站根目錄,然后通過(guò)瀏覽器按照安裝向?qū)瓿砂惭b;3)進(jìn)行基本配置,如設(shè)置網(wǎng)站名稱和添加內(nèi)容。

PHP依賴注入:好處和例子 PHP依賴注入:好處和例子 May 17, 2025 am 12:14 AM

使用依賴注入(DI)在PHP中的好處包括:1.解耦,使代碼更模塊化;2.提高可測(cè)試性,易于使用Mocks或Stubs;3.增加靈活性,方便更換依賴;4.提升可重用性,類可在不同環(huán)境中使用。通過(guò)將依賴從外部傳遞給對(duì)象,DI使代碼更易維護(hù)和擴(kuò)展。

PHP電子郵件教程:發(fā)送電子郵件很容易 PHP電子郵件教程:發(fā)送電子郵件很容易 May 19, 2025 am 12:10 AM

sendingemailswithphpisstraightforwardusingthemail()functionormoreadvancedLibrariesLikeLikePhpMailer.1)usemail()forbasicemails,settrecipients,settrecipients,subjects,message,messages,andheaders.2)forhtmlemails,juspeStheadeStheadeSteStheadeSteStospeSpepeSpepeSpepeSpepeCifyHtmlConteN.3)

See all articles