I have not blogged for a while;I have been busy ;).This blog post presents the partial codes for the presentation I did at BarCamp Cameroon. Barcamp Cameroon(barcamp.org/BarCampCameroon2010#BarCampCameroonProgram) passed 2 weekends ago and I must say it was an amazing event, the presentations and the people and the energy!. Check for My the presentaion here: MVC I did a preach on MVC(Model View Controller), how it can ease development across platforms:Web,Enterprise and Mobile: I am to demonstrate that for php using Kohanaphp;don’t ask why…I just love the framework.You can read the previous post if you are not using a framework and wish to know what a framework is. I don’t like assumptions,I will wish to make it common for everybody but I am force to!.:). I shall assume or require the following: 1.You have some familiarity with php 2.You have a basic familiarity with the kohanaphp framework(I use 2.3.1), If you are interested in the framework, then check my other post about 10 reasons, ok!. No more talking and business…GO!. We need to demonstrate the use of Model View Controller (MVC) in php using the Kohana php framework. It is a simple form with user Email and a Message to be submitted,validated and stored to the database. DATA: Model: Create a php file call it, contact.php drop it under the application folder/model and copy the following code to it: **header = new View('header'); //We set the title of the header.php file $view->header->title = 'Contact Info'; //we pass the footer.php to the view\_template $view->footer = new View('footer'); //we pass to the content of the view\_template contact\_view for the main business $view->content = new View('contact\_view'); $view->content->heading = 'Contact Info | MVC by Nara'; //lets get the form fields we intend to use ready $field = array ('message'=>'','email'=>''); //We set out notice variable to NULL $notice = ""; $post = $this->input->post(); //we then copy the form fields into an error variable, each form field has an error association $errors = $field; //VALIDATION...... //check if the form is submitted , if($post){ //we call the validation Library to do the validation for us, Thank you frameworks ;) $post = new Validation($\_POST); //we add rules to the data input //We filter whitespaces at the start and end of the input $post->pre\_filter('trim'); //All our fields are required $post->add\_rules('\*','required'); //We make sure the email is a valid one $post->add\_rules('email','valid::email'); //We do our stuffs when we pass the validation; We want to store in the database!. if($post->validate()) { //instantiate the object, to start using the model //I am using ORM $store = ORM::factory('contact'); $store->email = $\_POST; $store->message =$\_POST; $store->save(); //Lets make sure the data is stored in the database, then send the notice to the user if($store->save()==true){ $notice .="Thanks , your Information is Saved!"; } else{ $notice .= die('Trouble saving your data,Try again!'); } } //if the validation fails then there where errors, Let us report the errors else{ $notice.="There where some Errors:
"; //Let us repopulate the form $field = arr::overwrite($field, $post->as\_array()); //Lets set the corresponding errors,as we defined in the validation and stored in the field\_errors file $errors = arr::overwrite($errors,$post->errors('field\_errors')); foreach($errors as $key=>$post) { $notice .= $post."
"; } } } //We set the notice then finally render once. $view->content->set('notice',$notice); $view->render(true); } } **PRESENTATION:** **The View**: We shall be using 'views of views' forget the name just do as follow: All the following files are droped under the application/view folder. Create another php file, call it header.php and past the following code in it: css/my_css.css” />
** ".$notice.""; } ?> ";?> ";?> Create another file call it view_template.php and paste the following code into it: Create the Custom Error Messages: create another php file, call it field_errors.php drop under application/i18n paste the following: <?php $lang=array( ‘message’=>array( ‘required’=>‘The message field is required!’, ), ‘email’=>array( ‘required’=>‘The email field is required!’, ‘email’=>‘The email you provided is not a valid email!’, ‘default’=>‘The email field has a problem!’, ),); Create … NO! We are done creating…;). But if you need style then Create a css file call it my_css.css and drop under the a CSS folder under the maindirectory(It is not there create it!). Paste the following code: /* CSS Document */ body { font:12px/1.3 Arial, Sans-serif; } /*css for our form entries*/ form { width:380px;padding:0 90px 20px;margin:auto;background:#f7f7f7;border:1px solid #ddd; } label { cursor:pointer;display:block; color:#00EE00; } input { width:300px;border:1px solid #999;padding:5px;-moz-border-radius:4px; } textarea { width:300px;border:1px solid #999;padding:5px;-moz-border-radius:4px; } input:focus { border-color:#777; } /* submit button */ input { cursor:pointer;border:1px solid #999;padding:5px;-moz-border-radius:4px;background:#eee; } input:hover, input:focus { border-color: #333;background: #ddd ;} input:active{ margin-top:1px; } Run the codes!. This is What you get:
