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 shttps://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.

Advertisements

Removing unnecessary nested index in array (generated by Cake PHP)

 

$a = array(
    array(
        'User' => array(
            'id' => 2,
            'group_id' => 1,
            'Data' => array(
                'user' => 'mariano.iglesias',
                'name' => 'Mariano Iglesias'
            )
        )
    ),
    array(
        'User' => array(
            'id' => 14,
            'group_id' => 2,
            'Data' => array(
                'user' => 'phpnut',
                'name' => 'Larry E. Masters'
            )
        )
    ),
    array(
        'User' => array(
            'id' => 25,
            'group_id' => 1,
            'Data' => array(
                'user' => 'gwoo',
                'name' => 'The Gwoo'
            )
        )
    )
);

RUN THIS TO REMOVE THE MODEL NAME USER

Set::extract($a, '{n}.User');

WILL RETURN THIS

$a = array(
        array(

                'id' => 2,
                'group_id' => 1,
                'Data' => array(
                    'user' => 'mariano.iglesias',
                    'name' => 'Mariano Iglesias'
                )

        ),
        array(

                'id' => 14,
                'group_id' => 2,
                'Data' => array(
                    'user' => 'phpnut',
                    'name' => 'Larry E. Masters'
                )

        ),
        array(

                'id' => 25,
                'group_id' => 1,
                'Data' => array(
                    'user' => 'gwoo',
                    'name' => 'The Gwoo'
                )

        )
    );
 

recursive in CakePHP

The recursive property defines how deep CakePHP should go to fetch associated model data via find(), and read() methods.

Imagine your application features Groups which belong to a domain and have many Users which in turn have many Articles. You can set $recursive to different values based on the amount of data you want back from a $this->Group->find() call:

  • -1 CakePHP fetches Group data only, no joins.
  • 0 CakePHP fetches Group data and its domain
  • 1 CakePHP fetches a Group, its domain and its associated Users
  • 2 CakePHP fetches a Group, its domain, its associated Users, and the Users’ associated Articles

Ajax request in cakephp 2.x

show.ctp

<?php echo $this->Html->script('jquery', FALSE); ?>

<?php

echo $this->Form->create('Count', array('action'=>'ajaxShow'));
echo $this->Form->input('field', array('id'=>'field'));
echo $this->Js->submit('Send', array(
'before'=>$this->Js->get('#sending')->effect('fadeIn'),
'success'=>$this->Js->get('#sending')->effect('fadeOut'),
'update'=>'#success'
));

?>

<div id='sending' style="display:none"> Counz[dot]gif will be displayed </div>
<div id='success'></div>

controller

var $name = 'count';

    public $helpers = array('Js' => array('Jquery'));
    //var $helpers = array('Ajax', 'Javascript');
    var $components = array('RequestHandler');
public function ajaxShow(){
         $this->layout = 'ajax'; 
        if($this->RequestHandler->isAjax()){  
            $this->render('success', 'ajax');
        }else{
            $this->set('for_map', $this->count->find('all'));

        }
    }

success.ctp

<p>It's working</p>

Controller Attributes in CakePHP

property Controller::$name

The $name attribute should be set to the name of the controller. Usually this is just the plural form
of the primary model the controller uses.

$components, $helpers and $uses

The next most often used controller attributes tell CakePHP what $helpers, $components, and
models you’ll be using in conjunction with the current controller. Using these attributes make
MVC classes given by $components and $uses available to the controller as class variables
($this->ModelName, for example) and those given by $helpers to the view as an object reference
variable ($this->{$helpername}).

property Controller::$uses
Controllers have access to their primary model available by default. Our RecipesController will have
the Recipe model class available at $this->Recipe, and our ProductsController also features the
Product model at $this->Product. However, when allowing a controller to access additional
models through the $uses variable, the name of the current controller’s model must also be included.
This is illustrated in the example below.
If you do not wish to use a Model in your controller, set public $uses = array(). This will
allow you to use a controller without a need for a corresponding Model file. However, the models
defined in the AppController will still be loaded. You can also use false to not load any
models at all. Even those defined in the AppController.

property Controller::$helpers
The HtmlHelper, FormHelper, and SessionHelper are available by default, as is the SessionComponent. But if you choose to define your own $helpers array in AppController, make sure to include HtmlHelper and FormHelper if you want them still
available by default in your Controllers. To learn more about these classes, be sure to check out their
respective sections later in this manual.

property Controller::$components
The components array allows you to set which Components a controller will use. Like $helpers
and $uses components in your controllers are merged with those in AppController. As with
$helpers you can pass settings into $components. See Configuring Components for more infor-
mation.

Other Attributes
While you can check out the details for all controller attributes in the API3 , there are other controller at-
tributes that merit their own sections in the manual.
property Controller::$cacheAction
The cacheAction attribute is used to define the duration and other information about full page caching.
You can read more about full page caching in the CacheHelper documentation.
property Controller::$paginate
The paginate attribute is a deprecated compatibility property. Using it loads and configures the
PaginatorComponent. It is recommended that you update your code to use normal component

Request Life-cycle callbacks in CakePHP

CakePHP controllers come fitted with callbacks you can use to insert logic around the request life-cycle:
Controller::beforeFilter()
This function is executed before every action in the controller. It’s a handy place to check for an active
session or inspect user permissions.
Note: The beforeFilter() method will be called for missing actions, and scaffolded actions.
Controller::beforeRender()
Called after controller action logic, but before the view is rendered. This callback is not used often,
but may be needed if you are calling render() manually before the end of a given action.
Controller::afterFilter()
Called after every controller action, and after rendering is complete. This is the last controller method
to run.
In addition to controller life-cycle callbacks, Components also provide a similar set of callbacks.

common tasks people learning CakePHP

These are common tasks people learning CakePHP usually want to study :
1. Layouts: Customizing your website layout
2. Elements: Including and reusing view snippets
3. Scaffolding: Prototyping before creating code
4. Code Generation with Bake: Generating basic CRUD code
5. Simple Authentication and Authorization Application: User authentication and authorization tutorial