CodeIgniter Forums
Calling a controller method from a view - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived General Discussion (https://forum.codeigniter.com/forumdisplay.php?fid=21)
+--- Thread: Calling a controller method from a view (/showthread.php?tid=20521)



Calling a controller method from a view - El Forum - 07-13-2009

[eluser]Simon Phoenix[/eluser]
I'm sure this is an easy one but does anybody know a way to call a controller method from a view without specifing the entire url each time.

For example how can you use:

Code:
<a href="controller/method/value">

instead of:

Code:
<a href="domain_name/controller/method/value">

I've tried leaving the domain name out but get an error message. Can you specify the domain name globally so you don't need to repeat it in every url?


Calling a controller method from a view - El Forum - 07-13-2009

[eluser]developer10[/eluser]
[quote author="Simon Phoenix" date="1247507483"]I'm sure this is an easy one but does anybody know a way to call a controller method from a view without specifing the entire url each time.

For example how can you use:

Code:
<a href="controller/method/value">

instead of:

Code:
<a href="domain_name/controller/method/value">

I've tried leaving the domain name out but get an error message. Can you specify the domain name globally so you don't need to repeat it in every url?[/quote]

seems like you should define your base path (http://yourdomain.com) or something like that


Calling a controller method from a view - El Forum - 07-13-2009

[eluser]Simon Phoenix[/eluser]
I have defined the basepath in the config file but this doesn't seem to make any difference. By using the url helper I can define each url as:

Code:
site_url("controller/method/value")

This seems to work well but I just thought there must be a way to define it globally since the basepath remains constant.


Calling a controller method from a view - El Forum - 07-13-2009

[eluser]Cro_Crx[/eluser]
Use the URL helper

http://ellislab.com/codeigniter/user-guide/helpers/url_helper.html

Here's an example.

Code:
&lt;?=anchor('controller/method/value')?&gt;



Calling a controller method from a view - El Forum - 07-13-2009

[eluser]Simon Phoenix[/eluser]
Thanks for your response Cro_Crx. Have tried out the anchor() function and it works well - I'll use this method in future. I have actually seen this done without using any functions and just specifing the relative path so if anyone knows how to do that i'd be interested to know.


Calling a controller method from a view - El Forum - 07-13-2009

[eluser]Cro_Crx[/eluser]
[quote author="Simon Phoenix" date="1247512352"]I have actually seen this done without using any functions and just specifing the relative path so if anyone knows how to do that i'd be interested to know.[/quote]

You could do this, the only problem is that it's not 100% portable. Let's say for example if your using the same view for the header and you have two pages one's blogs/jeff/posts/3 and the other is blogs/jeff. The URI relative to one won't be the same to the other. That's why you always want to use the URL helper functions when possible.