System

Zend Framework ( Introduction Part-2)

မဂၤလာပါ။ကၽြန္ေတာ္ကေတာ့ ေမာင္ရဲထက္ေအာင္ ျဖစ္ပါတယ္။
Spiceworks Myanmar company မွာ web developer အျဖစ္ဝင္ေရာက္လုပ္ကိုင္ေနပါတယ္။
က်ြန္ေတ့ာ္အေနနဲ႕ PART(1)မွာ Zend Framework Structureအေၾကာင္းကိုဒီ Link မွာရွင္းၿပထားပါတယ္ခင္ဗ်။
https://www.spiceworksmyanmar.com/blogs/Nzg5/detail
ဒီတစ္ခါမွာေတာ႕ Zend Framework Coding အေၾကာင္းကို ကၽြန္ေတာ္ေလ့လာထားသေလာက္ sharing လုပ္ေပးခ်င္ပါတယ္။
Application/configs/application.ini

      
resources.db.adapter = "pdo_mysql"
resources.db.params.dbname = "database" 
resources.db.params.username = "root"
resources.db.params.password = ""
resources.db.params.host = "localhost"
resources.db.params.charset = "utf8"
resources.layout.layoutPath = APPLICATION_PATH "/layouts/scripts/"
[staging : production]    

Line နံပါတ္(2) က ကိုယ္ခ်ိတ္မယ့္ database ရဲ႕ name ထည့္ေပးရပါမယ္။
Line နံပါတ္(3) က ကိုယ္ခ်ိတ္မယ့္ database ရဲ႕ username ထည့္ေပးရပါမယ္။
Line နံပါတ္(4) က ကိုယ္ခ်ိတ္မယ့္ database ရဲ႕ password ထည့္ေပးရပါမယ္။

Application/layouts/scripts/layout.phtml

<html lang="ja">
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge">
        <meta name="viewport" content="width=device-width, initial-scale=1">
        <meta http-equiv="Content-Style-Type" content="text/css">
        <meta http-equiv="Content-Script-Type" content="text/javascript">
        <meta name="keywords" content="">
        <meta name="title" content="Sample">
        <meta name="author" content="">
        <meta name="copyright" content="2008 Zend">
        <title>Sample Project</title>
        <script type="text/javascript" src="/js/jquery-2.1.3.min.js"></script>
        <?php echo $this->headScript() ?>
   </head>
    <body>
        <?php echo $this->layout()->content; ?>
    </body>
</html>

Application/controller/IndexController.php

<?php
class IndexController extends Zend_Controller_Action
{
    public function init()
    {
        $this->headScript()
                ->appendFile('/js/jquery-2.1.3.min.js')
    }

    public function indexAction()
    {
       $this->_helper->layout->setLayout('layout’);

        $this->view->name=‘Hello World!’;
    }

Application/View/script/view.phtml

<p>
    <?php echo $this->name ?>
</p>

headScript() အေၾကာင္းကိုရွင္းျပရမယ္ဆိုရင္ေတာ့ layout နဲ႕ controller တြဲ ျပီးရွင္းျပပါမယ္။
ပထမဆံုး IndexController ရဲ့ code line နံပါတ္ 6 မွာ ေရးထားတဲ့ headScript code ကို ျပန္ေခၚခ်င္ရင္ layout ရဲ႕ code line နံပါတ္ 14 ပုံစံလိုမ်ိဳး ေၾကညာျပီးျပန္ေခၚလို႔ရပါတယ္။အဲလိုမသံုးခ်င္ဘူး တစ္ျခားနည္းဘဲသံုးခ်င္တယ္ဆိုရင္ေတာ့ layout မွာ script ကို code line နံပါတ္ 13 မွာလိုိမ်ိဳး တိုက္ရိုက္ေၾကညာေပးလို႕ရပါတယ္။

layout/layout.php ကို controller ထဲက Action မွာ ဘယ္လိုေၾကညာေပးရမလဲဆိုတာ ေျပာျပခ်င္ပါတယ္။
layout ကိုယူသံုးမယ္ဆိုရင္ေတာ့ ကိုယ္အသံုးျပဳခ်င္တဲ့ Actionထဲမွာ $this->_helper->layout->setLayout(‘layout’)
လို႕ေ့့့ၾကညာေပးရပါမယ္။ IndexController ရဲ႕ code line နံပါတ္ 12 မွာ ေရးျပထားပါတယ္။

controller ထဲကေန data ေတြကို View page မွာ ဘယ္လိုထုတ္ျပရမယ္ဆိုတာကို ရွင္းျပပါဦးမယ္။
controller ထဲက code line နံပါတ္ 14 မွာ $this->view->name=‘Hello World!’; လို႔ ေရးထားတာကို
View page မွာ echo $this->name လို႕ထုတ္လိုက္ရင္ browser မွာ ‘Hello World!’ လို႔ ထြက္လာပါလိမ္႕မယ္။

ေနာက္တစ္ခုကေတာ႔ Form မွာ အသံုးၿပဳတဲ့ code ေတြ အေၾကာင္းကိုရွင္းျပပါမယ္။

	<?php class Application_Form_Add extends Zend_Form { public $elementPrefixPaths = array('filter' => array(
		'prefix' => 'Filter',
		'path' => DIR_FILTERS
	));

	public $elementFilters = array(
		'XSSCode',
	);

	public function init() {

		$Name = $this->createElement('text', 'Name');
		$Name->setAllowEmpty(false);
		$Name->addValidator('NotEmpty', true);
		$Name->getValidator('NotEmpty')->setMessage('err_Add_name_&&_Please Fill Add Name.');


		$this->addElements(array(
			'Name' =>$Name,
			
		));

		$this->addElementPrefixPaths($this->elementPrefixPaths);
		$this->setElementFilters($this->elementFilters);
	}
}

Type နဲ႔ Name ကို ေၾကညာ ဖို႔ ဆိုရင္ createElement() function ကို အသံုးျပဳ ရပါမယ္။
setAllowEmpty() function ကေတာ႕ input box က blank ျဖစ္ေနလို႕ Required လုပ္ခ်င္တယ္ဆို true ကို အသုံးျပဳမယ္။မလုပ္ခ်င္ဘူးဆိုရင္ flase ကို အသံုးျပဳမယ္။
addValidator() function ကေတာ႔ setAllowEmpty() က false ေရြးထားရင္ NotEmpty, true လို႔ေရးေပးရပါမယ္။true ေရြးထားတယ္ဆိုရင္ေတာ႕ NotEmpty, false ေပါ႕။
getValidator() function ကေတာ႕ အေပၚက အကုန္ျပီးသြားရင္ validate စစ္ျ္ပီး setMessage() မွာကိုယ္ေရးခ်င္တဲ့ error ထုတ္ျပဖို႕ အသံုးျပဳရပါတယ္။

ကၽြန္ေတာ္သိသေလာက္ ေနာက္ထပ္လည္း ေလ့လာျဖစ္တာေလးေတြ Sharing လုပ္ေပးသြားပါ့မယ္ ခင္္ဗ်။
ေနာက္တစ္ၾကိမ္မွာလည္း Zend Framework PDF EXPORT အေၾကာင္းကို ကၽြန္ေတာ္ေလ့လာထားသေလာက္ sharing လုပ္ေပးခ်င္ပါတယ္။ ဖတ္ရႈေပးပါဦးလို႕ တိုက္တြန္းပါရေစဗ်ာ။
ဖတ္ရႈေပးတဲ့အတြက္လည္း အားလံုးကို ေက်းဇူးတင္ပါတယ္ ခင္ဗ်ာ။

Hello