Routes in Codeigniter - Automatically

I have a problem with Codeigniter routes. I would like all registered users on my site to get their own "directory", for example: www.example.com/username1 , www.example.com/username2 . This "directory" should be displayed on the controller "polica", the method "ogled", parameter "username1".

If I like it, then each controller maps to this route: "polica / ogled / parameter". This not normal:

 $route["(:any)"] = "polica/ogled/$1"; 

This works, but I always manually entered the information in routes.php :

 $route["username1"] = "polica/ogled/username1"; 

How to make it automated?

UPDATE: For example, I have a controller called ads . For example, if you go to www.example.com/ads/ ads will be placed. If you go to www.example.com/username1 , the ads are displayed by username1 . There is also a user controller, profile , latest , ...

My Current routes.php :

 $route['oglasi'] = 'oglasi'; $route['(:any)'] = "polica/ogled/$1" $route['default_controller'] = 'domov'; $route['404_override'] = ''; 

I solved the problem with this code:

 $route['oglasi/(:any)'] = 'oglasi/$1'; $route['(:any)'] = "polica/ogled/$1" $route['default_controller'] = 'domov'; $route['404_override'] = ''; 

Regards, Mario

+6
source share
4 answers

The problem with your route is that using: any that you match is actually ... ANY route, so you pretty much got stuck there. I think you can have two solutions:

1) You can selectively redirect all the controllers of your sites individually, for example:

 $route['aboutus'] = "aboutus"; $route['where-we-are'] = "whereweare"; //And do this for all your site controllers //Finally: $route['(:any)'] = "polica/ogled/$1"; 

All these routes should arrive BEFORE ANY, as they are read in the order in which they are presented, and if you put: any at the beginning, he will gladly skip everything else.

EDIT after comment:

I mean, if you are going to match any segment, this means that you cannot use any controller at all (which is the first segment of the URI by default), since the router will always redirect you using your own law. To allow CI to execute other controllers (no matter what they are, I just used some common web pages, but maybe literally everything), you need to enable them by excluding them from re-routing. And you can achieve this by putting them in front of ANY rule so that every time the CI goes through your routing rules, it first analyzes the one you "escaped" and ONLY if they do not match anything found in the URL address, it passes to the rule: ANY.

I know this is verbosity, though, but they will probably be less than 6K, as you said. Since I don't know the actual structure of your URLs and your web application, this is the only solution that comes to my mind. If you provide additional information, for example, how the regular URLs of your application are formed, then I can update my answer

/ end edit

This is not a very rational solution, because it will require a lot of code, but if you want such a design to be the only one, it comes to my mind. Also think that you can use regular expressions as an index of $ route, but I donโ€™t think it can work here, since your usernames are hardly suitable for this, but I just wanted to point out the possibility.

OR

2) You can slightly change the design template and assign a different route for usernames, something along the line

 $route['user/(:any)'] = "polica/ogled/$1"; 

This will create pretty pretty (and semantic) URLs, though, and avoid all the problems associated with avoiding other routes.

+23
source

see this: http://www.web-and-development.com/codeigniter-minimize-url-and-remove-index-php/ which includes removing index.php / remove 1st url segment / remove 2st url sigment / routing automatically . It will be very helpful for you.

+2
source

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

Define a redirection controller using the remap method. This will allow you to collect requests sent to the controller with any string of the process variable into one function. Therefore, if a request is made at http://yoursite/jeff/ or http://yoursite/jamie , it will not hit these methods, but instead will hit the http://yoursite/ remap function. (even if these methods / names do not exist and even if you have an index function, it replaces it). In the _Remap method, you can define a conditional switch that will then work with the rest of your code, redirecting the user in any way.

Then you must define this direct redirection controller as the default by default and configure your routes as follows:

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

This is initially a problem, because it will basically force everything to redirect to this controller no matter what, and ultimately using this _remap switch.

But what you can do is to define rules / routes that you do not want to abide by this condition over these route instructions.

i.e

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

I found that this creates an excellent system in which I can have as many user variables as defined, where I can easily redirect them based on what they support through one controller.

+1
source

Another way is to declare an array with your intenal controllers and redirect everything else to the user controller, like this, in codeigniter's routes.php file:

 $controllers=array('admin', 'user', 'blog', 'api'); if(array_search($this->uri->segment(1), $controllers)){ $route['.*'] = "polica/ogled/$1"; } 
+1
source

Source: https://habr.com/ru/post/898428/


All Articles