Welcome Guest, Not a member yet? Register   Sign In
Unable to pass URI parameters
#1

[eluser]vedant[/eluser]
Hi All,

I have been struggling with this for a while. I am trying to pass a parameter to a method via the URI but anytime I pass a parameter, the default controller gets invoked. From the logs,
DEBUG - 2013-03-03 02:40:44 --> Router Class Initialized
DEBUG - 2013-03-03 02:40:44 --> No URI present. Default controller set.

URL example:
http://www.site.com/index.php/account/verifyemail/123

From routes.php:
Code:
$route['account/verifyemail'] = "account/verifyemail";
$route['account/verifyemail/(:any)'] = "account/verifyemail";

This is what Account looks like:
Code:
class Account extends CI_Controller {
function Main()
{
  parent::Controller();
}

public function verifyemail() {
  echo $this->uri->segment(3);  
}
}

Am I missing something here? Any help will be greatly appreciated!
#2

[eluser]Eduard Stankovic[/eluser]
your controller should be like this
Code:
class Account extends CI_Controller {
function __construct()
{
  parent::__construct();
}

public function verifyemail() {
  echo $this->uri->segment(3);  
}
}

and I think your filename should be lowercase


I hope it was helpful
#3

[eluser]vedant[/eluser]
Hi Eduard - thanks for the suggestion.

The filename for the Account class is in lowercase - account.php

Also, I updated the controller based on your recommendation. When I pass the URI param, I am still being redirected to the default controller.

Some more information:
Code:
$config['uri_protocol'] = 'AUTO';

Does the routing look right to you?
Code:
$route['account/verifyemail'] = "account/verifyemail";
$route['account/verifyemail/(:any)'] = "account/verifyemail";
#4

[eluser]TheFuzzy0ne[/eluser]
I would suggest you comment out the routes and see if you can access the page directly. I suspect the problem is not with your routing, but more to do with the location of your file, or maybe the permissions.

The URI protocol might be a problem. I would only suggest playing with that after you've commented out your routes, just in case the problem is indeed with your routing (which I doubt), but no harm in playing it safe.

Please can you repost:
1) The exact location of your controller file.
2) The exact name of your controller file.
3) The new contents of your controller file.
#5

[eluser]vedant[/eluser]
Location and Name of controller file:
/application/controllers/account.php

Contents of controller file
Code:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Account extends CI_Controller {
  
function __construct() {
  parent::__construct();
}

public function verifyemail() {
  echo '<h1>Verify Email</h1>';
  echo $this->uri->segment(3);  
}

}

Based on the suggestion, I commented out the routes:
1. mysite.com/index.php/account/verifyemail now also gets routed to the default controller. This was correctly echo-ing the <h1>Verify Email</h1> earlier
2. mysite.com/index.php/account/verifyemail/123 is still routing to the default controller.

From the logs:
DEBUG - 2013-03-03 17:35:34 --&gt; URI Class Initialized
DEBUG - 2013-03-03 17:35:34 --&gt; Router Class Initialized
DEBUG - 2013-03-03 17:35:34 --&gt; No URI present. Default controller set.
#6

[eluser]TheFuzzy0ne[/eluser]
I find it rather odd that the rest of your site seems to work without any problems.

What is the output of the following:

Code:
log_message('debug', print_r($_SERVER, TRUE));

Please be sure to remove any sensitive information.

The only other thing I can think of, is that the problem lays somewhere in your .htaccess file. Other than that, I'm totally baffled.
#7

[eluser]Otemu[/eluser]
Hi,

Actually everything is working as expected(Your routes aren't doing anything really and are not even needed):

mysite.com/index.php/account this would direct to the actual default page of your class you have no index set so this would return 404 error, add this to your controller if you want default page for your account class

Code:
public function index()
{
  echo 'Default Page';
}

mysite.com/index.php/account/verifyemail the second segment of the URI determines which function in the controller gets called 'verifyemail', which is correct, this is why you are displayed with the verify email h1 tag

and finally mysite.com/index.php/account/verifyemail/123 If your URI contains more then two segments they will be passed to your function as parameters, so this is why your still getting directed to verifyemail function, for instance try this code below

Code:
public function verifyemail($myPara) {
  echo '<h1>Verify Email</h1>';
  echo $myPara;    
}

Now if you use mysite.com/index.php/account/verifyemail/123

You will see it echos Verify Email but also your parameter that you passed gets echoed too.

Hope that helps.

#8

[eluser]Aken[/eluser]
It's possible that your host doesn't allow index.php to see the extras of the URI added behind it. Try going to application/config/config.php and changing the uri_protocol setting until you see something different. You don't need the routes and your controller looks fine.




Theme © iAndrew 2016 - Forum software by © MyBB