CodeIgniter Forums
dynamic route - 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: dynamic route (/showthread.php?tid=66108)



dynamic route - Tecvid - 09-06-2016

hi, how can i add new routes dynamically? directly in the controller, like this:

PHP Code:
class Users extends CI_Controller {

 
   public function __remap()
 
   {
 
       $route['username'] = 'users/profile/user_id'// username and user_id r from the db
 
   }

 
   public function profile($user_id)
 
   {
 
       // ...
 
   }


the __remap also can be in MY_Controller, it doesn't matter, the main is the result

any idea?


RE: dynamic route - dave friend - 09-07-2016

The short answer is you cannot - at least not in a controller. The router class is the one that consumes the $route array and it does that before the controller is loaded. Because your addition to $route is "in memory" it is gone on the next redirect or link to another page.

You might be able to create a pre_system hook that would add the dynamic route. Look at the "DEFAULT CONTROLLER" section of the main index.php file and the constructor in system/core/Router.php for inspiration on how it might be implemented.


RE: dynamic route - Tecvid - 09-08-2016

(09-07-2016, 09:25 AM)dave friend Wrote: The short answer is you cannot - at least not in a controller. The router class is the one that consumes the $route array and it does that before the controller is loaded. Because your addition to $route is "in memory" it is gone on the next redirect or link to another page.

You might be able to create a pre_system hook that would add the dynamic route. Look at the "DEFAULT CONTROLLER" section of the main index.php file and the constructor in system/core/Router.php for inspiration on how it might be implemented.

thank u dave friend! i'll check it, hope it'll help me)