Routes in Codeigniter - Automatically

后端 未结 4 1903
自闭症患者
自闭症患者 2020-12-03 19:06

I have a problem with Codeigniter routes. I would like to all registered users on my site gets its own \"directory\", for example: www.example.com/username1,

4条回答
  •  余生分开走
    2020-12-03 19:23

    I was struggling with this same problem very recently. I created something that worked for me this way:

    Define a "redirect" controller with a remap method. This will allow you to gather the requests sent to the contoller with any proceeding variable string into one function. So if a request is made to http://yoursite/jeff/ or http://yoursite/jamie it won't hit those methods but instead hit http://yoursite/ remap function. (even if those methods/names don't exist and even if you have an index function, it supersedes it). In the _Remap method you could define a conditional switch which then works with the rest of your code re-directing the user any way you want.

    You should then define this re-direct controller as the default one and set up your routes like so:

    $route['(.*)'] = "redirect/index/$1";
    $route['default_controller'] = "redirect";
    

    This is at first a bit of a problem because this will basically force everything to be re-directed to this controller no matter what and ultimately through this _remap switch.

    But what you could do is define the rules/routes that you don't want to abide to this condition above those route statements.

    i.e

    $route['myroute'] = "myroute";
    $route['(.*)'] = "redirect/index/$1";
    $route['default_controller'] = "redirect";
    

    I found that this produces a nice system where I can have as many variable users as are defined where I'm able to redirect them easily based on what they stand for through one controller.

提交回复
热议问题