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

目錄
驗(yàn)證資料
範(fàn)例
輸出
Http - 客戶端
HTTP GET 方法
HTTP POST 方法
首頁 後端開發(fā) php教程 CakePHP 建立驗(yàn)證器

CakePHP 建立驗(yàn)證器

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

可以透過在控制器中新增以下兩行來建立驗(yàn)證器。

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

驗(yàn)證資料

一旦我們建立了驗(yàn)證器,我們就可以使用驗(yàn)證器物件來驗(yàn)證資料。以下程式碼說明了我們?nèi)绾悟?yàn)證登入網(wǎng)頁的資料。

$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 對象,我們首先呼叫 notEmpty() 方法,這將確保用戶名不能為空。之後,我們連結(jié)了 add() 方法來新增一個(gè)正確的電子郵件格式驗(yàn)證。

之後,我們使用 notEmpty() 方法新增了對密碼欄位的驗(yàn)證,這將確認(rèn)密碼欄位不能為空。

範(fà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 建立一個(gè) ValidsController.php 檔案。 將以下程式碼複製到控制器檔案中。

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 處建立一個(gè)目錄 Valids 並在該目錄下建立一個(gè) View 文件,名稱為 index.php。 複製以下程式碼位於該檔案中。

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();
?>

透過造訪以下 URL 執(zhí)行上述範(fàn)例 -

http://localhost/cakephp4/validation

輸出

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

Click PHP

Http - 客戶端

http 用戶端可用於發(fā)出 GET、POST、PUT 等請求

要使用 http 用戶端,請加入以下內(nèi)容 -

use Cake\Http\Client;

讓我們透過範(fàn)例來了解 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í)行以下操作 -

對於普通文字資料?

$response->getBody();

對於Json -

$response->getJson();

對於 Xml ?

$response->getXml()

範(fà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('getData',['controller'=>'Requests','action'=>'index']);
   $builder->fallbacks();
});

src/Controller/RequestsController.php 建立一個(gè) RequestsController.php 檔案。 將以下程式碼複製到控制器檔案中。

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 處建立一個(gè)目錄 Requests 並在該目錄下建立一個(gè) View 文件,名稱為 index.php。 複製以下程式碼位於該檔案中。

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>';
      }
   }
?>

透過造訪以下 URL 執(zhí)行上述範(fàn)例 -

http://localhost/cakephp4/getData

輸出

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

Users URL

HTTP POST 方法

要使用 post,您需要呼叫 $http 用戶端,如下所示 -

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

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

範(fà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('postData',['controller'=>'Requests','action'=>'index']);
   $builder->fallbacks();
});

src/Controller/RequestsController.php 建立 RequestsController.php 檔案。 將以下程式碼複製到控制器檔案中。如果已創(chuà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處建立目錄Requests,並在該目錄下建立一個(gè)名為index.php的View檔案。將以下程式碼複製到該文件中。

src/Template/Requests/index.php

<h3>Testing Post Method</h3>

透過造訪以下 URL 執(zhí)行上述範(fàn)例 -

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 建立驗(yàn)證器的詳細(xì)內(nèi)容。更多資訊請關(guān)注PHP中文網(wǎng)其他相關(guān)文章!

本網(wǎng)站聲明
本文內(nèi)容由網(wǎng)友自願(yuàn)投稿,版權(quán)歸原作者所有。本站不承擔(dān)相應(yīng)的法律責(zé)任。如發(fā)現(xiàn)涉嫌抄襲或侵權(quán)的內(nèi)容,請聯(lián)絡(luò)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脫衣器

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整合開發(fā)環(huán)境

Dreamweaver CS6

Dreamweaver CS6

視覺化網(wǎng)頁開發(fā)工具

SublimeText3 Mac版

SublimeText3 Mac版

神級程式碼編輯軟體(SublimeText3)

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

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

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

社保號驗(yàn)證在PHP中通過正則表達(dá)式和簡單邏輯實(shí)現(xiàn)。 1)使用正則表達(dá)式清理輸入,去除非數(shù)字字符。 2)檢查字符串長度是否為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庫。 1.安裝並引入Crypto-JS庫。 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ù)通過\_\_construct魔術(shù)方法定義。 1)在類中定義\_\_construct方法,它會(huì)在對象實(shí)例化時(shí)自動(dòng)調(diào)用,用於初始化對象屬性。 2)構(gòu)造函數(shù)可以接受任意數(shù)量的參數(shù),靈活初始化對象。 3)在子類中定義構(gòu)造函數(shù)時(shí),需要調(diào)用parent::\_\_construct()確保父類構(gòu)造函數(shù)執(zhí)行。 4)通過可選參數(shù)和條件判斷,構(gòu)造函數(shù)可以模擬重載效果。 5)構(gòu)造函數(shù)應(yīng)簡潔,只做必要初始化,避免複雜邏輯或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)站根目錄,然後通過瀏覽器按照安裝嚮導(dǎo)完成安裝;3)進(jìn)行基本配置,如設(shè)置網(wǎng)站名稱和添加內(nèi)容。

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

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

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

sendingemailswithphpisstraightforwardusingthemail()functionormoreAdvancedLibrariesLikeLikePhpMailer.1)usemail()forbasicemails,settreCipients,settrecipients,subjects,message,messages,messages和headeers.2)forhtmlemails,juspeStheadeStheadeStheadeSteStospeSpepeSpepeSpepeCifyHtmlconteN.3)

See all articles