So I'm trying 100% to use MVC...

#1
[eluser]d3[/eluser]
So I've set aside a bunch of time and decided to learn the MVC approach because the projects that I'm looking to get started on are getting bigger and bigger and things might just flow better using MVC. Adding a second programmer to the mix and getting him onboard right from the word "go" would be an asset.

So here's my issue:

I've got:
Code:
<?php
echo '<div id="menu">'."\n";
/*
<li class="current_page_item">
*/


$list = array(
'<a href="'. site_url(">homepage</a>',
'<a href="'. site_url(">Services</a>',
'<a href="'. site_url(">Hosting</a>',
'<a href="'. site_url(">Portfolio</a>',
'<a href="'. site_url(">About Me</a>',
'<a href="'. site_url(">Contact Me</a>'
);

$attributes = array(
'class' => '',
'id'    => 'main'
);

echo ul($list, $attributes);


echo '</div>'."\n";

Code in a "view"? Now thats a negative. But I'm not sure where it should be. no real data envolved... only going to be used once per page.. *shrug* Am I trying to get TOO MVC? I've looked at a few blogs on the subject and they are talking about having one word attributes in the view, which as a self taught php-guy blows my mind (kinda)

Once everyone stops laughing at the new guy I'll ask mysecond question on switch() (one of my olde favorites.) aswell.

d3.

edit: all my site_urls are messed. can't even add a txt file. I'm more then sure that you guys know what I'm trying to get at.

Thanks for the read.
d3.

#2
[eluser]jtkendall[/eluser]
Hi d3,

In my opinion, I think you're trying too hard to do 100% MVC. It's going to be hard to get a dynamic view that doesn't have any PHP code in it (though, you could look into the template parser class http://ellislab.com/codeigniter/user-gui...arser.html).

Now, regarding your code, I'm a little confused as it seems you're over complicating it. Why not simply do:

Code:
<div id="menu">
    <ul id="main">
        <li><a href="&lt;?=URL;?&gt;">Homepage</a></li>
        <li><a href="&lt;?=URL;?&gt;">Services</a></li>
        <li><a href="&lt;?=URL;?&gt;">Hosting</a></li>
        <li><a href="&lt;?=URL;?&gt;">Portfolio</a></li>
        <li><a href="&lt;?=URL;?&gt;">About Me</a></li>
        <li><a href="&lt;?=URL;?&gt;">Contact Me</a></li>
    </ul>
</div>

Obviously replace all of the "URL"'s with the actual urls you want. It's 10 lines of code instead of 25 and requires far less PHP than your example (it won't need any if you use relative urls eg. /services). Even the user guide has examples of views with code in them. Everyone who has used MVC has put code in their views at one point or another.

You mentioned a question about the switch() statement, what is it?

#3
[eluser]Unknown[/eluser]
Views are there to setup the display that the user is going to see. If you need to put code in to convert how the data is displayed, then so be it.

You could code the whole page in Php and have it push out HTML and you'll still be fine.

That being said, most designers use things like Dreamweaver and HTML to design pages - so to make it easier for everybody involved, it's better to develop your views in html and only using php when really needed. Pretty much like what jtkendall did

#4
[eluser]xwero[/eluser]
I have to agree with jtkendall, you are using functions just because they are there. A view should contain more markup than php. The urls and their links can be prepared in the controller and then you only have to loop over them in the view. To make it easier for a designer to change the links you can put them in a language file.
Code:
// controller
$this->lang->load('nav');
$this->load->helper('language'); // the lang function isn't loaded by default
$data['nav'] = array(site_url('home')=>lang('home'),site_url('services')=>lang('services'));

// view
&lt;?php foreach($nav as $url=>$link): ?&gt;
<li><a href="&lt;?php echo $url ?&gt;">&lt;?php echo $link ?&gt;</a></li>
&lt;?php endforeach ?&gt;

#5
[eluser]d3[/eluser]
awesome.

I read through the user guide a few times and tried to apply EVERYTHING no matter at the cost of size (no of lines)

I'm going to take my laptop into work with me today and do another re-write and see about squeezing a bit more PHP out of my views and dumping it into a controller or three.

Thanks for the input,

d3.

#6
[eluser]obiron2[/eluser]
MVC pattern explained for above scenario

Your list of links is a 'dynamic' element and may change on a different web site or in the future - or you may want to have different menu options on different pages.

Your links data should go in a model
Your controller should ask the model for a list of links
Your view should render the links as is seen fit.

Now your model could just store your links in an array and return the array, or it could read a text file or it could get the information from a database.

The beauty of MVC is that the controller doesn't care how the model gets it's data and doesn't have to worry about what the page looks like.

Implementing the above, you have a 'generic' process for building your menus. Adding a new option to the menu for each page could be as simple as adding a record to a database table.

All the business logic happens in the controller
All the user rendering happens in the view
All the data access happens in the model.

Obiron


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2020 MyBB Group.