• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How do I make the route for methods in a controller all stem from the root?

#1
How do I make the route for methods in a controller all stem from the root?


So If I have


Code:
<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Main extends MY_Controller {

public function debates(){
}

public function questions(){

}

public function index()
{
}

/main/debates routes to /debates and /main, /main/questions routes to /questions automatically based on the method name.

According to the documentation I can define a route then define what I want it to be rewritten to, but when I do that, it doesn't work.

Putting $route['main/debates'] = 'debates'; or $route['main/questions'] = 'questions'; causes an error 404.
Reply

#2
You've got it backwards. For instance should be:

$route['debates'] = 'main/debates';

If you want to be able to visit /debates and have the debates method in Main.php handle the request.
Reply

#3
Is there a way to make all methods in a controller stem from the route, so instead of me manually having
Code:
$route['newdebate'] = 'main/newdebate';
$route['questions'] = 'main/questions';
Instead every controller method in the Main controller is set from the root so it automatically becomes /newdebate or /questions upon me creating the controller method?

Also I'm having a problem with routing and my controller.

Code:
I have this code in my controller.
public function debate($id){
    $debate = $this->db->query("SELECT * FROM debates WHERE nodeviewid = '$id' && nodeview = 'debate'")->result_array();
    print_r($debate); exit();
}

When I point the browser to /main/debate/1 the $debate variable isn't blank, but when I go to /debate/1 the $debate variable is blank.

My route says
Code:
$route['debate/:num'] = 'main/debate/:num';
Reply

#4
Your route should be


Code:
$route['debate/:num'] = 'main/debate/$1';
A good decision is based on knowledge and not on numbers. - Plato

Reply

#5
(07-12-2017, 03:48 AM)salain Wrote: Your route should be


Code:
$route['debate/:num'] = 'main/debate/$1';


I've done that now, but when I access /debate/1 in my web browser the $debate variable as seen above is blank, but when I access /main/debate/1 the $debate variable has values in it.

What is going on?
Reply

#6
I am not sure if it has an effect but I think the wildcard should be in parentheses.
I always use parentheses and I have no issues.



Code:
$route['debate/(:num)'] = 'main/debate/$1';
A good decision is based on knowledge and not on numbers. - Plato

Reply

#7
It works now. It looks like there's a mistake in the documentation which confused me.
Reply


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


Users browsing this thread:
1 Guest(s)


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2017 MyBB Group.