Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Routes in Codeigniter

I want to have a clean URL in CodeIgniter based application for User's Profile Information.

Please Take a look at URL formats below.

Actual URL : http://www.mydomain.com/index.php/users/profile/user1

I'm expecting users to have Personal URL's like

http://www.mydomain.com/user1
http://www.mydomain.com/user2
http://www.mydomain.com/user3

URL http://www.mydomain.com/user1 should process http://www.mydomain.com/index.php/users/profile/user1 in background execution.

I will be removing index.php from URL using Route library.

Thanks in advance for any sort of help.

like image 637
pinaldesai Avatar asked Oct 09 '12 05:10

pinaldesai


People also ask

Where is Routes file in CodeIgniter?

Routing rules are defined in your application/config/routes. php file. In it you'll see an array called $route that permits you to specify your own routing criteria. Routes can either be specified using wildcards or Regular Expressions.

What is use of routes in CodeIgniter 4?

Routing rules are defined in the app/Config/Routes. In it you'll see that it creates an instance of the RouteCollection class ( $routes ) that permits you to specify your own routing criteria. Routes can be specified using placeholders or Regular Expressions.

What is URI routing?

URI routing is the process of taking the requested URI and deciding which application handler will handle the current request.

What is routes php?

The routes/web.php file defines routes that are for your web interface. These routes are assigned the web middleware group, which provides features like session state and CSRF protection. The routes in routes/api.php are stateless and are assigned the api middleware group.


1 Answers

Have a look at https://www.codeigniter.com/user_guide/general/routing.html.

$route['user(:num)'] = "users/profile/user/$1";

If you mean you want /anyusername to route to the users controller, you would have to put:

$route['(:any)'] = "users/profile/$1";

At the bottom of routes.php and every non user-URL above it. Otherwise every URL would be routed there, obviously. You will need to implement some mechanism in the users-controller to throw 404-errors, since you are routing all requests not catched in the routing rules above.

like image 83
Andreas Bergström Avatar answered Sep 22 '22 02:09

Andreas Bergström