What to do about those page specific little classes that don't seem to fit as Models or Libraries in CodeIgniter?

StackOverflow https://stackoverflow.com/questions/17602017

문제

In my current implementation of the MVC design pattern (demonstrated using PHP and CodeIgniter):

Let's say I have a "page" located at "www.mysite.com/blue/page" that is ultimately generated by the following (greatly simplified) files:

/libraries
    session_lib.php
/controllers
    /red
    /white
    /blue
        page.php
/models
    /red
    /white
    /blue
        funky_class.php
        page_model.php
/views
    /red
    /white
    /blue
        page.php

And here's an easy to understand controller:

// FILE: /controllers/blue/page.php

// Get some paramaters from URL
$params = $this->input->get();

// Use a library to do some stuff with these params with a session
$this->load->library('session_lib');
$this->session_lib->store_in_session($params);

// Use a very page specific class to do some funky stuff with the params
$this->load->model('blue/funky_class');
$funkified_params = $this->funky_class->funkify($params);

// Pass params to a model
$this->load->model('blue/page_model');
$data['output_from_db'] = $this->page_model->get_some_output_from_db($funkified_params);

// Send to view
$this->load->view('blue/page', $data);

And now the question...

What is the best procedure for these "funky" little page specific classes that don't interact with the database? In this example I store the little class with the models, and in some cases might just add additional methods inside the model class to do the funky stuff. Is this good practice? Or should the library and funky class both go inside the model so the controller is even skinnier (however the model might be used in other places without the need for sessions and funkiness)?

도움이 되었습니까?

해결책

I would use a helper.

  • When a helper is loaded, that function will be available in the global scope so you can call it anywhere.
  • They're great for small, stand-alone functions that do only one thing that is not necessarily related to code in a model or library
  • Helpers especially excel for me when converting things between one format or another (slugify, convert_timezone, change_query_string) or doing little tasks that you don't want to think about (force_ssl, is_image, uri_string)

funkify() seems like an appropriate helper function that may prevent a lot of repeated code.

Here's the codeigniter documentation page on them: http://ellislab.com/codeigniter/user-guide/general/helpers.html

If you're in a situation where the helper function is so specific it will be only used in one place, you can still use a helper. Name the helper after your controller, page_helper.php for example.

page_helper.php

function funkify($params) {
    // do funky stuff
    return $funky_params;
}

then in your controller:

class Page extends CI_Controller {
    public function __construct() {
        parent::__construct();
        $this->load->helper('page');
    }

    public function page() {
        // do stuff
        $funky_params = funkify($params);
        // do more stuff
        $this->load->view('blue/page', $data);
    }

I have no excuse for it, but sometimes if I am in a situation where I need a razor specific function that will only be used on one location (say, a controller) ever, I will put it right in the controller's file. You can paste a function outside of the class definition and it will act like a helper and be available globally (as long as that controller is loaded). You can also define functions inside of a view. Yucky, but possible. I don't like to do it often because it's unusual and not expected (by myself or other developers)

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top