Welcome Guest, Not a member yet? Register   Sign In
What is the best way to write organize your code?
#1

[eluser]Mareshal[/eluser]
I am asking this, because I am not a php expert. But since I started to write my scripts in CodeIgniter is a bit difficult until I'll fully understand MVC coding.

I saw I can work most of the time only with model < - > view , and use controller only for a few things(different URLs, easy stuff). Is this good for security of the script?

For example I make all database queries inside a model using functions, then I simply return the array with results(and I use the array directly in view file). Is this ok or I should make several view files for almost each function from model, then access it with controller, and then $this->load->view('view1') and so on.

Please tell me, and tell other who maybe will read this topic: how do you organize your code? What do you write in controller? what do you write in model? Views?

I am some security maniac. I want the highest level of security in my scripts.
#2

[eluser]gtech[/eluser]
I wouldnt stress to much as CI is quite a loose frame work,

To answer your initial question:

I would be inclined to use the controller to parse the information comming in and then pass any data to the model function(s) then when the data is returned from the model you can pass that data to a single view and display accordingly (I am assuming you are developing a non Javascript/ajax orientated site). You may want to use more than one view if you have common views you wish to use for example a header or footer html.

Here are some tips I can think of:

1) Be consistant, If you find an approach that suits you that is fine, think about code readability, maintenance and reusability.

2) I consider controller functions to act like glue, they pull the data out from the request, call the correct models and then load the correct views.

3) If you have a controller function that is needed to be used in other controllers create a base controller with the common function(s) and then extend from the base controller.

4) Normally you would load a view from the controller, it is also common to load a view within a view if it makes sense to, loading a view in a model works but is a tad unauthodox.

5) Libraries and Helpers can be used in the view, controller or the model.. if you autoload a library or a helper it is automatically available in the view, model and controllers.

6) You usually would call model/models from the controller, however you can call a model from a view as long as you do not alter the data in the model/database. This would be useful if multiple controllers need to call the same view and access the same model as you would not need to load the model in every function.. however I think going down the extend controller route is a much more structured approach.

7) You can call a model in a model, but again the best way is to create a base model with common functionality and then extend it.

8) Try and keep html content away from the controller, and definatly the model.

9) Use active record to make your code database independant.

10) If you are writing code with a large datamodel, make sure you think the datamodel through first then the code structure will be more obvious.

these are things I can immediatly think of.. hope it helps
#3

[eluser]gtech[/eluser]
some more tips.

11) Models are considered to be database centric, but if you are unsure whether to put a piece of code in the controller or the model don't get to stressed over it, do what you think is right and be consistant as sometimes there is a grey area.

12) when passing a data array to a view don't forget the keys in the associative array become the variables in the view.

13) Once an associtive array has been passed to a view, the variables are available in EVERY other view in the same request, without having to pass the array to them again.

14) The XMLRPC code works quite well but I have found that you can only call functions in the same controller as the XMLRPC server.

15) A lot of databases don't support transactions within transactions.. so be carefull when calling a function with a transatcion from a model to another model function with a transaction. If this is the case put some conditional tags around the transation code to see if its being called from another model function with a transaction (phew that was a long winded tip)

16) When I have designed a datamodel, I usually keep my model classes closely linked to the table names, e.g. users.php would have add_user, edit_user and so on.. again sometimes there is a grey area as a function my need to join two tables, again I just try to use a consitant approach when this happens. Other people do have different methods.

17) Some people like to use [url="http://ellislab.com/forums/viewthread/65749/"]modular separation of code (Matchbox is a good example)[/url], its not my preference but wont hurt to get familiar with it.

18) dont spill coffee on the keyboard, my space bar is suffering.
#4

[eluser]Mareshal[/eluser]
[quote author="gtech" date="1245450715"]these are things I can immediatly think of.. hope it helps[/quote]
Now only hope...this really helps a lot. Thanks.

Yes, I am keeping html away from controller, even if I am tempted to put some html there, and same with models. I have html only in views.

Can I pass variables to a view from a model with $this->view->load('view', $data)

I don't understand this
Quote:12) when passing a data array to a view don’t forget the keys in the associative array become the variables in the view.

13) Once an associtive array has been passed to a view, the variables are available in EVERY other view in the same request, without having to pass the array to them again.

Can you please give me a simple example?
#5

[eluser]gtech[/eluser]
sure try it yourself, to prove it works.

Controller function ($v_data can be called anything):
Code:
// function inside controller

function testpage() {
  //create an associative array, the keys var1 and var2 become the variable
  //names in the view.
  $v_data = array('var1'=>'1234','var2'=>'abcd');
  $this->load->view('view1',$v_data);
  // dont need to pass $v_data to view2 to as the variables have been cached.
  $this->load->view('view2');
}

view view1 (note the array keys are the variable names):
Code:
<hr>
&lt;?=$var1?&gt;
&lt;?=$var2?&gt;

view view2 (var1 is still available):
Code:
<hr>
&lt;?=$var1?&gt;


*note &lt;?= $var1?&gt; is the same as &lt;?php echo $var1 ?&gt;
#6

[eluser]Mareshal[/eluser]
OK, now I understood. I didn't know that

Btw, I am actually using this: &lt;?=$var1?&gt; ;-P is easier than echo .

I have almost 1 year of php experience, but not daily work. Only when I needed.
#7

[eluser]gtech[/eluser]
Cool, good luck!

another top tip.. don't spend to much time on the forums, it stops you doing your work.
#8

[eluser]Mareshal[/eluser]
I don't spend much time on forums. I am student, 2nd degree and now I am finishing the session. I have one more exam tomorrow. PHP is just like a simple relax for me
#9

[eluser]TheFuzzy0ne[/eluser]
[quote author="Mareshal" date="1245463847"]PHP is just like a simple relax for me[/quote]

Trying building a forum with CodeIgniter. Tongue
#10

[eluser]Mareshal[/eluser]
ok, I can make it. But don't expect to have high security , or complicate admin panel - user control panel. A basic forum is ok. But building a forum in only 1 person could take 2-3 weeks of hard working and a lot of bugs. A forum is a big project. Anyway, the main problem in big projects is not the code, methods or classes. The problems is the way you think it. For big projects you need pen and paper first.

When I said php is relax for me, I didn't think you would say build a forum Smile). But this is simple now: http://codeigniter.com/tutorials/watch/blog/ Wink




Theme © iAndrew 2016 - Forum software by © MyBB