• En
  • /
  • Jp
  • MVC Framework: Cake PHP Version 3

    2017.7.3
    • Share
    • Twitter
    • Google+
    • はてなブックマーク
    introduction

    This article is a continuation of the previous blog that I published titled “Model-View-Controller (MVC Framework)”. For those who has not read it yet please refer to this link: http://devfun-lab.com/1061 that contains the introduction of MVC, what is MVC, where and when can we use MVC, and a brief introduction on what are the sample language and framework that uses MVC.

    In this article, we will be focusing on the sample languages and framework where it uses MVC. For this month, our topic will be MVC Framework: Cake PHP version 3. This article is more of a hands-on activity where you can follow and build a basic cake PHP app. Credits to “Traversy Media” on YouTube as it is where I based my basic understanding in cake PHP as they gave a very good example on the app which I will showing today.

    Contents:


    Today I will be introducing the topic of the Model Control View framework (MVC) framework. In this short research paper, we will be discussing the following items:

    Why Cake PHP?

    • Model, view, controller architecture.
    • Application scaffolding
    • Code generation via bake
    • Free & open source
    • Access control list and authentication
    • Validation model data
    • Router for mapping url
    • Built in email, cookies, security, session, HTML Helpers

    Pre-requisite before learning Cake PHP Framework.

    Like in all development tools, before using a framework, one must learn the basic of the tool. For this instance, before learning Cake PHP, one must first learn the Native PHP.

    Install XAMP/WAMP and composer

    Installing Cake PHP

  • Run cmd on htdocs folder and run the create script
  • php composer.phar create-project –prefer-dist cakephp/app [my_app_name]
  • Successful installation
    Usual error:
    Reason extension not set
    Fix by Changing Php.ini From: ;extension=php_intl.dll
    To: extension=php_intl.dll

    Set up DB connection

    Open config>app.php
    Database Username Username access credential that has access to the Database.
    Database Password Password for the Username that has been used.
    Database Name Name of the database where Cake PHP will connect to.
    Open config>Successful connection

    Cake Bake

    • Bake command for cake creates the scaffolding for MVC
    • To run Bake Command, Go to cake project root folder and run cmd prompt.
    • Bin/cake bake all [table Name]

    Folder Structure

    Model Responsible for single entity myLogin>>src>>Model>>Entity
    Responsible for whole table myLogin>>src>>Model>>TabLe
    Controller Connects the View to the Model myLogin>>src>>Controller
    View GUI of the Tool myLogin>>src>>Template.

    Key points

    View default layout is in: myLogin>>src>>Template>>Layout>>Default.ctp
    Redirection based on return $this->redirect([‘controller’ => ‘controller that will be used’ , ‘action’ => ‘index’])
    If there is no [‘controller’ ], cake will assume that you are using the current controller you are in: $this->redirect(‘action’ => ‘index’])
    If there is no [ ‘action’], cake will assume that you are using the index function $this->redirect([‘controller’ => ‘controller that will be used’])
    Usually used for redirection Html helper Html->link(‘[link label]’, [‘controller’ => ‘controller that will be used’, ‘action’ => ‘function in the controller’]) ?>
    Html->link(‘Logout’, [‘controller’ => ‘users’, ‘action’ => ‘logout’]) ?>

    Data Manipulation Pull to from Model to Controller

    Pull all $users = $this->Users->find()->all();
    Pull with where $users = $this->Users->find()->where([‘name’ => ‘rey’])->all();
    Pull with multiple where $users = $this->Users->find()->where([ ‘name’ => ‘rey’, ‘email’ => ‘norbert@gmail.com’] ) ‘])->all();

    Data Manipulation Pull to from Controller to View

    Pull with multiple where – Set data after pulling – In Controller $this->set(compact(‘users’));
    Using different variable name in front end – In Controller $this->set(‘newUsers’, $users);

    Data Manipulation Pull to from View to Controller

    Create from with input Form->input(‘name’);?> Form->submit(‘Get Data’, array(‘class’ => ‘button’));?>
    On controller make a data request if($this->request->is(‘post’)){ $data = $this->request->data; }

    Reference:

    For Questions and Clarification, you can refer to the reference below