Monday, April 27, 2015

Validate Email using Validator of Cakephp3

Let's assume you need to validate an email that is not a part of model Entity. If it was a field in model, the validation rules in model would have taken care of it. But in this case, you need to validate the email address yourself. So, what will you use? A regex ? No, instead you can use the Validation class of Cake.

So, How do we validate an email in cakephp3 using it's validator?

It's simple :

First insert the below line in your controller or Model :

use Cake\Validation\Validation;

This will include the validation class in your file.

Now use the below line in your function :

$check=Validation::email('email to be validated');

If $check is true, then it's a valid email else the email is invalid.




Thursday, April 2, 2015

Install Cakephp 3 on Ubuntu

Stable version of cake3 is released for use. Old way of downloading cake and placing it in root directory and making changes to it, to get the app working won't work anymore. We have to install cakephp using composer. I tried installing it on my machine and felt like sharing the steps that I followed to install. So, here it goes

Following are the requirements for installing cakephp3 :
1. HTTP Server like Apache
2. PHP version greater than or equal to 5.4.16
3. mbstring extension
4. intl extension
5. Composer, without which you cannot install cakephp3

Installing Requirements 

Go to Terminal,

Install apache and php >= 5.4.16.

Install mbstring extension using the following command :
sudo apt-get install libapache2-mod-php5

Install intl extension using the following command :
sudo apt-get install php5-intl

Now restart apache using the command :
service apache2 restart

Install Composer :
Once all Apache,PHP, mbstring and intl extension are installed. Next step is to install composer.
Go to your root directory( /var/www/ in ubuntu ) cd /var/www
If you have curl installed you can use command curl -s https://getcomposer.org/installer | php
else you can use command php -r "readfile('https://getcomposer.org/installer');" | php
to install composer.
Note, commands above will install composer only in the directory where you ran the command.

Now we are ready to install cakephp3
Go to document root directory( /var/www/ in ubuntu ) cd /var/www
Now type the command below :
php composer.phar create-project --prefer-dist cakephp/app [app_name]
Your cakephp3 application will now be running.

 Connecting to database :
 In cakephp3 we give database connection parameters in app.php in folder <app-name>/config
 Go to  <app-name>/config/app.php and change the requires parameters mentioned below
   'Datasources' => [
         'default' => [
             'username' => '<give your db username here>',
              'password' => '<give your db passwoprd here>',
              'database' => '<give your db name here>',


 Type http://localhost/app_name in browser to see your cakephp3 app.

Issues that you might face when you type command
php composer.phar create-project --prefer-dist cakephp/app [app_name]

1. Could not open input file: composer.phar
    This means you haven't installed composer globally, and your composer installation was directory specific. Go to the directory where you have installed the composer and issue the above command.

2. This package requires php >=5.4.16 but your PHP version does not satisfy that requirement.
    You will get this error when you are trying to install cakephp3 on php version lesser than 5.4.16.
    PHP version of your machine has to be upgraded to version 5.4.16 or greater.
    Note : Restart apache after  the upgrade.

3. cakephp/cakephp 3.0.x-<dev/beta/alpha/RC> requires ext-intl * -> the requested PHP extension intl is missing from your system. 
   This means intl extension for php was not installed. You need to install intl extension as mentioned     above, using the command sudo apt-get install php5-intl .
    Note : Restart apache after installing.

Issues that you might face after installing 

1. Database driver Cake\Database\Driver\Mysql cannot be used due to a missing PHP 
    extension or unmet dependency
    The above error will be displayed on browser, if Mysql database driver couldn't be used by app or
     is missing. Type the following command in terminal :
     sudo apt-get install php5-mysql
     Now restart mysql using command service mysql restart then restart Apache using command
     service apache2 restart
     Refresh the browser now, the above error would have disappeared.





Monday, March 11, 2013

Find date of last day of a month and number of days of a month in PHP


To find date of last day of a month you can use the below line :

  date("j-n-Y", mktime(0, 0, 0, date("m")+1 , date("d")-date("d"), date("Y"))); 

For example, if you have to find last day of Febraury 2012, code goes as below :

 $month='02';
  $year='2012';
  $dtLastDay = date("j-n-Y", mktime(0, 0, 0, $month+1 , date("d")-date("d"),     
   $year));

$dtLastDay will have 29-2-2012.


To calculate number of days in a month, syntax is as below :

  $num = cal_days_in_month(CAL_GREGORIAN, month, year) ; 

For example, if you want number of days in Feb 2012, you can change the above syntax as the follwing one 
  
  $year='2012';
  $num = cal_days_in_month(CAL_GREGORIAN, $month, $year) ; 

 $num will have no. of days ie., 29 in this case





Sunday, February 17, 2013

Install Mysql On Ubuntu


Installing Mysql on Ubuntu is a very simple task :
  
  Open the terminal and type the following command
        sudo apt-get install mysql-server

  During installation you will be asked to provide password for Mysql root user.

  You can check whether Mysql is running by using the following command :
       sudo /etc/init.d/mysql status

  If Mysql is not running, then issue the following command
          sudo /etc/init.d/mysql start

   
 

Sunday, June 19, 2011

Installing Eclipse Galileo , PHPEclipse plugin and SVN plugin

Steps to Install Eclipse Galileo on Ubuntu , PHPEclipse and SVN plugin  :

Start with Eclipse Installation first
 
   Login as root user
    
   Update the package repository by the following command :
         apt-get update (or) aptitude update
     
   To install eclipse use the following command :
         aptitude install eclipse
 
   This will complete the Eclipse Installation.

Now, Install PHPEclipse and SVN plugins
 
   Go to Applications->Programming->Eclipse

   In Eclipse menubar, select Menu->Help->Install New Softwares

   Click on Add, and insert the follwing:
   
   Installing PHPEclipse plugin
 
     Name: PHPEclipse

     URL: http://phpeclipse.sourceforge.net/update/nightly_1.2.x/
  
     If the above Url doesn't work, then use the URL below
      http://phpeclipse.sourceforge.net/update/stable/1.2.x/

     Now click on Next, select the items to be installed and Finish.

     After Installing PHPEclipse, restart your eclipse to view the changes.

   Installing SVN plugin 

    Name: Subversion

     URL: http://subclipse.tigris.org/update_1.4.x

     Now click on Next, select the items to be installed and Finish.

     After Installing Subversion, restart your eclipse to view the changes.
 

Saturday, June 18, 2011

Installing CAKEPHP on your ubuntu machine

Hi Everyone,
 
         I'm listing out the steps to install cakephp :

 1. Install the necessary packages using the command
       sudo apt-get install apache2 mysql-server php5

 2. In case you encounter any Lock Error, make sure no other installation is
     happening and close Synaptic Package Manager if open. Even if the problem
     exists, execute sudo killall apt-get

 3. Enable Mod-rewrite: sudo a2enmod rewrite

 4. Download the latest stable version of cake from http://cakephp.org/ and save
     it to your machine.

 5. Extract the downloaded file and rename the extracted folder with your project
     name(assume, project name is cakephp) and move it to the document root
     (most of the times it will be /var/www/)


 6. Give writable permission to tmp folder:
     sudo chmod -R 777 cakephp/app/tmp

 7.  Open file /etc/apache2/sites-enabled/000-default  using command
       sudo vim /etc/apache2/sites-enabled/000-default
       and change AllowOverride None to AllowOverride All.
     i.e, change the following content 
     <Directory /var/www/>
        Options Indexes FollowSymLinks MultiViews
        AllowOverride None
        Order allow,deny
        allow from all
     </Directory>
                            TO
     <Directory /var/www/>
        Options Indexes FollowSymLinks MultiViews
        AllowOverride All
        Order allow,deny
        allow from all
     </Directory>
 
 8. Create .htaccess file inside the project directory
    sudo vim.tiny /var/www/cakephp/.htaccess
     and add these contents:
     RewriteEngine on
    RewriteRule    ^$ app/webroot/    [L]
    RewriteRule    (.*) app/webroot/$1 [L]


 9. Restart Apache: sudo /etc/init.d/apache2 restart
 Cakephp installation is now complete. Open http://localhost/cakephp from
 browser.

 If in case, you have an error 
 " Your database configuration file is NOT present" after installing
  then execute the following command : 
  sudo aptitude install php5-mysql
  and restart the apache service.

 Guided by Arjun Urs(http://arjunurs.blogspot.com/)

Introduction to CAKEPHP and MVC architecture

CakePHP is a rapid development framework for PHP that provides an extensible architecture for developing, maintaining, and deploying applications. It uses MVC software design pattern.

MVC design pattern divides the application into three main parts :
Model : The model represents the database table.
Controller : It is used for writing logic for your application. All your business logic needs to be put into controller.

View : It is used to present the data of your application to end user.


Understanding MVC architecture :
         Whenever a user accesses the MVC application the dispatcher hands over the request to controller, controller performs the required operation with model(data in database table) and sends the request to view where the data is presented for the user.

Refer : http://book.cakephp.org/view/880/What-is-CakePHP-Why-Use-it#!/view/890/Understanding-Model-View-Controller