Welcome Guest, Not a member yet? Register   Sign In
Should I create a library or put everything in a controller?
#1

[eluser]revered[/eluser]
I am new to codeigniter, never coded anything with it. Now I want to start my first codeigniter app (mainly for code standardization and avoiding bugs and security issues).

This app will have (in the backend) a cron that will scrape data from a lot of websites, these websites provides an API which I can use by making a request with cURL.

I currently have three PHP files, one has a class that do the database SELECTS/INSERTS/UPDATES and also manages cron status, another file has a class that build the requests to scrape the data from the APIs, and another one that include those two classes and run the requests (this is what runs as a cronjob).

Now I have been doing some reading and I see that is a "best practice" to keep controllers small and simple. So to code this on codeigniter it looks like the best option would be to create a library for the APIs, then a model for the database functions and a controller to run the requests (which would run as a cronjob). In the future I'd have to deal with a few different APIs so I would need different libraries for each API.

Is this correct, I mean is a library what I need? Or should I put everything into the controller that will run as a cronjob?
#2

[eluser]smilie[/eluser]
Hi,

It really depends on the amount of the (lines) of your code.
In case it is not very long, you could put it in the controller itself.

However, you do say that in future more (new) API interfaces will occure. In that case, I would recommend to place those API calls in their own libraries and keep controller 'mean and lean'.

This will help you keep your files / code orginized most of all. This way, in case of changing code, you will most probably only change the library (API) and not the controller itself.

Hope this helps you on the way :-)

Cheers,
Smilie
#3

[eluser]revered[/eluser]
Thanks, it did help, I needed a confirmation from people that already uses CI.

Libs seems to be the best option, otherwise my controller will be a mess.




Theme © iAndrew 2016 - Forum software by © MyBB