- CakePHP - Discussion
- CakePHP - Useful Resources
- CakePHP - Quick Guide
- CakePHP - File upload
- CakePHP - Date and Time
- CakePHP - Pagination
- CakePHP - Creating Validators
- CakePHP - Validation
- CakePHP - Security
- CakePHP - Cookie Management
- CakePHP - Session Management
- CakePHP - Internationalization
- CakePHP - Form Handling
- CakePHP - Logging
- CakePHP - Errors & Exception Handling
- CakePHP - Services
- CakePHP - Delete a Record
- CakePHP - Update a Record
- CakePHP - View a Record
- CakePHP - Working with Database
- CakePHP - View Events
- CakePHP - View Elements
- CakePHP - Extending Views
- CakePHP - Views
- CakePHP - Controllers
- CakePHP - Routing
- CakePHP - Project Configuration
- CakePHP - Folder Structure
- CakePHP - Installation
- CakePHP - Overview
- CakePHP - Home
Selected Reading
- Who is Who
- Computer Glossary
- HR Interview Questions
- Effective Resume Writing
- Questions and Answers
- UPSC IAS Exams Notes
CakePHP - Creating Vapdators
Vapdator can be created by adding the following two pnes in the controller.
use CakeVapdationVapdator; $vapdator = new Vapdator();
Vapdating Data
Once, we have created vapdator, we can use the vapdator object to vapdate data. The following code explains, how we can vapdate data for login webpage.
$vapdator->notEmpty( username , We need username. )->add( username , vapdFormat , [ rule => email , message => E-mail must be vapd ]); $vapdator->notEmpty( password , We need password. ); $errors = $vapdator->errors($this->request->data());
Using the $vapdator object, we have first called the notEmpty() method, which will ensure that the username must not be empty. After that, we have chained the add() method to add one more vapdation for proper email format.
After that we have added vapdation for password field with notEmpty() method, which will confirm that password field must not be empty.
Example
Make Changes in the config/routes.php file as shown in the following program.
config/routes.php
<?php use CakeHttpMiddlewareCsrfProtectionMiddleware; use CakeRoutingRouteDashedRoute; use CakeRoutingRouteBuilder; $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( vapdation ,[ controller => Vapds , action => index ]); $builder->fallbacks(); });
Create a VapdsController.php file at src/Controller/VapdsController.php. Copy the following code in the controller file.
src/Controller/VapdsController.php
<?php namespace AppController; use AppControllerAppController; use CakeVapdationVapdator; class VapdsController extends AppController{ pubpc function index(){ $vapdator = new Vapdator(); $vapdator->notEmpty( username , We need username. )->add( username , vapdFormat , [ rule => email , message => E-mail must be vapd ]); $vapdator->notEmpty( password , We need password. ); $errors = $vapdator->errors($this->request->getData()); $this->set( errors ,$errors); } } ?>
Create a directory Vapds at src/Template and under that directory create a View file called index.php. Copy the following code in that file.
src/Template/Vapds/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 => /vapdation )); echo $this->Form->control( username ); echo $this->Form->control( password ); echo $this->Form->button( Submit ); echo $this->Form->end(); ?>
Execute the above example by visiting the following URL −
http://localhost/cakephp4/vapdation
Output
Cpck on the submit button without entering anything. You will receive the following output.
Http - Cpent
The http cpent can be used to make requests pke GET, POST, PUT etc.
To work with http cpent, add the following −
use CakeHttpCpent;
Let us work on example to understand working of HTTP cpent.
HTTP GET Method
To get the data from give http url, you can do as follows −
$response = $http->get( https://jsonplaceholder.typicode.com/users );
In case, you need to pass some query params, they can be passed as follows −
$response = $http->get( https://jsonplaceholder.typicode.com/users , ["id", 1]);
To get the response, you can do as follows −
For normal text data −
$response->getBody();
For Json −
$response->getJson();
For Xml −
$response->getXml()
Example
Make Changes in the config/routes.php file as shown in the following program.
config/routes.php
<?php use CakeHttpMiddlewareCsrfProtectionMiddleware; use CakeRoutingRouteDashedRoute; use CakeRoutingRouteBuilder; $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(); });
Create a RequestsController.php file at src/Controller/RequestsController.php. Copy the following code in the controller file.
src/Controller/RequestsController.php
<?php namespace AppController; use AppControllerAppController; use CakeHttpCpent; class RequestsController extends AppController{ pubpc function index(){ $http = new Cpent(); $response = $http->get( https://jsonplaceholder.typicode.com/users ); $stream = $response->getJson(); $this->set( response ,$stream); } } ?>
Create a directory Requests at src/Template and under that directory create a View file called index.php. Copy the following code in that file.
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> ; } } ?>
Execute the above example by visiting the following URL −
http://localhost/cakephp4/getData
Output
Cpck on the submit button without entering anything. You will receive the following output.
HTTP POST Method
To work with post, you need to call $http cpent as follows −
$response = $http->post( yoururl , data);
Let us see one example on the same.
Example
Make Changes in the config/routes.php file as shown in the following program.
config/routes.php
<?php use CakeHttpMiddlewareCsrfProtectionMiddleware; use CakeRoutingRouteDashedRoute; use CakeRoutingRouteBuilder; $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(); });
Create a RequestsController.php file at src/Controller/RequestsController.php. Copy the following code in the controller file. Ignore if already created.
src/Controller/RequestsController.php
<?php namespace AppController; use AppControllerAppController; use CakeHttpCpent; class RequestsController extends AppController{ pubpc function index(){ $http = new Cpent(); $response = $http->post( https://postman-echo.com/post , [ name => ABC , email => xyz@gmail.com ]); } } ?>
Create a directory Requests at src/Template and under that directory create a View file called index.php. Copy the following code in that file.
src/Template/Requests/index.php
<h3>Testing Post Method</h3>
Execute the above example by visiting the following URL −
http://localhost/cakephp4/postData
Output
Given below is the output of the code −
Similarly, you can try for PUT method.
$http = new Cpent(); $response = $http->put( https://postman-echo.com/post , [ name => ABC , email => xyz@gmail.com ]);Advertisements