CodeIgniter Forums
How do I make the route for methods in a controller all stem from the root? - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Using CodeIgniter (https://forum.codeigniter.com/forumdisplay.php?fid=5)
+--- Forum: General Help (https://forum.codeigniter.com/forumdisplay.php?fid=24)
+--- Thread: How do I make the route for methods in a controller all stem from the root? (/showthread.php?tid=68433)



How do I make the route for methods in a controller all stem from the root? - desbest - 07-10-2017

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.


RE: How do I make the route for methods in a controller all stem from the root? - skunkbad - 07-10-2017

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.


RE: How do I make the route for methods in a controller all stem from the root? - desbest - 07-12-2017

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';



RE: How do I make the route for methods in a controller all stem from the root? - salain - 07-12-2017

Your route should be


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



RE: How do I make the route for methods in a controller all stem from the root? - desbest - 07-12-2017

(07-12-2017, 02: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?


RE: How do I make the route for methods in a controller all stem from the root? - salain - 07-12-2017

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';



RE: How do I make the route for methods in a controller all stem from the root? - desbest - 07-12-2017

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