Welcome Guest, Not a member yet? Register   Sign In
Helpers, Libraries, and where to load them..
#1

[eluser]tomclowes[/eluser]
This is a very basic question, but to finally get a grasp on this would help my understanding vastly.

I am using the helpers, and libraries A LOT. They are great.

Firstly, What exactly is the difference between a helper and a library.
Would I be correct to understand that a helper makes mundane task like outputting form inputs simple, and a library is essentially a series of functions that perform specific tasks?

Basically I am running through the user guide here, there, and everywhere. My files load libraries all over the place - if i get an obvious error, I simply include the helper directly in said page and it usually fixes the problem.

As such I am confident I have duplicate 'loads' of various helpers.

If I understand this correctly, is the best place to load helpers and libraries the index function of a controller? Although atm I do, should I NOT load helpers/libraries in view files?

To expand on that, I have made a custom function/callback for my form validation. It relies on sessions. Rather than have to load the session class and define the callback function for use in any/all form pages.. it would seem logical to load these helpers, and define this function in my MY_Controller.php file - would that be a correct assumption.

Thanks !
#2

[eluser]bretticus[/eluser]
A library is a stand-alone PHP class (related functions and variables; or methods and properties to use OOP terms.) Helpers are typically a collection of related simple functions that do not need to be loaded as an object and can stand by themselves. This is basically the difference between an object method(s) and a function (See PHP Classes and Objects.)

In classes, you can call the constructor (in PHP4 it's a method by the same name as the class. In PHP5 you can simply call function __construct().) This constructor method is called each time the class is instantiated (the same as setting a variable to a new class which CI does automatically via URL path. i.e: "Convention over configuration.") The index() method is just the default method for the controller class in CodeIgniter. Loading from here doesn't load globally for the entire Controller class. In other words, use the constructor instead. If you want the same helpers and libraries loaded in all your controllers, there are two options:

1. load from the constructor in a MY_Controller and extend all subsequent Controllers from that.

2. Or, the easiest method, load from the application/config/autoload.php file.

It's okay to load libraries, etc over and over again in different controllers and controller methods. One advantage is that your current method gets only the resources it needs which results in better overall performance. Of course, if you find yourself using a function from a helper or library over and over again, autoload it.
#3

[eluser]tomclowes[/eluser]
Absolutely brilliant. You guys/gals are brilliant Smile

In relation to the above coulf you elaborate - when there is no need to autoload, would it be correct to say that helpers/libraries should only be loaded in the controller?

As always, when I have one question, I think of ten - rather than create numeorus threads I will simply offload them here. Hope that is ok Smile

Firstly relates to routes.

First things first - I have removed index.php using htaccess.

On my website I want a URL system as follows.

http://domain.com/content/town/county

- content is the name of my controller.
- I then have a function index which has two parameters town, and county.

As such my URLs are:
http://domain.com/content/index/town/county

My resolution was simply to utilize route redirection to make

http://domain.com/content/town/county load http://domain.com/content/index/town/county

Is this the best approach?

Thanks
#4

[eluser]bretticus[/eluser]
[quote author="tomclowes" date="1282785102"]
In relation to the above coulf you elaborate - when there is no need to autoload, would it be correct to say that helpers/libraries should only be loaded in the controller?[/quote]

I'd go as far as to say in a controller method where it's needed unless every method in that controller needs it. It's good practice to make your controller methods as thin as possible and grab data from models (not just for database transactions.) It's okay to load libs/helpers in models as needed too.

[quote author="tomclowes" date="1282785102"]
On my website I want a URL system as follows.

http://domain.com/content/town/county

- content is the name of my controller.
- I then have a function index which has two parameters town, and county.

As such my URLs are:
http://domain.com/content/index/town/county

My resolution was simply to utilize route redirection to make

http://domain.com/content/town/county load http://domain.com/content/index/town/county

Is this the best approach?

Thanks[/quote]

Yes, because of course, custom routes lets us trade configuration over convention in special cases like yours.

Please note that you can also make use of the built in controller method _remap (you have to declare it of course.) It will process your URL however you want in just that controller (I prefer routing personally unless the use case is quite complex.)
#5

[eluser]tomclowes[/eluser]
Ok. Im in a position to run off my nights worth of questions.

Any help would be greatly appreciated.

1. $_POST and $this->input->post('')
I read somewhere in the CI docs that $_POST and $_GET did not work - they were disabled for security, yet $_POST works no problems in my controller files. As such, what is the point of the latter?

2. Universal values.
I have created a MY_Controller file which manages my user login 'stuff'.
On discussion with a user here I wrote the following code - my comments explain what I believe it to do.

Code:
//THIS MAKES IT AVAILABLE IN ALL VIEWS/CONTROLLERS AS $user['username'] etc
        $this->load->vars($this->data); //Make $this->data available to all views / controllers

Now I can access my user variables through the user array in my view files, however when i tried to utilize print_r($user) in my controller, nothing is outputted and I get errors.
My question is essentially - what is the true nature of load->vars?

3. External files.
I am learning about CodeIgniter after a friend suggested it for ease of use.
In my previous very much procedural approach to PHP, I had many a JS file included, some of which communicate with PHP files to do dynamic things behind the scenes.
At the moment, I have simply left these files as is in a seperate folder.

It would however be extremely useful to be able to utilize various code Igniter functions in these dynamic files (although not vital). They are housed in a folder entitled /js/ in my root - nowhere near my system file.

Could someone perhaps elaborate on the protocol/approach associated with the above.

4. Not specifically CI - but you guys know your stuff.
As you may have noticed, I am working extra hard on this development.
One thing that has been concerning me is general web standards as such.
I know PHP pretty proficiently (although CI is knew to me).
I have an understanding of CSS/Javascript etc etc - and although I couldnt code a feature from scratch, with a bit of guidance, tutorials etc, I can do what I want.

My concern is about the 'lastability' of something you dont fully understand.
For example using Javascript, and PHP I have developed an autosuggest for a form which works perfectly in IE and FF (others not tested). To what extend to web standards/browsers change - my ideal is 'it works now, it will always work'. Is this realistic?

Many thanks to anyone who can answer any of the above - it is greatly appreciated.

Cheers
Tom
#6

[eluser]bretticus[/eluser]
1. Here's my opinion on the matter. :cheese:

2.
Quote:This function takes an associative array as input and generates variables using the PHP extract function.
Use accordingly.

3. I STRONGLY think javascript (client-side code) should be totally abstracted from server-side code. In fact, I love jquery for this because the html you render in a view can be "latched on to." I think the best way to go about working with javascript functionality is to code the site to work without it and then come back with jquery and add enhancements (AJAX, effects, etc.) Thus, I tend to frown on the notion of rendering javascript server-side.

4.
Quote:To what extend to web standards/browsers change - my ideal is ‘it works now, it will always work’. Is this realistic?
Of course not, but you or someone else will re-factor all the code before there's anything to really worry about. Why? Because you or someone else will use the "next big thing" before the "big thing" of now is totally deprecated. Embrace the coolness of today and forge on!
#7

[eluser]tomclowes[/eluser]
Thanks for your responses.

In relation to 1, I have read your post - it makes a very interesting read. I have however not met problem such as yours. I have made absolutely no change to CodeIgniter and I can use $_POST['var']. Why would I ever use $this->input->post(’var‘)

2. This is the one that has caused me the most trouble - and getting a grip on it will help a lot. Basically I am struggling to get my head arround arrays etc within codeigniter and how they are passed from place to place.

My MY_Controller file contains:

Code:
$logged_in=$this->session->userdata('logged_in');
        
        if($logged_in == TRUE)
        {    
            
          $this->load->model('user_model'); //load model
          $this->data['user'] = $this->user_model->user_details();  //get user details


        }else{
          $this->data['user'] = FALSE;
        }
        
        //THIS MAKES IT AVAILABLE IN ALL VIEWS/CONTROLLERS AS $user['username'] etc
        $this->load->vars($this->data); //Make $this->data available to all views / controllers

Apologies for my idiocy here - this is my first usage of classes in production.
Code:
$this->user_model->user_details();
returns the user row as an array. I can access vars with
Code:
$this->data['user']['username']
for example.

Why does one not simply use:
Code:
$user = $this->user_model->user_details();  //get user details
then i can access things with
Code:
$user['username']

Anyway.. I assume the code above is the right approach.

Code:
$this->load->vars($this->data);

extracts the array per se.. but what is the process which means that trying to echo $user['username'] in my controller/model causes an error, but in my view files it works?

The idea of this is that I dont want to have to pass user details anywhere.. i want them to be everywhere.

3. Well that is essentially what I have done - I have coded the site, and am now sorting out the javascript. I am aware of the benefits of jquery but all my javascripts are simply things that I have seen online and implemented. My Javascript is completely abstracted, as is the simple files which process the results of the JS. Is this OK?

As an extra - my intention was to use these stand alone JS files, and then port to jquery as I have time to learn, appreciate and understand it. This somewhat links to 4. and CodeIgniter for PHP. It is like learning a new language because minimal knowledge of the base language is needed. A whole site based on CI and Jquery... what happens if they are both deprecated?


Thanks a lot !
#8

[eluser]bretticus[/eluser]
1. Are you sure you read my post? ;-) I never said that $_POST was not available. Your question was about hearing that both $_POST and $_GET were not available. You already mentioned you could use $_POST. My post deals with getting $_GET or query strings to work in CI, nothing more.

The benefit to using the Input class for post ($this->input->post('some_var')) is explained in the manual. You do not have to use it, but it's not a bad idea by any means.

2. From what you wrote, yes, in your views you should be able to use $user['username'] when $user is not FALSE because of the result of the extract function. $this->load->vars($this->data); extracts your $data property for views only. In fact, vars() is specifically for views (and any and all views for that controller...or controller method if called at that level.) It's the same as passing an array to ...load->view('name', array(...)). Within your controller class, the variable HAS NOT BEEN extracted.

3. Do not worry about deprecation. Everything will eventually become deprecated. Doesn't mean you shouldn't use the cutting edge (or the standard) now.
#9

[eluser]tomclowes[/eluser]
Many Thanks.

1. I did read your post, and I understood the point of it - it was simply that my original question was relating solely to $_POST so i wanted a little further clarification Smile

2. On that basis is there a similar way to $this->load->vars($this->data); which will allow me to have my user vars available in all Controllers/Models?

3. Ok Smile
#10

[eluser]bretticus[/eluser]
In addition to calling...

Code:
$this->load->vars($this->data);

call the following in every class method...

Code:
extract($this->data);

...but makes zero sense because you are in a class and $this->data['user'] isn't much harder to write than just $user.




Theme © iAndrew 2016 - Forum software by © MyBB