Internationalization & Localization Application with CakePHP 3

CakePHP has one of the coolest features to make application Internationalization & Localization. It’s really easy to translate your application or website into multiple languages with CakePHP 3.  Let’s see it.

First of all, create a directory Locale inside PROJECT_ROOT/src

Create a subdirectory of each language (which language you want to translate your application) inside PROJECT_ROOT/src/Locale/. Subdirectory name can be anything (ex. english, french, etc), but prefer ISO code of the language (ex. en_US, fr_FR, bn_BN etc). So follow this tutorial and make your subdirectory name en_USfr_FR, and bn_BN

Create default.po file inside all subdirectories (en_USfr_FR, and bn_BN). And copy the following code in the respective file.

src/Locale/en_US/default.po

msgid "message"
msgstr "Hello World"

 

src/Locale/fr_FR/default.po

msgid "message"
msgstr "Bonjour le monde"

 

src/Locale/bn_BN/default.po

msgid "message"
msgstr "ওহে বিশ্ব"

Here msgid key of which will use inside the template (view files) and msgstr is the values which will gonna translate.

So now you have three different languages (en_USfr_FR, and bn_BN). and one key (message) which can be translated into these three languages.

Now use <?php echo __(‘message’); ?> in your view (any ctp file of your application).

Then open PROJECT_ROOT/config/app.php file and find

 'defaultLocale' => env('APP_DEFAULT_LOCALE', 'fr_FR')

and replace with your language.

But if you want to translate language in runtime then you can use it

use Cake\I18n\I18n;

I18n::locale('fr_FR');

I hope this article will help you to translate your application in multiple language.

CakePHP 3 – DebugKit Toolbar Not Visible

Are you looking for the solution of DebugKit Toolbar not visible in CakePHP 3? These small tips will help you do that. Ensure you have pdo-sqlite installed & enabled on your machine. Cause debugKit by default uses a sqlite db. If not then follow this instruction.

At first install pdo-sqlite in your machine.

sudo apt-get install php7.0-sqlite

Then restart your server (apache server)

sudo service apache2 restart

Now that will help you to visible the DebugKit Toolbar

 

Configure Email Transporter in CakePHP Runtime

If you are looking for a solution to configure email transporter from CakePHP controller, components in run-time. This script will help you lot. This script is working for CakePHP 3.1.X – 3.3.9.

Email::configTransport('transporter_name', [
            'className' => 'Smtp',
            'host' => 'SMTP_HOST',
            'port' => 'SMTP_PORT',
            'timeout' => 30,
            'username' => 'SMTP_USERNAME',
            'password' => 'SMTP_PASSWORD',
        ]);
        
        $email = new Email();
        $email->transport('transporter_name');
        
        $email->from('no-replay@example.com')
            ->to('to@example.com')
            ->subject('Dummy Subject')
            ->message('This is a test message')
            ->send();

Simply copy paste this snippet and modified with real information and test it.

Loading Model Inside Another Model and Controller in CakePHP 3

Sometimes it necessary to load a model inside another model as well as another controller in CakePHP. This article will help you to load a model from another model & controller with CakePHP 3

Load From Model:
$anotherTable = TableRegistry::get('AnotherTable');
$data = $anotherTable->find('list');

CakePHP TableRegistry is a factory of the Table object. It comes with a lot of static function and get() one of them. TableRegistry::get() provides an instance of Table. You can see an example from above snippet.

Load From Controller:
$this->loadModel('AnotherModel');
$data = $this->AnotherModel->find('list');

If you need to load a model from the controller which does not default model of the controller. You can use loadModel() function to get an instance of another model.

Password and Confirm Password Validation in CakePHP 3.1

When we are working with signup/registration, adding user, update profile, etc in web application then we need to validate password and confirm password fields. Here I showing you how to validate password and confirm password fields  in CakePHP 3.1

 

Here I am using custom validation of CakePHP. CakePHP allowed us to write our own custom validation rules by using anonymous/callback function. Hope this will he helpful to you.

How to develop your first page in CakePHP

Before you read this tutorial you have to know how to install CakePHP. If you do not know how to install CakePHP, you should read install CakePHP . To create your first page in CakePHP you have do couple of things, I an gonna show you.

Step 1 : Go to app/config folder and open routes.php file. You will see this code

<?php
    Router::connect('/', array('controller' => 'pages', 'action' => 'display', 'home'));

?>

You have to rename your controller name and action . (e.g your controller name sites and action name index).

<?php
    Router::connect('/', array('controller' => 'sites', 'action' => 'index', 'home'));

?>

That means you select your application home page. That means your application will be opened with SitesController’s index file.

Step 2 : Now you need to create a controller named like “SitesController.php” in your app/controller folder and you have to write following codes .

<?php
/**
* Created Sohel Rana
* User:  Rana
* Date: 2/20/13
*/
class  SitesController extends AppController{
    var $name = 'Sites';
    public function index(){
    }
}
?>

Step 3 : Your Controller has been ready now you need to create “View“ file. Go to app/View and create a folder with the same name of your Controller (e.g Sites).

Step 4 : You have created Site folder and now you have to create index.ctp file and write this code.

<h2>Welcome Page</h2>

Refresh you browser and see the result.

How to Install CakePHP

CakePHP is the most simpler, faster, powerful open source framework. it is written by PHP (Object Oriented)” . It’s follow the MVC (Model View Controller) pattern. Which is most interactive part of it. You can easily build your application require less coding. Let’s I am going to introduce how to Install CakePHP with some step.

Step 1 : Download latest version of CakePHP from cakephp.org and extract the downloaded file.

Step 2 : If you downloaded 2.2.3 then you will see cakephp-2.2.3 >> cakephp-2.2.3 this folder structure. You need to rename the second cakephp-2.2.3 with your desire name like cake.

Step 3 : Copy the folder and paste into your htdocs folder (your local server). and type in your browser “http://localhost/cake” (If you rename with cake name, otherwise write http://localhost/your_desire_name). Then you will see something like this image.

cake_install

Step 4 : After that you need to change the “Security Salt” and ”Security ChiperSeed”. To that go to app/config and open the core.php and you will see this code.

* A random string used in security hashing methods.
*/
    Configure::write('Security.salt', 'DYhG93b0qyJfIxfs2guVoUubWwvniR2G0FgaC9mi');

/**
* A random numeric string (digits only) used to encrypt/decrypt strings.
*/
    Configure::write('Security.cipherSeed', '76859309657453542496749683645');

You need to change those security.salt and security.CiperSeed. Like this

* A random string used in security hashing methods.
*/
    Configure::write('Security.salt', 'fesfsdfsdfsesfdlkfmma;lfm,as,mfcesR2G0FgaC9mi');

/**
* A random numeric string (digits only) used to encrypt/decrypt strings.
*/
    Configure::write('Security.cipherSeed', '7685645641654165416546549309657453542496749683645');

Step 5:  After that rename the file dsatbase.php.default to database.php . For doing that you have to go app/config and you will see the database.php.default

Step 6 : Then create a database and then open database.php file and you will see this code.

public $default = array(
        'datasource' => 'Database/Mysql',
        'persistent' => false,
        'host' => 'localhost',
        'login' => 'user',
        'password' => 'password',
        'database' => 'database_name',
        'prefix' => '',
        //'encoding' => 'utf8',
    );

change the host,login,password,database name with your host,user, password,database name.

Step 7: That’s fine, you have done all things. Now refresh you browser and see you browser.