What is the benefit of helpers in CodeIgniter?

I am starting to learn CodeIgniter. I see some advantages, for example. MVC conventions. But in many cases, I don’t see how CI helps me grow faster.

For example, this

$attributes = array('class' => 'email', 'id' => 'myform'); echo form_open('email/send', $attributes); 

can use instead

 <form method="post" accept-charset="utf-8" action="http:/example.com/index.php/email/send" class="email" id="myform" /> 

I do not understand why this should be faster.

In addition, there are HTML helpers for creating <h1> tags. I do not see the benefits of using an assistant here.

So why should I use these types of helpers?

+4
source share
4 answers

Helpers can make your life easier and even make you write cleaner code, sometimes at the expense of flexibility.

There is no significant improvement in your example - whether in terms of performance or code length - but these are not all CodeIgniter helpers.

Personally, I don’t like the fact that the Framework writes HTML for me, but I want it to help me, figure out some data. Think of them as static methods that can be accessed globally from your controllers, models, or even views, if you need to. You send the data as a parameter, process it and return the result.

Some Codeigniter helpers I can't live with are 'url and ' date ' .

+1
source

I agree that there are some helpers that are pretty much useless because some use more than writing the HTML result that they will output. But there are other helpers that make life much easier, url is the first that comes to mind.

Moreover, the supporting structure is very useful. If you ever find formatting data the same way over and over in your views, you should think about creating (or expanding) the helper and using it instead. Although not all helpers are useful, the ability to add to them is very convenient.

+1
source

ha its funny i had the same reaction. but even this form is open.

  • in your example, the open form allows you to specify the code without the need for use or mixing in any HTML . to write and browse. less mistakes.

  • same idea for different types of form fields. think about whether you are passing values ​​from the database to the form fields. Like a user record that is being updated. if you do not use the form helper, this will be a shocking confusion of the php and html and echo statements for each individual form field.

  • real gain? especially when you are dealing with several forms - you can specify the values ​​of the form fields - in the configuration file - and then make them available for viewing. then everything is important in a simple array, and you barely need to touch the view.
0
source

One big option for using form helpers is the drop-down list (select, option), because you can pass the default value from a variable without going through parameters and setting 'selected="selected"'

0
source

Source: https://habr.com/ru/post/1499680/


All Articles