Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Implement URL Routing in PHP

How to implement URL Routing in PHP.

like image 873
Rajasekar Avatar asked Aug 22 '09 14:08

Rajasekar


People also ask

How does php routing work?

In its most common configuration, PHP relies on the web server to do the routing. This is done by mapping the request path to a file: If you request www.example.org/test.php, the web server will actually look for a file named test. php in a pre-defined directory.

What is routing in MVC php?

The Router attempts to match the first one, or two, parts of the path component to a corresponding route combination ( Controller / Action [method], or just a Controller that executes a default action (method). An action, or command, is simply a method off of a specific Controller .

How does Symfony routing work?

Symfony provides a Routing component which allows us, for a HTTP request/URL, to execute a specific function (also known as "Controller"). Note: Controllers must be a callable, for example: an anonymous function: $controller = function (Request $request) { return new Response() }; .


2 Answers

If you use Apache you can do the URL routing via mod_rewrite.

Small example:

RewriteEngine On
RewriteRule ^(dir1)/?(path2)? main.php?dir=$1&path=$2

That'll have any request like

http://yoursite.com/dir1/path1 

served by

http://yoursite.com/main.php?dir=dir1&path=path2

More examples here.

The other alternative have every request redirect to a single php file

RewriteEngine On
RewriteRule (.*) main.php?request=$1

and then to do it in code, where you can use a similar approach, by having a set of regular expressions that are matched by some code and then redirected via header() or just internally.

like image 165
Vinko Vrsalovic Avatar answered Sep 19 '22 14:09

Vinko Vrsalovic


First of all, you will need Apache's (I suppose your webserver is Apache) mod_rewrite to be enabled.


Then, you need to create a RewriteRule to redirect everything to your index.php page.
Something like this could do :

RewriteEngine on 
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule .* index.php 

It will redirect every request to a file that doesn't exist to index.php ; this means that, if the requested URL is www.example.com/blah, it is actually index.php that will be called.

About that, here a couple of link that can help :

  • URL Rewriting Guide
  • mod_rewrite: A Beginner's Guide to URL Rewriting


Then, this page has to determine what has to be displayed, depending on what initial URL was called -- or what parameters are received.

This can be done using the Design Pattern Front Controller, for instance -- it's implemented in most modern PHP Frameworks, for instance.


There have been many questions of this subject on SO ; some of those (and their answers) might probably help you. For instance :

  • How do I do URL rewriting in php?
  • URL mapping in PHP?
  • PHP Application URL Routing
like image 21
Pascal MARTIN Avatar answered Sep 18 '22 14:09

Pascal MARTIN