Php – MVC function – okay in the view


I am upgrading my site from some old spaghetti coding to some nice, clean custom MVC structure (and having fun learning in the process).

On my page to display blog listings, I had a function that would help my dynamically build HREF's for the a links – to keep track of applied filters via $_GET…hard to explain…but here it is:

 | this function reads current $_GET values for controlling the feed filters,
 | and replaces the $value with the desired new $value
function browse_controls($key,$value=null,$ret='url') {

    // find current control settings
    $browse_controls = array();
    if(array_key_exists('browse',$_GET)) { $browse_controls['browse'] = $_GET['browse']; }
    if(array_key_exists('display',$_GET)) { $browse_controls['display'] = $_GET['display']; }
    if(array_key_exists('q',$_GET)) { $browse_controls['q'] = $_GET['q']; }

    // replace desired setting
    if($value) {
        $browse_controls[$key] = $value;

    // build url
    $url = ABS_DOMAIN . 'sale/';
    if(!empty($_GET['cat'])) { $url .= $_GET['cat'] . '/';}
    if(!empty($_GET['sub'])) { $url .= $_GET['sub'] . '/';}
    $url .= '?' . http_build_query($browse_controls);
    return $url;

I could call this query by simply:

<a href='<?php echo browse_controls('browse',$prev_page); ?>' class="crumb">Previous Page</a>

How can I achieve that same with MVC structure and full separation of presentation and logic. Are functions allowed in my template?


Best Solution

I am not sure about your particular framework, but in Ruby on Rails and ASP.NET MVC, this kind of stuff is goes to the helper classes. They are UI concerns. So now logic here. Just formatting, transforiming, building HTML. You can place them near of the templates, or give them separate directory.

What I can reccomend is to inspect examples boundled with your framework. Often they give good knowlege of the framework.