Kyle Thomas Kyle Thomas - 2 months ago 8
PHP Question

Mvc Routing - Friendly URL's

From reading up on MVC, I want to steer away from using Requests from client-side scripts and focus on a main router.

From research, urls look something like this:

http://www.example.com/controller/action/params


However, when looking at StackOverflows and other similar frameworks it looks more like this:

http://stackoverflow.com/users/1365260/example


Can someone please explain what is happening in this that's different? 1365260 looks like the users ID & example is his username so these must be the params and users is clearly the controller.

How does this framework know what action to run? Ie: search, view, update

Answer

This is not actually

/controller/action/params

You can define anything as a route.

http://www.example.com/this/is/a/route

From laravel framework perspectives- Whenever you visit a URI the framework by default redirects your request to a index page where it checks your URI in the routes list. Where each route is bound to a controllers method which handles your request. This method could do anything like search, view, create, update, delete etc.

In this routing system you can also pass parameters. Here in your example

http://stackoverflow.com/users/1365260/example

this route is defined in routes list and a method is bound with it and user id, user name these are parameters. Whenever this route is visited it runs the method and it shows the profile of the user id and user name it gets from the route parameter.

Hope this helps.