Calling a controller method from a view

#1
[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?

#2
[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

#3
[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.

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

http://ellislab.com/codeigniter/user-gui...elper.html

Here's an example.

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

#5
[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.

#6
[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.


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2021 MyBB Group.