Welcome Guest, Not a member yet? Register   Sign In
[solved] Need help with a simple routing problem
#1

[eluser]Unknown[/eluser]
Edit: nevermind, ill just catch everything with

Code:
$route['(:any)'] = "site";
$route['default_controller'] = "site";

And use uri->segment to see what pages have to be displayed.


---

Hi everyone, I've got a (i hope) simple question for you, why does the following code not work?


My site.php in the controller directory:
Code:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Site extends CI_Controller {

        function __construct() {
                parent::__construct();
        }

        public function index() {
                echo "index";
        }

        public function subpage() {
                echo "subpage";
        }

        public function newspost() {
                echo "newspost";
        }
}
?>

My routes file:

Code:
$route[':any/:any/:num'] = "site/newspost";
$route[':any/:any'] = "site/subpage";
$route['default_controller'] = "site";
$route['404_override'] = '';

If i try to go to localhost/mysite/ I see "index", but when i go to localhost/mysite/whatever i get a page not found (a page generated by apache, not CI), but shouldnt it say "subpage"?
#2

[eluser]Aken[/eluser]
Your route example was correct up to a point. You didn't specify a generic rule to handle only a single URL segment - you only caught three segments, two and zero.

I'd recommend setting it up like so:
Code:
$route['default_controller'] = 'site';
$route['404_override'] = '';

$route[':any/:any/:num'] = 'site/newspost';
$route[':any/:any'] = 'site/subpage';
$route[':any'] = 'site';
#3

[eluser]Unknown[/eluser]
Thanks!




Theme © iAndrew 2016 - Forum software by © MyBB