Welcome Guest, Not a member yet? Register   Sign In
Pass variables through the url to codeigniter
#1

[eluser]Thefox[/eluser]
Hello,

I have a page in CI which needs to receive data from a different website. How do I pass the variables from site1 to the CodeIgniter site through the URL?

Basically: site1's link is something like: www.mycodeigniterpage.com/ci/index.php/controller/page1?var1=hey

How do I get CI to receive those variables?

Thanks for the help
-Fox
#2

[eluser]bretticus[/eluser]
If I had a dime for every time that question is asked. :-)

You can query the forum and find endless posts about this. Short answer: CI uses a "convention over configuration" approach like other MVC frameworks and passing GET variables is not part of the convention. However there are ways to get around this (look for those posts.)

I stumbled on this article when googling just now. It's an approach I have never seen before (may be in the wiki for all I know.) Looks interesting.
#3

[eluser]Colin Williams[/eluser]
CI has never, never ever ever, never never ever ever, prevented segment + query sting URIs. Just set your uri_protocol to something other than AUTO or QUERY_STRING, and set enable_query_strings to TRUE. Really no reason to do it any other way. Should be the default config IMHO, but I guess EllisLab really does have some problem with query strings.
#4

[eluser]bretticus[/eluser]
[quote author="Colin Williams" date="1254397641"]CI has never, never ever ever, never never ever ever, prevented segment + query sting URIs. Just set your uri_protocol to something other than AUTO or QUERY_STRING, and set enable_query_strings to TRUE. [/quote]

Take a look at his URL: ...controller/page1?var1=hey. Setting enable_query_strings to TRUE will force TheFox to use index.php?c=controller&m=method sitewide, will it not? I believe this is the same ole "mixing url segments with get variables" question, don't you?
#5

[eluser]Colin Williams[/eluser]
No, because the Router class uses either REQUEST_URI or PATH_INFO to interpret the request. In fact, some server setups have trouble with REQUEST_URI, I believe, so PATH_INFO is recommended. This is in fact the primary suggestion in the article you linked to.
#6

[eluser]bretticus[/eluser]
[quote author="Colin Williams" date="1254398345"]No, because the Router class uses either REQUEST_URI or PATH_INFO to interpret the request. In fact, some server setups have trouble with REQUEST_URI, I believe, so PATH_INFO is recommended. This is in fact the primary suggestion in the article you linked to.[/quote]

Are we getting the point of mixing segment based URI with GET variables here confused? Actually the examples in the article did not work for me (CI 1.7.2 using MAMP.) Now, I can do REQUEST_URI w/ enable_query_string = TRUE and access the file with index.php?c=controller&m=function&somevar=1 with /controller/function working (without the param.) I CANNOT do, however, /controller/function?somevar=1.

Is this what you are referring to?

EDIT: Ah, I was getting the questions out of context. I wrote:
Quote:Setting enable_query_strings to TRUE will force TheFox to use index.php?c=controller&m=method sitewide, will it not?
According to the article, and my own testing, you are not forced to use GET variables sitewide. I suppose the following warning in the manual always scared me off:

Quote:Please note: If you are using query strings you will have to build your own URLs, rather than utilizing the URL helpers (and other helpers that generate URLs, like some of the form helpers) as these are designed to work with segment based URLs.

Always made me think setting enable_query_strings to TRUE would mess up anchor or something and turn off segment-based URIs.

I stand corrected (and a little wiser.)
#7

[eluser]Colin Williams[/eluser]
I haven't had any trouble with using PATH_INFO + enable_query_strings = TRUE. I think the Router class might need to be modified to work with REQUEST_URI (since it has the query string and PATH_INFO does not).

I think the real solution is for CI to:

1.) Abandon PATH_INFO and REQUEST_URI as primary options
2.) Alter query string method to use a single parameter (like ?q=controller/function/id)
3.) Suggest a mod_rewrite rule that does
Code:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?q=$1 [L,QSA]
#8

[eluser]bretticus[/eluser]
[quote author="Colin Williams" date="1254399878"]I think the real solution is for CI to:

1.) Abandon PATH_INFO and REQUEST_URI as primary options
2.) Alter query string method to use a single parameter (like ?q=controller/function/id)
3.) Suggest a mod_rewrite rule that does
Code:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?q=$1 [L,QSA]
[/quote]

I actually do that in a home-brewed mvc framework that I wrote back some time ago. I agree, seems like drupal, wordpress, etc. do it that way as well.
#9

[eluser]wiredesignz[/eluser]
The PATH_INFO environment variable will work as Colin describes on your development server but it tends to fail on servers using a CGI wrapper for PHP

Setting your own environment variable in .htaccess allows you to use segmented url's together with query_strings.
Code:
RewriteEngine On

#RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
#RewriteRule ^(.*)$ http://%1/$1 [R=301,L]

RewriteBase /path_to_index_php/

RewriteCond %{ENV:REDIRECT_CI_PATH} !^$
RewriteRule ^(.*)$ - [E=CI_PATH:%{ENV:REDIRECT_CI_PATH}]

RewriteCond %{ENV:CI_PATH} ^$
RewriteRule ^(.*)$ - [E=CI_PATH:/$1]

RewriteCond $1 !^(index\.php|robots\.txt|favicon\.ico)
RewriteRule ^(.*)$ index.php [L]
Code:
$config['uri_protocol'] = 'CI_PATH';
#10

[eluser]bretticus[/eluser]
This question has been asked at least 4000 times this year. Perhaps someone with good experience on this matter should write a guide into the wiki (I looked briefly but didn't turn up anything.)




Theme © iAndrew 2016 - Forum software by © MyBB