Click here to Skip to main content
12,452,721 members (52,633 online)
Click here to Skip to main content
Add your own
alternative version

Tagged as


1 bookmarked

Write a router for PHP MVC Applications

, 13 May 2013 CPOL
Rate this:
Please Sign up or sign in to vote.
Write a router for PHP MVC Applications with $_SERVER['PHP_SELF']


PHP MVC pattern has a router for giving data to the controller and to process it. The router has an action and a parameter, like this:

In this tip, we want to create a router like this: 

We should use $_SERVER['PHP_SELF'] to create this.

Using the Code 

Now, we explode $_SERVER['PHP_SELF'] for detecting action and parameters, so input this code in your controller and include it in index.php:

public function router() {
    $rt = $_SERVER['PHP_SELF'];
    $rt = explode('/',$rt);

    $this -> action    = $rt[2];
    $this -> parameter = $rt[3];

public function isValidRt() {
    error_reporting(E_ALL ^ E_NOTICE);
    $rt = self::router();
    if(!is_numeric($this -> action && $this -> parameter)) {
    else {
        return $rt;

posts is action and parameter is hello-world.

you can use isValidRt() to use router.

You have these two variables! You can use them. Wink | ;)



This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Web Developer
Iran (Islamic Republic of) Iran (Islamic Republic of)
I'm a persian software maker/developer & user interface designer.
I like creativity & open-source.
My dream language is javascript, i like to make & develop online applications!

My believe in web like real world, and i love it, I'm trying for design with art.
Coding for me is enjoyable & can't make me tired.

You may also be interested in...


Comments and Discussions

-- There are no messages in this forum --
| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.160826.1 | Last Updated 13 May 2013
Article Copyright 2013 by RezaFarhadian
Everything else Copyright © CodeProject, 1999-2016
Layout: fixed | fluid