CodeIgniter Forums
Routing issue? - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forum-20.html)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forum-23.html)
+--- Thread: Routing issue? (/thread-55187.html)



Routing issue? - El Forum - 10-13-2012

[eluser]Joe Dawson[/eluser]
Hello everyone,

Firstly, apologies if this has already been posted elsewhere but I wasn't sure exactly the problem is therefore what to search. Secondly, I am very new to Codeigniter, so apologies if something doesn't seem clear but bare with me. Smile

I installed the most recent version of Codeigniter (2.1.3) and then set the base_url inside of my 'config.php' file to the following address: http://www.joedawson.me/labs/codeigniter

I then created my new controller, 'portal.php', and was made aware from a tutorial I began following (http://net.tutsplus.com/tutorials/php/codeigniter-from-scratch-day-1/) that you can set the default_controller inside of 'routes.php' so I did so - just to check this is correct, my code should be as follows... right?

Code:
$route['default_controller'] = "portal";

Just so who ever is reading this understands everything completely here is my 'portal' controller:

Code:
<?php

// Begin Portal Controller

class Portal extends CI_Controller {

function index() {
  $this->load->view('home');
}

function stats() {
  $this->load->view('stats');
}

}

So far so good, everything is running fine (at least I think it is, no errors are being returned). I have created my 'home.php' and 'stats.php' inside of my views folder just so you know. I then tried loading my 'stats' method from my 'portal' controller, but I ran into a problem.

As you can see, my 'home' view is being loaded fine: http://joedawson.me/labs/codeigniter/

But when I try loadings 'stats' I get the following: http://joedawson.me/labs/codeigniter/stats - I gather this is a .htaccess problem, I don't yet have one - when I extracted the .zip I didn't have one. Do I need one?

Assuming it's a .htaccess issue there, I then tried loading it like so: http://joedawson.me/labs/codeigniter/index.php/stats - Now I receive a 404?

What's weird, I can actually load it like so: http://joedawson.me/labs/codeigniter/index.php/portal/stats - which leads me back to my default_controller inside of 'route.php' - is that correct or am I doing something wrong?

Hopefully I explained that well enough (not too much, I hope) for someone to understand where I'm going wrong, I'd really appreciate the help!

Thanks, Joe Smile


Routing issue? - El Forum - 10-13-2012

[eluser]CroNiX[/eluser]
1) base_url needs a trailing slash (http://www.joedawson.me/labs/codeigniter/)
2) home controller (index method) is what gets called when going to your site root (no controller in the URL) (what the base_url is). A homepage. If you want to access other methods in the home controller, you have to call them like any other controller (controller/method) unless you use (additional) routes.
3) You get the 404 because to access the "stats" method of the "Portal" controller, you need to access it like:
http://http://www.joedawson.me/labs/codeigniter/index.php/portal/stats




Routing issue? - El Forum - 10-13-2012

[eluser]Joe Dawson[/eluser]
[quote author="CroNiX" date="1350161319"]1) base_url needs a trailing slash (http://www.joedawson.me/labs/codeigniter/)
2) home controller (index method) is what gets called when going to your site root (no controller in the URL) (what the base_url is). A homepage. If you want to access other methods in the home controller, you have to call them like any other controller (controller/method) unless you use (additional) routes.
3) You get the 404 because to access the "stats" method of the "Portal" controller, you need to access it like:
http://http://www.joedawson.me/labs/codeigniter/index.php/portal/stats

[/quote]

Hey,

Thank-you for the reply!

1: Thank-you, I've added the trailing slash
2: I think I got confused here, I assumed that you could access the 'default_controller' that is set inside of 'routes.php' can be accessed like so: http://mydomain.com/index.php/method
3: Going back to point 2, is there a way to call a method from the 'default_controller' without having to pass the controller name into the URL?

Thanks again!


Routing issue? - El Forum - 10-13-2012

[eluser]CroNiX[/eluser]
For #3, you'd have to create a new route to bypass CI's expected default controller/method (which can introduce problems unless you understand how this works).

Code:
$route['stats'] = $route['default_controller'] . '/stats';  //(portal/stats)



Routing issue? - El Forum - 10-13-2012

[eluser]Joe Dawson[/eluser]
[quote author="CroNiX" date="1350162247"]For #3, you'd have to create a new route to bypass CI's expected default controller/method (which can introduce problems unless you understand how this works).

Code:
$route['stats'] = $route['default_controller'] . '/stats';  //(portal/stats)
[/quote]

I think I'll avoid bypassing anything for now then, thank-you for your explanation and help!