Welcome Guest, Not a member yet? Register   Sign In
Call a controller from a library
#1
Rainbow 

Hi guys,
some day ago I start to learn CI and a bit of MVC framework.
I follow this tutorial on youtube  about login system (using ion_auth was too difficult for me now).
While I follow the tutorial I've made some changes to let this compatible with CI3, correct some bug like xss_clean, ad adapt to my need.
Everything works, of course its not awesome but it works.
But, I want to learn I hate writing code and don't understand. So, I read that Controller in MVC paradigm, have to be used for build pages, load views, etc.
I don't like to have function like "form validation" in controller.
So I try to build a library with all function like logout, registration, login, validation, but I don't understand how I could let the library works.

My function on library call specific controller function, how can I call that function on controller but let that independent? so I could maybe think to reuse the library for other small project of mine?

thank you all in advance
Reply
#2

You shouldn't do this ... calling a controller from a library is the complete opposite of making it independent.

Controllers are application-specific, libraries are not - that's the main difference between them. When you call a controller from within a library, that makes the controller a dependency for your library, and that in turn means that your library is no longer portable.
Reply
#3

(01-29-2016, 03:45 AM)Narf Wrote: You shouldn't do this ... calling a controller from a library is the complete opposite of making it independent.

Controllers are application-specific, libraries are not - that's the main difference between them. When you call a controller from within a library, that makes the controller a dependency for your library, and that in turn means that your library is no longer portable.

mmm =( you right...you damn right...so I have to loose my intention to write a library =( to do an example I have this function

PHP Code:
 public function login_validation(){
        
// caricamento della librerira per la validazione del form
        
$this->load->library('form_validation');

        
// setting delle regole per input
        //il callback rimanda ad una funzione dello stesso controller che controlla le credenziali d'accesso
        
$this->form_validation->set_rules('email','Email','required|trim|callback_validate_credentials');
        
$this->form_validation->set_rules('password','Password','required|md5|trim');

        
// la funzione run ritorna un true solo se le regole sopra sono verificate
        
if ($this->form_validation->run() == true) {
            
//salviamo la mail nella sessione, se i dati sono esatti. e impostiamo un flag a 1.
            
$data = array('email' => $this->input->post('email'),
                                         
'is_logged_in' => true);
            
$this->session->set_userdata($data);
            
$this->members();
        } else {
            
$this->login();
        } 

where members(); and login(); that you see at the end are function of the same controller.
With a code like this there is no way to make a library...
Reply
#4

Quote:With a code like this there is no way to make a library...

Libraries sound cool but much of the time Models are the better solution.
when you are first developing start by just working in the controller. its faster to get something working that way. then refactor the code into your model(s).

Use application/config/autoload.php right away so you don't have to wonder if some library or helper has been loaded or not, like
PHP Code:
$autoload['libraries'] = array('database''form_validation','session','table''email'); 
Reply




Theme © iAndrew 2016 - Forum software by © MyBB