Welcome Guest, Not a member yet? Register   Sign In
existing simple CMS, reauthoring for MVC framework
#1

[eluser]benmanson[/eluser]
Hi,

I am using CI for the front end of a website I have built and have used the MVC approach to build the code. I have existing code for a simple CMS for the site that is procedural PHP and was created before I moved to CI.

I want to rebuild the CMS so it now sits within the CI framework but am getting stuck with some simple things.

Editing content for example. Currently it is done with one PHP file basic code structure as follows:
Code:
if(isset($POST['submit'])):

  $h1 = $_POST['h1'];
  etc with all variables

  $sql = mysql_query("UPDATE menu SET...");
  etc updating all dbase tables

else:

  $id = $_GET['id'];

  $sql query to get existing content
  etc

  <form action="<?=$_SERVER['PHP_SELF']?>" method="post">
    <input name="description" type="text" value="<?=$description?>" />
    etc

I figure I should have all the database queries in my cms model. But where is the best place to build the form? Within my cms library file? And should I use the basic if/else syntax in the library like I have done above? How do I make PHP_SELF work in this structure (that's if I even need to use it)?

Currently I have one CMS class set up (cms.php) with an index function which lists all content pages. To edit a particular page, the link is:
Code:
cms/editcon/$id


So I have started another function within the cms class called editcon.
Code:
function editcon($id)

And now I am a bit stuck as to how to structure it. Can anyone help? Apologies for the ramblings, hopefully someone can understand what I am trying to do.

Thanks
Ben
#2

[eluser]Crafter[/eluser]
You are on the right track. I suggest you don't rty and complicate thinks. Have another look through the first section of the CI manual, and you will pull through.

So far, you say you have created a controller class CMS. That's fine.

Code:
class Cms extends Controller {
;;;
}

Next you say that you want to allow the CMS class to provide edit functionality.So far so good:
Code:
class Cms extends Controller {
;;;
   function editcon() {
   :::
   }

}

OK, In general your controller function should be doing one or more of the following:
- Getting a request from the user (from the URL)
- Processing the data (eg. validating data, converting, calculating)
- Requesting data from your data storage
- Sending data to your data storage (new or existing data)
- Displaying some user interface items to the user


The forms should be build using the forms component of the CI framework.
All database access should be left to the models.
Your controller should be involved in preparing the data and making the requests.

For example, see if you can follow the code below. I'm not suggesting this is the right way to do it, but understanding what is going on will help you grasp the concepts a bit better.
Code:
function editcon() {
      if ($this->input->post('save') ) {           // User has pressed save on form
         $cms_data['content']      = $this->input->post('content');
         $cms_data['description'] = $this->input->post('description');

         // Load the model and give it the data to save
         $this->load->model('contentmodel');
         $this->contentmodel->modify($cms_data);
     }
     else {
        // User is asking for data (from the URL) http://www.mydomain.com/cms/editcon/4
        //  segment 1 : http://www.mydomain.com
        //  segment 2: cms
        //  segment 3: 4

        $id = $this->uri->segment(3);

         // Request data from the model layer
         $this->load->model('contentmodel');
         $cms_data = $this->contentmodel->get($id);

        // Display the data
        $this->load->view('content_form', $cms_data);

    }


   :::
   }
#3

[eluser]benmanson[/eluser]
thanks heaps Crafter, i'll work through your post and let you know how I go.

cheers
Ben
#4

[eluser]benmanson[/eluser]
[quote author="Crafter" date="1187064923"]
The forms should be build using the forms component of the CI framework.
[/quote]

Where is it best to build the form? In the library I have been building for the cms or in the view file?

In the blog video tutorial it's built directly in the view file but they don't touch on libraries there.

cheers
Ben




Theme © iAndrew 2016 - Forum software by © MyBB