Category Archives: Zend Frameworks

Zend Framework2: Tutorial Part4: Building a CRUD app by using Table Gateway2

 Here you have your form fold, the file stuction should look like this:

crud4

here is the code for userForm.php

<?php
namespace CsnUser\Form;

use Zend\Form\Form;

class UserForm extends Form
{
public function __construct($name = null)
{
parent::__construct(‘user’);
$this->setAttribute(‘method’, ‘post’);

$this->add(array(
‘name’ =>’usr_name’,
‘attributes’=> array(
‘type’=>’text’,
),
‘options’=>array(
‘label’=>’Username’,
),
));

$this->add(array(
‘name’ =>’usr_password’,
‘attributes’=> array(
‘type’=>’password’,
),
‘options’=>array(
‘label’=>’Password’,
),
));

$this->add(array(
‘name’ =>’usr_email’,
‘attributes’=> array(
‘type’=>’email’,
),
‘options’=>array(
‘label’=>’E-mail’,
),
));

$this->add(array(
‘name’ =>’usrl_id’,
‘type’=>’Zend\Form\Element\Select’,
‘options’=>array(
‘label’=>’Role’,
‘value_options’=>array(
‘1’=>’Public’,
‘2’=>’Member’,
‘3’=>’Admin’,
),
),
));

$this->add(array(
‘name’ =>’lng_id’,
‘type’=>’Zend\Form\Element\Select’,
‘options’=>array(
‘label’=>’Language’,
‘value_options’=>array(
‘1’=>’English’,
‘2’=>’French’,
‘3’=>’German’,
),
),
));

$this->add(array(
‘name’ =>’usr_active’,
‘type’=>’Zend\Form\Element\Select’,
‘options’=>array(
‘label’=>’Active’,
‘value_options’=>array(
‘0’=>’No’,
‘1’=>’Yes’,

),
),
));

$this->add(array(
‘name’ =>’usr_question’,
‘attributes’=> array(
‘type’=>’text’,
),
‘options’=>array(
‘label’=>’Question’,
),
));

$this->add(array(
‘name’ =>’usr_answer’,
‘attributes’=> array(
‘type’=>’text’,
),
‘options’=>array(
‘label’=>’Answer’,
),
));

$this->add(array(
‘name’ =>’usr_picture’,
‘attributes’=> array(
‘type’=>’text’,
),
‘options’=>array(
‘label’=>’Picture URL’,
),
));

$this->add(array(
‘name’ =>’usr_picture’,
‘attributes’=> array(
‘type’=>’text’,
),
‘options’=>array(
‘label’=>’Picture URL’,
),
));

$this->add(array(
‘name’ => ‘usr_password_salt’,
‘attributes’ => array(
‘type’  => ‘text’,
),
‘options’ => array(
‘label’ => ‘Password Salt’,
),
));

$this->add(array(
‘name’ => ‘usr_registration_date’,
‘attributes’ => array(
‘type’  => ‘text’,
),
‘options’ => array(
‘label’ => ‘Registration Date’,
),
));

$this->add(array(
‘name’ => ‘usr_registration_token’,
‘attributes’ => array(
‘type’  => ‘text’,
),
‘options’ => array(
‘label’ => ‘Registration Token’,
),
));

$this->add(array(
‘name’ => ‘usr_email_confirmed’,
‘type’ => ‘Zend\Form\Element\Select’,
‘options’ => array(
‘label’ => ‘E-mail was confirmed?’,
‘value_options’ => array(
‘0’ => ‘No’,
‘1’ => ‘Yes’,
),
),
));

$this->add(array(
‘name’ => ‘submit’,
‘attributes’ => array(
‘type’  => ‘submit’,
‘value’ => ‘Go’,
‘id’ => ‘submitbutton’,
),
));
}
}

here is the code for userFilter.php(you could use javascript like backbone to validate the form instead will more faster)

<?php
namespace CsnUser\Form;

use Zend\InputFilter\Factory as InputFactory;
use Zend\InputFilter\InputFilter;

class UserFilter extends InputFilter
{
public function __construct() {
$this->add(array(
‘name’=>’usr_name’,
‘required’=>true,
‘filters’=>array(
array(‘name’=>’StripTags’),
array(‘name’=>’StringTrim’),
),
‘validators’=>array(
array(
‘name’    => ‘StringLength’,
‘options’ => array(
‘encoding’ => ‘UTF-8’,
‘min’      => 1,
‘max’      => 100,
),
),
),
));

$this->add(array(
‘name’       => ‘usr_email’,
‘required’   => true,
‘validators’ => array(
array(
‘name’ => ‘EmailAddress’
),
),
));

$this->add(array(
‘name’     => ‘usr_password’,
‘required’ => true,
‘filters’  => array(
array(‘name’ => ‘StripTags’),
array(‘name’ => ‘StringTrim’),
),
‘validators’ => array(
array(
‘name’    => ‘StringLength’,
‘options’ => array(
‘encoding’ => ‘UTF-8’,
‘min’      => 6,
‘max’      => 12,
),
),
),
));

$this->add(array(
‘name’     => ‘usr_active’,
‘required’ => true,
‘filters’  => array(
array(‘name’ => ‘Int’),
),
‘validators’ => array(
array(
‘name’    => ‘Digits’,
),
),
));

}

}

the index.phtml code:for retrieve data from your database, here is your presentation page, on you browser type:http://zf2.local/csn-user/user/index

should look like this:

crud3

 

<h1>Index = Retrieve</h1>
<a href=”<?php echo $this->url(‘csn_user/default’, array(‘controller’ => ‘user’, ‘action’ => ‘create’)); ?>”>Create</a>
<table>
<tr>
<th>usr_id</th>
<th>usr_name</th>
<th>usr_email</th>
</tr>
<?php
foreach ($rowset as $row) {
?>
<tr>
<td><?php  echo $row->usr_id; // The row is an ArrayObject $row->usr_id and $row[‘usr_id’] with both work?></td>
<td><?php  echo $row[‘usr_name’]; ?></td>
<td><?php  echo $row[‘usr_email’]; ?></td>
<td><a href=”<?php echo $this->url(‘csn_user/default’, array(‘controller’ => ‘user’, ‘action’ => ‘update’, ‘id’ => $row[‘usr_id’])); ?>”>Edit</a></td>
<td><a href=”<?php echo $this->url(‘csn_user/default’, array(‘controller’ => ‘user’, ‘action’ => ‘delete’, ‘id’ => $row[‘usr_id’]));?>” onclick=”return confirm(‘Do you rely want to delete the record?’)”>Delete</a></td>
</tr>
<?php
}
?>
</table>

your update.phtml code:

<h1>Update</h1>
<?php
$form = $this->form;
$form->prepare();
$form->setAttribute(‘action’,$this->url(‘csn_user/default’, array(‘controller’ => ‘user’, ‘action’ => ‘update’, ‘id’ => $id)));
$form->setAttribute(‘method’,’post’);

echo $this->form()->openTag($form);
echo $this->formCollection($form);
echo $this->form()->closeTag();

your create.phtml code:

<h1>Create an new user</h1>
<?php
$form = $this->form;
$form->prepare();
// Assuming the “contact/process” route exists…
$form->setAttribute(‘action’, $this->url(‘csn_user/default’, array(‘controller’ => ‘user’, ‘action’ => ‘create’))); //’contact/process’));
// Set the method attribute for the form
$form->setAttribute(‘method’, ‘post’);

echo $this->form()->openTag($form);
echo $this->formCollection($form);
echo $this->form()->closeTag();

And you don’t need a delete view

 

Zend Framework2: Tutorial Part4: Building a CRUD app by using Table Gateway1

The previous post I already wrote how to add a references to the databases connection, this post I will tell you how to building a CRUD app.

First, create a new module named “CsnUser”, or just copy the entire “Application” fold, and change the name to “CsnUser” .

then you file struction should look like this.

crud1

Second, go to open your Module.php

Here is the code:inside it quick just standard proceed, just tell where is the config file and  the namespace’s path.

<?php

namespace CsnUser;//this namespace must match your module’s name

class Module
{
public function getConfig()
{
return include __DIR__ . ‘/config/module.config.php’;
}

public function getAutoloaderConfig()
{
return array(
‘Zend\Loader\StandardAutoloader’ => array(
‘namespaces’ => array(
__NAMESPACE__ => __DIR__ . ‘/src/’ . __NAMESPACE__,
),
),
);
}

}

Continue reading Zend Framework2: Tutorial Part4: Building a CRUD app by using Table Gateway1

Zend Framework2: Tutorial Part3 use TableGateway add references to the database connection

Just go to modify “config/autoload/global.php”

return array(
‘db’ => array(
‘driver’ => ‘Pdo’,
‘dsn’ => ‘mysql:dbname=test;host=localhost’,
‘username’ => ‘db_user’,
‘password’ => ”,
‘driver_options’ => array(
PDO::MYSQL_ATTR_INIT_COMMAND => ‘SET NAMES \’UTF8\”
),
),
‘service_manager’ => array(
‘factories’ => array(
‘Zend\Db\Adapter\Adapter’
=> ‘Zend\Db\Adapter\AdapterServiceFactory’,
),
),
);

Zend Framework2: Tutorial Part2: Hello World Module Application

The reason I start to write this post, because I read saw a lot video on youtube.com take hours to tell you how do it. And I also follow it but it is waste my time. Didn’t work for me. And I write this as my study record also hope that could help someone to start Zend Framework2.

Actually it is very simple,

First,copy the entire “Application file” and rename it to “Hello” (You could just rename the “Application” to “Hello” or any name you like, but under this file those three name must match )

HelloModuleZendFrameWork

Second, open the “Module.php”, the namespace Hello (this namespace should match your module name).

Continue reading Zend Framework2: Tutorial Part2: Hello World Module Application

Zend Framework2: Tutorial Part1:Teaching you step by step install Zend Framework under window 8 system

Since Zend Framework is very popular, so I decided to install it in my local server to use it. Install Zend Framework is similarly like your install another PHP Framework.

1) go to here download the sample programmer, below is the link

https://github.com/zendframework/ZendSkeletonApplication

2)Unzip to your local hosting my is C:\ xampp \ htdocs and rename ZendSkeletonApplication

3)go to below link to download the latest version, I just download Zend Framework 2.2.5 Full

http://framework.zend.com/downloads/latest

4)unzip it and put it under the vendor file

the path in my computer is like this:

zfDirection

C:\xampp\htdocs\ZendSkeletonApplication\vendor\ZF2

The direction should look like this:

4) Now we need to binding domain

go to here:C:\Windows\System32\Drivers\etc

use notePad to open the file:hosts

Add two line below it:(you actually could change to another name you like it,since this use Zend Framework version 2 and under local host ,so I just named like this for easy remember it).

127.0.0.1       zf2.local
127.0.0.1       http://www.zf2.local

Now,you could access http://zf2.local testing has become effective.

5)Edit the Apache configuration file,I just go to C:\xampp\apache\conf\extra , use notePad open httpd-vhosts.conf

Add this to it:

<VirtualHost *:80>
ServerName localhost
DocumentRoot “C:\xampp\htdocs”
</VirtualHost>

<VirtualHost *:80>
ServerName zf2.local
ServerAlias http://www.zf2.local
DocumentRoot “C:\xampp\htdocs\ZendSkeletonApplication\public”
</VirtualHost>

restart the Apache service. In the browser to re-visit http://zf2.local can open ZendSkeletonApplication test program.

Now you could start programming now.

Here have some option set,not necessary.

1) Adjust the level of error messages,go to C:\ xampp \ php \ php.ini

a)change this error_reporting=E_ALL & ~E_DEPRECATED & ~E_STRICT to error_reporting=E_ALL & ~E_STRICT

b)PHP by default is use short_open_tag=off, now you could change to short_open_tag = On

c) write 2 lines below the finle

extension= php_intl.dll
extension= php_openssl.dll