Tag Archives: Cake Php

Quick Start With Cake PHP 3

I. Install IDE for PHP

  • Download PHPStorm 10.0.3 – Key PhpStorm-Key
  • Keymap Scheme : Eclipse
  • IDE Theme : Dracula
  • Colors and Fonts : Dracula
  • You cant edit keymap follow : File -> Settings -> Keymap -> change what you want.

Display *.ctp as PHP file :

filetypes
II. Install Composer on Window via Composer-Setup.exe

III. Create new project using Composer

Edit file xampp/php/php.ini and remove “;” at extension=php_intl.dll

Right Click -> Use Composer Here

IV. Command using in composer

  • Remove dependencies : composer remove facebook/php-sdk-v4 –update-with-dependencies
  • Create project

  • Generate add,edit,delete,view sites of table in database

V. Using template HTML/CSS/JS

  • In AppController, function beforeRender we need set template before open site.

template

  • In template.ctp only keep header and footer. We must replace contain follow code:
  • Change link CSS and JS, Image follow code:

VI. Multi-language

  • In Cake3, We using I18n Shell to translate language
  • Link http://book.cakephp.org/3.0/en/core-libraries/internationalization-and-localization.html

VII. Admin Plugin and Routing

  • In config/router.php

  • Create folder Admin in /src/Controller and Admin in /src/Template. .
  • Copy AppController and TestController into folder /src/Controller/Admin. Remember change namespace to App\Controller\Admin.
  • Create folder Test in /src/Template/Admin. Create index.ctp in folder Test. Content of file index.ctp : <h1> Hi index </h1>.

admin
VIII. Associations (Relationship in Cake)

Link : http://book.cakephp.org/3.0/en/orm/associations.html

  • In parent table , function init :

  • In child table, function init:

  • How to get list child from parent

IX. jQuery/Javascript to replace broken images

Handle the

event for the image to reassign its source using JavaScript:

Add event on img tag

Or without a JavaScript function:

The following compatibility table lists the browsers that support the error facility:

http://www.quirksmode.org/dom/events/error.html

X. Ajax function cannot write Cookie

First thing, you should enabled debug mode in your application (

), or check the debug logs, then you’d see that you are getting a “Cannot modify header information – headers already sent” warning.

Do not manually output data using or the like

Except for session cookies which are being sent immediately when a session is started, normal cookies are queued in the

object (

in your controller) until after the controller action has been executed, so doing an

in your controller action will cause headers and data to be sent, and therefore the cookie headers cannot be sent anymore.

Use a view or the object instead

 

using reCaptcha in cake php

  1. Go to https://www.google.com/recaptcha/admin#list register your website and get key.
  2. Client-side integration
    Paste this snippet before the closing </head> tag on your HTML template:

    Paste this snippet at the end of the <form> where you want the reCAPTCHA widget to appear:
  3. Server side integration
    When your users submit the form where you integrated reCAPTCHA, you’ll get as part of the payload a string with the name “g-recaptcha-response”. In order to check whether Google has verified that user, send a POST request with these parameters:

    URL: https://www.google.com/recaptcha/api/siteverify
    secret (required) your secret key
    response (required) The value of ‘g-recaptcha-response’.
    remoteip The end user’s ip address.
  4. Controller

     

Sent mail in cake php.

  • Config sent mail in app/Config/email.php

  • In Layouts/Email/html and Layouts/Email/text >> Create contact_mail.ctp.

  •  In Controller.

Some experience in CakePhp.

1. before load page

 2. remove cache

Sometime project will remember cache so we must remove it.

Change debug 0 -> 2 after refesh page change again 2->0

In file core.php

 3. when use ajax and don’t want redirect url, we set render = false

 4. example ajax in cakephp

  • In controller.php .

  •  In jquery .

 5. explode array in php.

 6. sub string.