English 中文(简体)
What s the proper MVC way to do this....?
原标题:

Quick question about general MVC design principle in PHP, using CodeIgniter or Kohana (I m actually using Kohana).

I m new to MVC and don t want to get this wrong... so I m wondering if i have tables:

categories, pages, notes

I create a separate controller and view for each one...? So people can go to

/category/#
/page/#
/note/#

But then lets say I want to also be able to display multiple notes per page, it would be bad to call the note view in a loop from the page view. So should I create some kind of a function that draws the notes and pass variables to that function from the note view and from a loop in the page view? Would this be the best way to go about it, if not how else should I do it...?

Thanks,

Serhiy

最佳回答

Yes, instead of just passing 1 entity (category, page, note) to your view, pass a list of entities. With a loop inside the view, you can display the whole list. That view may call another one (or a function) that know how to display one entry.

问题回答

I would personally have a "show" method for one item and a "list" method for multiple. In your controller you can say something like $page_data[ note ] = get_note(cat_id,page_id) for the "show" method and $page_data[ notes ] = get_all_notes(cat_id) for the "list" method.

Then in your view, you loop over the $page_data[ notes ] and display HTML for each one. If the list view is using the same "note" HTML as the "show" view, create a template or function to spit out the HTML given a note:

// In your "list" view
foreach($n in $page_data[ notes ]){
    print_note_html($n)
}

//In your "show" view
print_note_html($n)

The print_note_html function can be a helper method accessible by all views for Notes. Make sense?

  1. You can loop in the View. The View is allowed can also access the model in MVC. See: http://www.phpwact.org/pattern/model_view_controller
  2. You don t need to have a controller (or model) for each table.

In CodeIgniter I create a separate helper file where I put functions that return the markup for UI elements that may need to be included multiple times in the one view.

In your example, I would create a function to return the markup for a note.

application/helpers/view_helper.php

function note($note)
{
    return  <div class="note">  .
         <h2>  . $note->title .  </h2>  .
         <p>  . $note->contents .  </p></div> ;
}

I would normally auto-load this helper file. And then in the view I would do something like this.

echo note($note);

For a list of notes in a view, I would iterate the list calling this function.

<div class="note-list">
<?php foreach ($notes as $note) : ?>
    <?php echo note($note); ?>
<?php endforeach; ?>
</div>

I found that including a view many times in another view was slow. Thats why I did it this way.

Edit

I just dug into the CodeIgniter Loader class and sure enough a PHP include is being done every time you call

$this->load->view( view_name );

This means that if you use this method to display a list of 20 notes, you re going to be doing 20 separate includes.





相关问题
Brute-force/DoS prevention in PHP [closed]

I am trying to write a script to prevent brute-force login attempts in a website I m building. The logic goes something like this: User sends login information. Check if username and password is ...

please can anyone check this while loop and if condition

<?php $con=mysql_connect("localhost","mts","mts"); if(!con) { die( unable to connect . mysql_error()); } mysql_select_db("mts",$con); /* date_default_timezone_set ("Asia/Calcutta"); $date = ...

定值美元

如何确认来自正确来源的数字。

Generating a drop down list of timezones with PHP

Most sites need some way to show the dates on the site in the users preferred timezone. Below are two lists that I found and then one method using the built in PHP DateTime class in PHP 5. I need ...

Text as watermarking in PHP

I want to create text as a watermark for an image. the water mark should have the following properties front: Impact color: white opacity: 31% Font style: regular, bold Bevel and Emboss size: 30 ...

How does php cast boolean variables?

How does php cast boolean variables? I was trying to save a boolean value to an array: $result["Users"]["is_login"] = true; but when I use debug the is_login value is blank. and when I do ...

热门标签