Welcome Guest, Not a member yet? Register   Sign In
Your Opinion on my view library
#1

[eluser]jake73[/eluser]
Hello, i'm new in codeIgniter, sorry for my bad english.

I create a big website with code igniter and i want your point of view on my code.

I have a big controller with lot of function :
Code:
<?php
class mycontroller extends Controller {

   function mycontroller() {
      parent::Controller();
   }

   function action1($var1, $var1){
    $this->load->model('mdl_1');
    $data['tab1'] = $this->mdl_1->get();
    $this->affichage->display_action1($data);
   }
   function action2($var1, $var1){
    $this->load->model('mdl_2');
    $data['tab2'] = $this->mdl_2->get();
    $this->affichage->display_action2($data);
   }
   function action3($var1, $var1){
    $this->load->model('mdl_3');
    $data['tab1'] = $this->mdl_1->get();
    $this->affichage->display_action3($data);
   }
//action4
//action5
//action6
//action7 .. etc ...

}
?>

I have create a library to manage view :

Code:
<?php
class display {

function display ()
{
    $data['title'] = '';
    $data['keywords'] = '';
    $data['description'] = '';
    $data['page'] = '';
}

function display_action1($data){
    $data['title'] = "title action 1";
    $data['keywords'] = 'keywords1, words1';
    $data['description'] = 'description 1';
    $data['page'] = 'action1';
    $this->_output($data);
}
function display_action2($data){
    $data['title'] = "title action 2";
    $data['keywords'] = 'keywords2, words2';
    $data['description'] = 'description 2';
    $data['page'] = 'action2';
    $this->_output($data);
}
function display_action3($data){
    $data['title'] = "title action 3";
    $data['keywords'] = 'keywords3, words3';
    $data['description'] = 'description 3';
    $data['page'] = 'action3';
    $this->_output($data);
}
//etc with display_action4, 5 ,6 , ...

function _output($data)
{    
    $CI =& get_instance();
    $CI->load->view('site/theme', $data);
}

}
?>

And the principal view site/theme (simple) :

Code:
<head>
<title><?php echo $title;?></title>
<meta name="keywords" content="<?php echo $keywords;?>" />
<meta name="description" content="<?php echo $description;?>" />
</head>
<div id="content">
&lt;?php $this->load->view('site/content/'.$page);?&gt;
</div>
&lt;/body&gt;
&lt;/html&gt;

What do you think about that, can i simplify ?
There is lot of code in my controller so i want to have a library or other thing which help me to generate the title and the meta.

I'm new in CI, be indulgent ! thanks :o)
#2

[eluser]Référencement Google[/eluser]
That's a lot repetitive, you could create functions instead of copying code like that, use the RAD principles:
http://en.wikipedia.org/wiki/Don't_repeat_yourself

I would say also that using a Database would be a good thing, sure a better idea than to store those meta stuff in hard coded PHP.
#3

[eluser]KeyStroke[/eluser]
I don't know what your "action"s here are, but I do know that you're using so many unneccessary functions in the Display class. Why not just code one functions that is flexible and does all the work. Instead of setting each variable in a "display_actionX" function, just set an array like this:
Code:
$data['title'] = "title action 1";
    $data['keywords'] = 'keywords1, words1';
    $data['description'] = 'description 1';
    $data['page'] = 'action1';

Then send that array to a single "display_action" which would look something like this:
Code:
function display_action($data)
{
$this->_output($data);
}

This way you don't have to edit all "display_actionX" functions if you some time decide you need to add more data to the page.

I think it would be more helpful if we show us the actual code though.
#4

[eluser]jake73[/eluser]
In fact my title, meta and description are generate with $data, so i have to manage by view how to compose them .
Code:
function display_action1($data){
    $data['title'] = "Something " . $data['field'];
// ...
}

Code:
function display_action2($data){
    $data['title'] = $data['field'] . "another";
// ...
}
#5

[eluser]KeyStroke[/eluser]
[quote author="elitemedia" date="1208974876"]That's a lot repetitive, you could create functions instead of copying code like that, use the RAD principles:
http://en.wikipedia.org/wiki/Don't_repeat_yourself

I would say also that using a Database would be a good thing, sure a better idea than to store those meta stuff in hard coded PHP.[/quote]Doesn't storing this kind of little information in the database create an unnecessary overhead?
#6

[eluser]jake73[/eluser]
I have another idea :

Put my title and my meta in the content view. :

Ex : view page1
Code:
&lt;?php
$data['title'] = "Something " . $data['field'];
$data['description'] = "Description " . $data['field'];
$data['keywords'] = $data['field1'].", " . $data['field2'];
?&gt;
Content of page1

And in the view theme :

Code:
&lt;?php
$content = $this->load->view('page1',$data,true);
?&gt;
&lt;head&gt;
&lt;title&gt;&lt;?php echo $title;?&gt;&lt;/title&gt;
&lt;meta name="keywords" content="&lt;?php echo $keywords;?&gt;" /&gt;
&lt;meta name="description" content="&lt;?php echo $description;?&gt;" /&gt;
&lt;/head&gt;
<div id="content">
&lt;?php echo $content ;?&gt;
</div>
&lt;/body&gt;
&lt;/html&gt;

But i think i can't access to $data in the content view ?

And how i can replace 'page1' of $content = $this->load->view('page1',$data,true);
with the name of the controller function ?
#7

[eluser]jake73[/eluser]
I can access to $data with $this->load->vars($data); ?
#8

[eluser]Référencement Google[/eluser]
[quote author="KeyStroke" date="1208975980"]Doesn't storing this kind of little information in the database create an unnecessary overhead?[/quote]

A database role is to store data. So I would say no, it does not add unnecessary overhead. If your goal is performance, then you can also cache those queries.
#9

[eluser]Référencement Google[/eluser]
[quote author="jake73" date="1208980281"]I can access to $data with $this->load->vars($data); ?[/quote]

You of course can do that. A good way to go is to use also your library constructor to simplify things and put in it the stuff that is common to your library. You also can use the MY_Controller.php technique that will make your variables available site wide, here is a simple example:

Code:
class MY_Controller extends Controller {

    function MY_Controller()
    {
        parent::Controller();

        // Set default meta tags globally
        $metas = array(
                        'title'     => 'Default site Title',
                        'meta_desc' => 'Default site description',
                        'meta_keyw' => 'Default site keywords'
                        );

        $this->load->vars($metas);

    }




Theme © iAndrew 2016 - Forum software by © MyBB