Welcome Guest, Not a member yet? Register   Sign In
call a MY_Controller function form a view (link)
#1

[eluser]zoellsche[/eluser]
Hello,

I am new at programming und CI.

I have some functions, which are needed in some controllers.

So I started to create a MY_Controller in the core directory of CI to follow the "keep it dry rule" and placed the functions there.

I can use these functions, which are called from inherited controllers of the MY_Controller.

Can somebody explain, why in the url, main/logUserOut works and MY_Controller/logUserOut did not work. I am getting "404 error massage" all time.

How can I solve this problem?

thank you.
#2

[eluser]Harold Villacorte[/eluser]
What you have to understand about CI is that controllers and their methods are always accessible through http. If you have functions that do not represent a particular uri segment you should put them in a library or helper.
#3

[eluser]zoellsche[/eluser]
ok, I understand. only controllers in the controller folder are accessible by url.

How can the problem be solved?

I think my Solution can not be called "best practise".

The Main controller who extends the MY_Controller can call the logUserOut function, this function calls logUserOutInMY function in the MY_Controller (or other libraries/ helpers).

I have a bad feeling doing it this way....

What is the "right way" to do it?
#4

[eluser]Harold Villacorte[/eluser]
CI_Controller is the "Super Object" so when you write a controller that extends CI_Controller or a model that extends CI_Model you can access just about any file. All libraries, helpers, and models can be loaded from anywhere using $this->load. Additionally you can load any resource from a library or helper by referencing the Super Object. It's all there very early in the documentation. If you put methods in a controller that do not represent a uri it is not just bad practice but it is highly insecure.
#5

[eluser]zoellsche[/eluser]
I solved the problem:

the logUserOut function is in every controller inherited, who extends the MY_Controller.

In the view file this worked very fine.

<a href = '&lt;?php echo base_url() . "login/logUserOut"; ?&gt;'> Logout </a>

It works, because the login Controller extends the MY_Controller and has the possibility to access the called function from the login Controller in the MY_Controller....

If somebody knows a better way, comment pls.


MfG
zoellsche.





Theme © iAndrew 2016 - Forum software by © MyBB