Discussion on CakePHP 3.0
Welcome to FindNerd. Today our discussion is based on cakePHP. We all knows CakePHP has been launched its version with bulk of changes. If you check CakePHP3 then you can see lots of changes, changes in concept, file structure and added new concepts. If we talk about the CakePHP then we will get the same answer from all of you. CakePHP is nothing but a framework to build the web development projects simply and easily. It provides us the toolbox to fulfill our web development requirements. We are going to discuss the basic concept behind this wonderful framework.
CakePHP provides a way in which development work will be done. It uses different table names, class names, file names and many more. CakePHP reduces our effort in web development. Multiple developer can work on single application due to organized structure. CakePHP provides different layer structure so we will discuss these one by one.
Model Layer
Model layer deals with database and handles the data for retrieving it as well convert it into meaningful way. It is working part which process the business logic. Model layer includes different operations such as association, validation, processing and data handling as well. You can write the code for model like this
namespace App\Model\Entity;
use Cake\Auth\DefaultPasswordHasher;
use Cake\ORM\Entity;
class User extends Entity
{
/*
* @description : To generate hash for passwords
*/
protected function _setPassword($password)
{
return (new DefaultPasswordHasher)->hash($password);
}
}
In above code We created User model and made a function to return a hash password.
Controller Layer
It is also known as request handler which handles the request from incoming users. It uses the view and Model Layers to render the output. Controller works as manager to ensure that all needed resources are assigned to correct workers. It holds on to process the model and to select the presentation data for operation.
In the end it assigned the rendering process to view layer. You can check the example below.
namespace App\Controller;
use App\Controller\AppController;
/**
* Users Controller
*/
class UsersController extends AppController
{
public function register_user()
{
$user = $this->Users->newEntity();
if($this->request->is('post'))
{
$user = $this->Users->patchEntity($user, $this->request->data);
if($this->Users->save($user, ['validate' => 'registration'])) {
$this->Flash->success(__('You has been registered successfully.'));
}
else
{
$this->Flash->error(__('Some Wrong.'));
}
}
$this->set('user', $user);
}
}
In above example we created a controller Users and inside it we included register_user function which is useful to register a new user. We set the variable for the view layer using set() function.
View Layer
View layer renders the model generated data on the page. There are many other extensions which are provided by view layer such as elements, view cells and view templates. You can see the example below in which we are rendering data using $user variable which we set inside the controller.
<?php
foreach($users as $user){
?>
<li class="details">
<?php
$this->element('user', ['user' => $user])
?>
</li>
<?php
}
?>
0 Comment(s)