views:

1436

answers:

2

Hello All, I'm working on a survey system for my company and I have it setup in the system so that there are two ways to take the survey.

1) New Survey Taker no prior information

2)Survey was already sent out and a session was created.

In case one I would like my URL to look like:
mydomain.com/SurveySystem/index.php/survey/$surveyID

($surveyID being a integer of the survey to take)

The second case would where we create a link with for the test taker. I would like the URL to look like this:
mydomain.com/SurveySystem/index.php/survey/$surveySessionID/$guestID

In my Survey class I have it setup as the following:

function index(){

$segments = $this->uri->total_segments();

if($segments == 1){
   echo "no surveyID set";
   return;
}

if($segments == 2){
    $this->take_survey($this->uri->segment(2));
}

if($segments == 3){
    $this->survey_session($this->uri->segment(3), $this->uri->segment(4));
}

}

When no information is passed the it echos just fine. But if I try to put a integer where the surveyID is it thinks i'm loading up a method in the controller.

Thank you for the help!

+2  A: 

Use URI routing to override the default controller/function/arguments mapping.

Example: in your application/config/routes.php:

$route['survey/:num'] = "survey/take_suvey";

Bonus: You can also remove the index.php/ part, see Removing the index.php file.

streetpc
Thanks for the answer! Thats exactly what it was. I have tried to remove the index.php but when I place .htaccess at the project level and add:RewriteEngine onRewriteCond $1 !^(index\.php|images|robots\.txt)RewriteRule ^(.*)$ /index.php/$1 [L]I always get an apache error :-/
whobutsb
This means that the rewrite module is not installed or that rewriting is not auhtorized. If you don't have control over it, you can use the ErrorDocument method: ErrorDocument 404 my_rewriter.php, in which you send the status code 200 (OK), then include index.php. Then make sure it still returns 404 status for really non-existing pages.
streetpc
A: 

My question is, with a URL that unfriendly, why do you care what it looks like at all? It is not semantic from a user perspective. Yes, you should be removing the index.php. Coupling the URL rewriting, the whole thing should be reduced from:

mydomain.com/SurveySystem/index.php/survey/????

to

mydomain.com/survey/

And your CI class methods can be reduced to "take," or "submit," and a subsequent "review."

Sessions should be managed using cookies or CI's session class. If you need to track state in the URI, combine your survey-specific "session" and the "guestID" into one segment with base64 encoding.

Lastly, using a route as suggested, there will be no way for your app to know what survey ID to load. It would need to capture the ":num" and feed it to take_survey:

$route['survey/(:num)'] = "survey/take_survey/$1";

If you take numeric ids as the first segment after /survey, you need another route placed after that one to handle the case where that segment is a session ID:

$route['survey/(:num)/(:num)'] = "survey/session_manager/$1/$2";

Where the $1 and $2 are session ID and Guest ID respectively. Personally, I would say this is bad form. The semantic meaning of your segments break down: it becomes difficult to determine what that first numeric segment means (is it a survey ID or session ID), unless you can always guarantee that these routes are in place.

TheresonAntaltego